[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.

Note

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.

Lab 1: Setting Up and Exploring the SDK

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.

Prerequisites

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.

Task 1: Creating an IAM policy

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.

  1. In the AWS Management Console, choose Services, and then open the IAM dashboard by choosing IAM.

  2. In the navigation pane, choose Policies.

  3. Choose Create policy.

  4. 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": "*"
        }
      ]
    }
  5. 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.

  6. Choose Next: Review.

  7. For Name, enter: BuildingModernAppsPolicy

  8. Choose Create policy.

You successfully created an IAM policy.

Task 2: Creating an IAM user and attaching a policy to the user

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.

Expand for step-by-step instructions.
  1. In the AWS Management Console, choose Services, and then open the IAM dashboard by choosing IAM.

  2. In the navigation pane, choose Users.

  3. Choose Add user.

  4. In the User name text box, enter: BuildingModernAppsUser

  5. For Access type, select Programmatic access and AWS Console access.

  6. 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.

  7. Choose Next: Permissions.

  8. In the Set permissions section, choose Attach existing policies directly.

  9. In the search text box for Filter policies, enter BuildingModernAppsPolicy and in the filtered list, select BuildingModernAppsPolicy.

  10. Choose Next: Tags. In this exercise, you don’t need to create tags for the user.

  11. Choose Next: Review.

  12. Review the information, and choose Create user. You should see a success message.

  13. Choose Download .csv. Store the CSV file in a safe location. The CSV file contains:

    • User name
    • Password
    • Access key ID
    • Secret access key
    • Console login link
  14. 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).

Task 3: Installing and configuring the AWS CLI and AWS Tools for PowerShell

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.

  1. 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.

  2. In the PowerShell command prompt, run the installer in passive mode.

    .\AWSCLIV2.msi /passive
  3. 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.

  4. In the PowerShell command prompt, configure the AWS CLI with the aws configure command.

  5. When you are prompted for:

    AWS configure options

    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.

  6. 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.

  7. 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.

    Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted ; Install-Module -Name AWSPowerShell.NetCore

Task 4: Creating an S3 bucket

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.

  1. 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.

    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.

  2. 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.

  3. 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.

Task 5: Deploying a web application to the S3 bucket

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.

  1. Download and extract the web application.

    Invoke-WebRequest RELATIVEdownloads/webapp1.zipRELATIVE -OutFile webapp1.zip ; Expand-Archive webapp1.zip -f
  2. Copy all the contents of the webapp1 folder to Amazon S3.

  3. 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
  4. 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.

    Dragons App

Task 6: Setting up the Dragons application

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.

  1. Download the source of the dragons data: dragon_stats_one.txt.

    Invoke-WebRequest RELATIVEdownloads/dragon_stats_one.txtRELATIVE -OutFile dragon_stats_one.txt
  2. Copy the dragons data in dragon_stats_one.txt to the root of the S3 bucket.

  3. Confirm that the S3 bucket has been populated. You will see the dragonsapp/ prefix and a dragon_stats_one.txt object.

  4. Set the Parameter Store value for dragon_data_bucket_name.

  5. Set the Parameter Store value for the dragon_data_file_name.

Task 7: Exploring the SDK console application

In this task, you will explore the SDK.

  1. 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
  2. Open the explore-sdk\App.sln solution in Visual Studio.

  3. Observe that the AWSSDK dependencies were added to the App project.

    Dependencies

    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" />
  4. Set some breakpoints in the Main, ReadDragonData, QueryS3, and GetQuery methods.

  5. 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.

Challenge

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 = ???,
});