Deploy Front-end

In this section, we will add the S3 bucket that hosts the web application to the SAM template, connect the FCAJ-Serverless-DMS application to the Cognito resources and the S3 bucket we just created, then test the sign-up and sign-in features.

  1. Open the template.yaml file in the fcajdms-sam folder. Add the following code below the FcajDMSDocumentStore bucket of part 1.1 (before the Outputs section):
  # S3 bucket that hosts the web application (static website hosting)
  FcajDMSWebStore:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "fcajdmswebstore-${AWS::AccountId}-${AWS::Region}-an"
      BucketNamespace: account-regional
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        IgnorePublicAcls: true
        BlockPublicPolicy: false
        RestrictPublicBuckets: false
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: index.html

  FcajDMSWebStorePolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref FcajDMSWebStore
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Sid: PublicReadGetObject
            Effect: Allow
            Principal: "*"
            Action: s3:GetObject
            Resource: !Sub "${FcajDMSWebStore.Arn}/*"
  • FcajDMSWebStore: the bucket that hosts the web application. Its name looks like fcajdmswebstore-<ACCOUNT_ID>-ap-southeast-1-an, the same as the bucket you created in workshop 135.
  • PublicAccessBlockConfiguration: allows a public bucket policy but still blocks public ACLs.
  • WebsiteConfiguration: enables Static website hosting with index.html as the Index document and the Error document. The application is a Single Page Application, so when you reload a page at a path such as /document or /profile, S3 returns index.html instead of a 404 error.
  • FcajDMSWebStorePolicy: the bucket policy that allows everyone to read the objects in the bucket, the same as the PublicReadGetObject policy of workshop 135.

WebStore

  • Add the following two values to the bottom of the Outputs section:
  WebStoreBucket:
    Description: S3 bucket that hosts the web application
    Value: !Ref FcajDMSWebStore
  WebsiteURL:
    Description: S3 static website URL
    Value: !GetAtt FcajDMSWebStore.WebsiteURL

WebStore

  1. Run the following commands to build and redeploy the SAM project:
sam build
sam deploy
  • Enter y when asked Deploy this changeset?

SAMDeploy

  1. Open the CloudFormation console, select the fcajdms stack, then select the Outputs tab. Write down the UserPoolId, UserPoolClientId, IdentityPoolId, DocumentStoreBucket, WebStoreBucket and WebsiteURL values.

StackOutputs

  • You can also view these values with the following command:
aws cloudformation describe-stacks --stack-name fcajdms --query "Stacks[0].Outputs" --output table

If you do not have the FCAJ-Serverless-DMS folder on your computer, run the following commands:
git clone https://github.com/Hieu192/FCAJ-Serverless-DMS.git
cd FCAJ-Serverless-DMS
npm install

  1. In workshop 134, Amplify Gen 2 generated the amplify_outputs.json file automatically so that the application could connect to Cognito and S3. In this workshop, we create that file ourselves from the stack Outputs. Create (or overwrite) the amplify_outputs.json file in the root folder of the FCAJ-Serverless-DMS project (next to the package.json file) with the following content:
{
  "version": "1.3",
  "auth": {
    "aws_region": "ap-southeast-1",
    "user_pool_id": "USER_POOL_ID",
    "user_pool_client_id": "USER_POOL_CLIENT_ID",
    "identity_pool_id": "IDENTITY_POOL_ID",
    "mfa_methods": [],
    "standard_required_attributes": ["email"],
    "username_attributes": ["email"],
    "user_verification_types": ["email"],
    "groups": [],
    "mfa_configuration": "NONE",
    "password_policy": {
      "min_length": 8,
      "require_lowercase": true,
      "require_numbers": true,
      "require_symbols": true,
      "require_uppercase": true
    },
    "unauthenticated_identities_enabled": true
  },
  "storage": {
    "aws_region": "ap-southeast-1",
    "bucket_name": "DOCUMENT_STORE_BUCKET"
  }
}
  • Replace USER_POOL_ID with the UserPoolId value
  • Replace USER_POOL_CLIENT_ID with the UserPoolClientId value
  • Replace IDENTITY_POOL_ID with the IdentityPoolId value
  • Replace DOCUMENT_STORE_BUCKET with the DocumentStoreBucket value
  • Save the file with UTF-8 encoding

AmplifyOutputs

The amplify_outputs.json file left from workshop 134 points to the Amplify sandbox that was deleted in the cleanup of workshop 135, so you must replace the whole content of the file. Do not run npx ampx sandbox again in this workshop; that command recreates an Amplify backend and overwrites amplify_outputs.json with the information of another User Pool. If the application shows a message asking you to run npx ampx sandbox, it means the application cannot find amplify_outputs.json; check the name and the location of the file.

The values in amplify_outputs.json are not secrets; they end up in the JavaScript code of the application after the build. The file is already listed in the .gitignore of the project, so it is not pushed to Git.

  1. Run the following commands in the root folder of the FCAJ-Serverless-DMS project to build the application and upload it to the S3 bucket that hosts the website:
npm run build
aws s3 cp dist s3://WEB_STORE_BUCKET --recursive
  • Replace WEB_STORE_BUCKET with the WebStoreBucket value, for example: aws s3 cp dist s3://fcajdmswebstore-111122223333-ap-southeast-1-an --recursive
  • The npm run build command creates the dist folder that contains the application built with the information in amplify_outputs.json. If you change this file, build and upload again.

BuildApp

  1. Paste the WebsiteURL value into your web browser to open the application, then click Sign up

Signup

We have not created the API yet, so you will see errors in the browser Console when the application fetches the document list. This is expected; we will create the API in part 2.

  1. Enter the user information:
  • A Display name, for example: admin
  • Your email.
  • A password, for example: Admin@123
  • Click Sign up

Signup

The password must have at least 8 characters with uppercase, lowercase, number and special characters, matching the password policy you declared for the User Pool in part 1.1.

  1. Open the console of the fcaj-dms-user-pool Cognito User Pool again and select User management -> Users on the left menu. You will see a registered user with the Unconfirmed status.

Signup

  1. Open your email to get the confirmation code that was sent automatically.

Signup

  1. Enter the confirmation code in the application and click Submit

Signup

  1. Go back to the Cognito User Pool console and click the Refresh icon. You will see the user with the Confirmed status.

Signup

  1. Sign in to the application with the email and password you registered.

Signin

  1. You have signed in successfully. The Home page shows your display name.

Signin

  1. Select My Profile on the left menu, then click Update profile

UpdateProfile

  1. Enter the old password, the new password and confirm the new password. Then click Update

UpdateProfile

  1. Click OK

UpdateProfile

  1. Click Sign out on the left menu to sign out

UpdateProfile

  1. Click Sign in to sign in again. Enter your email and the new password, then click Sign in

UpdateProfile

  1. You have signed in successfully with the new password.

UpdateProfile

You have hosted the web application on S3, authenticated users with Cognito and prepared the S3 bucket that stores documents with AWS SAM. In the next part, we will set up the DynamoDB table, the Lambda functions and the REST API of the application.