[version_1.0]
© 2021 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training. All trademarks are the property of their owners.
The exercises in this course will have an associated charge in your AWS account. In this exercise, you will create the following resources:
The final exercise includes instructions to delete all the resources that you create in the exercises.
Familiarize yourself with Amazon S3 pricing and the AWS Free Tier.
In this lab, you will install and configure the AWS Command Line Interface (AWS CLI) and AWS Tools for PowerShell. After you install the tools, you will create an Amazon Simple Storage Service (Amazon S3) bucket and deploy a web application to the bucket. You will set up data in Amazon S3, and configure AWS Systems Manager parameters for the Dragons application. When all resources are created, you will explore the software development kit (SDK). A simple console application solution has been provided, which you will run and debug in Microsoft Visual Studio.
This lab requires access to a Microsoft Windows PC with PowerShell, and Visual Studio 2019 or greater. Visual Studio Community is a free, fully-featured integrated development environment (IDE) for students, open-source developers, and individual developers. A download for Visual Studio is available from Visual Studio Community.
In this task, you will create a customer managed policy in AWS Identity and Access Management (IAM). Customer managed policies provide more precise control over your policies than policies that are managed by AWS. This policy will have permissions that are specific to the AWS resources you need for this course.
In the AWS Management Console, choose Services, and then open the IAM dashboard by choosing IAM.
In the navigation pane, choose Policies.
Choose Create policy.
Choose the JSON tab.
In the editor text box, replace the sample policy with the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"apigateway:*",
"lambda:*",
"s3:*",
"ssm:*",
"logs:*",
"iam:*",
"cloudwatch:*",
"cognito-identity:*",
"cognito-idp:*",
"states:*",
"kms:ListAliases",
"xray:*"
],
"Resource": "*"
}
]
}
Choose Next: Tags. You can optionally create tags to help identify, organize, or search for your policy. The exercise doesn’t require you to create any tags.
Choose Next: Review.
For Name, enter: BuildingModernAppsPolicy
Choose Create policy.
You successfully created an IAM policy.
In this task, you will create an IAM user and attach a policy to the user. If you are familiar with IAM users, you might want to complete this section before you read the step-by-step instructions.
IAM User Name | BuildingModernAppsUser |
Access Type | Programmatic access and AWS Console access |
Policy | BuildingModernAppsPolicy |
Important: IAM creates a comma-separated values file to store the user name, password, access key ID, secret access key, console login link for the user. Retrieve this CSV file by choosing the Download.csv button. Store the CSV file in a safe location.
In the AWS Management Console, choose Services, and then open the IAM dashboard by choosing IAM.
In the navigation pane, choose Users.
Choose Add user.
In the User name text box, enter: BuildingModernAppsUser
For Access type, select Programmatic access and AWS Console access.
For Console password, choose Autogenerated password or Custom password. If you choose Autogenerated, you will be prompted to change your console password when you log in to the AWS Management Console as the BuildingModernAppsUser user. Note the password.
Choose Next: Permissions.
In the Set permissions section, choose Attach existing policies directly.
In the search text box for Filter policies, enter BuildingModernAppsPolicy
and in the filtered list, select BuildingModernAppsPolicy.
Choose Next: Tags. In this exercise, you don’t need to create tags for the user.
Choose Next: Review.
Review the information, and choose Create user. You should see a success message.
Choose Download .csv. Store the CSV file in a safe location. The CSV file contains:
Sign out of the console. Use the console login link to sign in as the BuildingModernAppsUser IAM user. You will be prompted to change password for BuildingModernAppsUser. Change the password and store your new password somewhere safe (for example, update the CSV file that you downloaded in this exercise).
In this task, you will download the AWS CLI and AWS Tools for PowerShell to your Windows computer. After you download and install them, you will configure the tools with the access key for BuildingModernAppsUser.
On your Windows computer, open a new PowerShell command prompt. Download the AWS CLI installer by using the following command.
Invoke-WebRequest https://awscli.amazonaws.com/AWSCLIV2.msi -OutFile AWSCLIV2.msi
You can learn more about the AWS CLI in the AWS documentation: Installing, updating, and uninstalling the AWS CLI version 2 on Windows.
In the PowerShell command prompt, run the installer in passive mode.
.\AWSCLIV2.msi /passive
Close the PowerShell command prompt and open a new PowerShell command prompt.
The installer added the aws
command to the system path. The updated path will take effect when you open a new PowerShell command prompt.
In the PowerShell command prompt, configure the AWS CLI with the aws configure
command.
When you are prompted for:
us-east-1
.The exercises in this course create infrastructure in the us-east-1 AWS Region in Northern Virginia, US. You can experiment with creating all the infrastructure in another Region. If you are new to the concepts in this course, we recommend following the instructions that will create all resources in us-east-1.
Observe the location where the AWS CLI stores credentials by using the Get-Content ~\.aws\credentials
and Get-Content ~\.aws\config
commands. This location is referred to as the shared credentials file.
To install the AWS Tools for PowerShell, you must first register the PowerShell gallery as a trusted repository.
When prompted, accept the NuGet provider installation.
-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted ; Install-Module -Name AWSPowerShell.NetCore Set
In this task, you will create an S3 bucket. This bucket will store the web application frontend. The commands for both the AWS CLI and AWS Tools for PowerShell are supplied.
In a PowerShell window, run the following command. You will be prompted for a bucket name, then the following command creates the bucket. You need to use a unique bucket name. For example, you could combine your initials and -dragons-app to create a bucket name like hjs-dragons-app.
AWS Tools for PowerShell
$MYBUCKET= Read-Host -Prompt 'Please enter a bucket name' ; New-S3Bucket -BucketName $MYBUCKET
AWS Command Line Interface
$MYBUCKET= Read-Host -Prompt 'Please enter a bucket name' ; aws s3 mb s3://$MYBUCKET
If your bucket name isn’t unique, you will see an error message: An error occurred (BucketAlreadyExists) when calling the CreateBucket operation: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.
Continue running command with a new bucket name until you have successfully created a bucket.
The subsequent commands in this exercise will refer to the $MYBUCKET
variable in your PowerShell session. If you close your PowerShell window for any reason, return to this command to set up the $MYBUCKET
variable.
List the buckets in your account. Confirm that the newly created bucket is in the response. The documentation for PowerShell cmdlets is available here: AWS Tools for PowerShell Cmdlet Reference. The documentation for the AWS CLI is available here: AWS CLI Command Reference.
AWS Tools for PowerShell
-S3Bucket Get
AWS Command Line Interface
ls aws s3
Which credentials did Get-S3Bucket
use to authenticate this request? To see the configured credentials, use the Get-AWSCredential -ListProfileDetail
cmdlet. The AWS Tools for PowerShell also uses the shared credentials file.
In this task, you will deploy a simple web application to the S3 bucket. Amazon S3 will become the web server for your static website, which includes the HTML, images, and client-side scripts. Instructions to copy the web application are supplied for both the AWS CLI and the AWS Tools for PowerShell.
Download and extract the web application.
Invoke-WebRequest RELATIVEdownloads/webapp1.zipRELATIVE -OutFile webapp1.zip ; Expand-Archive webapp1.zip -f
Copy all the contents of the webapp1
folder to Amazon S3.
AWS Tools for PowerShell
-S3Object `
Write-BucketName $MYBUCKET `
-KeyPrefix dragonsapp `
-Folder webapp1 `
-CannedACLName public-read `
-Recurse
AWS Command Line Interface
cp webapp1 s3://$MYBUCKET/dragonsapp/ --recursive --acl public-read aws s3
Use Write-Host
to build the URL to your web application. Copy the output of the command, and paste it into a browser to visit the web application, which is hosted on Amazon S3.
Write-Host https://$MYBUCKET.s3.amazonaws.com/dragonsapp/index.html
Confirm that your web application works. It will look like the following screen capture. Bookmark this application in your browser because you will return to it in future labs.
In this task, you will create resources that will be used by the Dragons application. You download the file that contains the dragons data in JavaScript Object Notation (JSON) format, and upload this data to your S3 bucket. Then, you create two parameters for Systems Manager. The console application that you create after this section uses the dragons data for querying and the parameters for configuration.
Download the source of the dragons data: dragon_stats_one.txt
.
Invoke-WebRequest RELATIVEdownloads/dragon_stats_one.txtRELATIVE -OutFile dragon_stats_one.txt
Copy the dragons data in dragon_stats_one.txt
to the root of the S3 bucket.
AWS Tools for PowerShell
-S3Object `
Write-BucketName $MYBUCKET `
-File dragon_stats_one.txt
AWS Command Line Interface
cp dragon_stats_one.txt s3://$MYBUCKET aws s3
Confirm that the S3 bucket has been populated. You will see the dragonsapp/
prefix and a dragon_stats_one.txt
object.
AWS Tools for PowerShell
-S3Object -BucketName $MYBUCKET Get
AWS Command Line Interface
ls s3://$MYBUCKET aws s3
Set the Parameter Store value for dragon_data_bucket_name
.
AWS Tools for PowerShell
-SSMParameter `
Write-Name "dragon_data_bucket_name" `
-Type "String" `
-Overwrite $true `
-Value $MYBUCKET
AWS Command Line Interface
-parameter `
aws ssm put--name "dragon_data_bucket_name" `
--type "String" `
--overwrite `
--value $MYBUCKET
Set the Parameter Store value for the dragon_data_file_name
.
AWS Tools for PowerShell
-SSMParameter `
Write-Name "dragon_data_file_name" `
-Type "String" `
-Overwrite $true `
-Value dragon_stats_one.txt
AWS Command Line Interface
-parameter `
aws ssm put--name "dragon_data_file_name" `
--type "String" `
--overwrite `
--value dragon_stats_one.txt
In this task, you will explore the SDK.
Download and extract the console application.
Invoke-WebRequest RELATIVEdownloads/explore-sdk.zipRELATIVE -OutFile explore-sdk.zip ; Expand-Archive explore-sdk.zip -f
The archive contains a Visual Studio solution, and a single project for the console application.
explore-sdk
├── .gitignore
├── App
│ ├── .editorconfig
│ ├── App.csproj
│ └── Program.cs └── App.sln
Open the explore-sdk\App.sln
solution in Visual Studio.
Observe that the AWSSDK
dependencies were added to the App project.
Inside the explore-sdk\App\App.csproj
project file, you will see the PackageReference
for each package.
<PackageReference Include="AWSSDK.Core" Version="3.7.0.18" />
<PackageReference Include="AWSSDK.S3" Version="3.7.0.19" />
<PackageReference Include="AWSSDK.SimpleSystemsManagement" Version="3.7.2" />
Set some breakpoints in the Main
, ReadDragonData
, QueryS3
, and GetQuery
methods.
To start debugging the application, choose Debug > Start Debugging. When the application completes, you will see the JSON output from an s3 select
operation. Spend some time experimenting with the code to understand the APIs for the AWS SDKs.
To help with some debugging, update an object named last-queried.txt
with the current date and time for each time that you query the S3 bucket.
To get started, add the following code fragment to the QueryS3
method. You can familiarize yourself with the online reference for PutObjectRequest.
string currentTime = DateTime.UtcNow.ToString("o");
var response = await s3.PutObjectAsync(new PutObjectRequest
{
= ???,
ContentBody = ???,
BucketName = ???,
Key });