Skip to main content

Creating a Serverless Application Using S3, HTTP API, Lambda, and DynamoDB

Hands-On Lab

 

Photo of Mark Richman

Mark Richman

AWS Training Architect II in Content

Length

01:45:00

Difficulty

Intermediate

Serverless allows you to build and run applications and services without thinking about servers. You can build them for nearly any type of application or backend service, and everything required to run and scale your application with high availability is handled for you. AWS provides a set of fully managed services that you can use to build and run serverless applications.

In this hands-on lab, you will build a serverless web application that utilizes a number of AWS services, including S3, API Gateway, Lambda, and DynamoDB.

Lab Prerequisites

  • Understand how to log in to and use the AWS Management Console.
  • Understand how to use the AWS Command Line Interface (CLI).
  • Understand how to use ssh from the command line.
What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Serverless Application Using S3, HTTP API, Lambda, and DynamoDB

Introduction

Serverless allows you to build and run applications and services without thinking about servers. You can build them for nearly any type of application or backend service, and everything required to run and scale your application with high availability is handled for you. AWS provides a set of fully managed services that you can use to build and run serverless applications.

In this hands-on lab, you will build a serverless web application that utilizes a number of AWS services, including S3, API Gateway, Lambda, and DynamoDB.

Lab Prerequisites

  • Understand how to log in to and use the AWS Management Console.
  • Understand how to use the AWS Command Line Interface (CLI).
  • Understand how to use ssh from the command line.

The Github repository used in this lab is here: https://github.com/linuxacademy/content-aws-sam

Logging In

Use the credentials provided on the hands-on lab page, and log in as cloud_user. Make sure you are operating the lab within the N. Virginia (us-east-1) region.

Create DynamoDB Table

  1. Navigate to DynamoDB in the AWS Management Console, and click on the Create table button.
  2. Give it the name friends:
    1. Set the partition key to id of type Number.
    2. Check Add sort key.
    3. Set the sort key value to name of type String.
  3. Leave Use default settings checked.
  4. Click Create.
  5. Wait until the Table status reads Active.
  6. Log into the EC2 instance with SSH, using the credentials provided. Remember, these credentials are not the same as the ones used for logging into the AWS console.
  7. Clone the repository:
    git clone https://github.com/linuxacademy/content-aws-sam
  8. Change to the directory for this lab:
    cd content-aws-sam/labs/Creating-Serverless-Application-S3-HTTP-API-Lambda-DynamoDB
  9. Seed the DynamoDB table with data:
    ./dynamodb.py friends

Create Lambda Function

  1. Navigate to Lambda in the AWS Management Console.
  2. Click Create function.
  3. Leave Author from scratch selected.
  4. Set Function name to GetFriends.
  5. Set Runtime to Python 3.8.
  6. Under Permissions, expand Choose or create an execution role.
  7. Select Create a new role from AWS policy templates.
  8. Set Role name to GetFriendsRole.
  9. Click Create function.
  10. In the Function code editor, replace the boilerplate code with the contents of https://github.com/linuxacademy/content-aws-sam/blob/master/labs/Creating-Serverless-Application-S3-HTTP-API-Lambda-DynamoDB/lambda_function.py.
  11. Under Environment variables, click Manage environment variables.
  12. Click Add environment variable.
  13. Set Key to TABLE_NAME.
  14. Set Value to friends.
  15. Click Save.
  16. Click Save (Lambda function).

Create HTTP API

  1. Navigate to API Gateway in the AWS Management Console, and click Get started, then click OK on the Create your first API window that pops up.
  2. Select APIs up at the top of the next screen, then click Build under the HTTP API API type.
  3. Click Add integration.
  4. Under Integration type select Lambda.
  5. Under Integration target select us-east-1.
  6. Choose the Lambda function named GetFriends.
  7. Under API name, enter GetFriends.
  8. Click Review and Create.
  9. Click Create.
  10. In the left navigation menu, under Develop, click CORS.
  11. Click Edit.
  12. Under Access-Control-Allow-Origin enter *.
  13. Click Add.
  14. Click Save.
  15. Now click on our API: GetFriends... link in the left-hand menu.
  16. Under Stages for GetFriends, click on the Invoke URL for our $default API:
    • This will give us a Not Found message. We need to add /GetFriends to the end of the URL in our browser, after the ...amazon.com.

Create S3 Bucket

  1. Navigate to S3 in the AWS Management Console.
  2. Select Create bucket.
  3. Under Bucket name enter something unique. There can be no other S3 bucket in all of AWS with the same name.
  4. Under Region select US East (N. Virginia) us-east-1.
  5. Uncheck Block all public access.
  6. Check the acknowledgement box.
  7. Click Create bucket. There will be an error here if a bucket with the name we chose already exists, so it might take a couple of tries of renaming it before this works.
  8. In the next screen, click the bucket name to get into it.
  9. In the Properties tab, click Static website hosting.
  10. Select Use this bucket to host a website.
  11. Under index document enter index.html.
  12. Click Save.
  13. In the Permissions tab, under Bucket Policy, paste the contents of https://github.com/linuxacademy/content-aws-sam/blob/master/labs/Creating-Serverless-Application-S3-HTTP-API-Lambda-DynamoDB/bucket_policy.json.
  14. Replace my-bucket-name with the actual name of the bucket. Make sure that /* is still there. The line should look something like this:
    "arn:aws:s3:::friends12345678/*"
  15. Click Save.

Configure and Deploy Web Application

  1. On the EC2 instance, change to the app directory:
    cd app
  2. Edit app.js:
    vim app.js

    Replace the XXXXXXXXXX part of the invokeUrl with the HTTP API Invoke URL, which we'll need to grab from the AWS console (AWS Console > API Gateway > GetFriends). We don't actually need the whole Invoke URL, just the API ID.

  3. Save the file.
  4. Copy the application to S3:
    aws s3 sync . s3://<bucket name>
  5. Browse to the S3 website URL http://<bucket name>.s3-website-us-east-1.amazonaws.com.

Conclusion

If we see a page showing a list of friends, with pictures, we're done. Congratulations on completing this hands-on lab!