AWS hands-on serverless project | April 2023 | Wild Rydes: Completed & updated.
AWS services used: Amazon API Gateway, Amazon Cognito, AWS Amplify/Amplify CLI, AWS Cloud9/Cloud9 IDE, AWS Console Home, AWS Lambda, CloudFormation, CloudWatch, CodeCommit, DynamoDB, EC2, IAM.
Introduction
After an excellent AWS hands-on bootcamp, I looked for more hands-on projects to evaluate other AWS services and features. Serverless was the key word I used when finding the “Wild Rydes” project on the AWS github page “Wild Rydes Serverless Workshops”.
Web Application — This workshop shows you how to build a dynamic, serverless web application. You’ll learn how to host static web resources with Amazon S3, how to use Amazon Cognito to manage users and authentication, and how to build a RESTful API for backend processing using Amazon API Gateway, AWS Lambda and Amazon DynamoDB. — https://webapp.serverlessworkshops.io/
4 Modules to follow, in sequence:
1 • Static Website Hosting = Deploying a static website using AWS Amplify Console by first creating a git repository in CodeCommit and then pushing the site code.
2 • User Management = Configure user management for the website using Amazon Cognito.
3 • Serverless Backend = Create an AWS Lambda function that will persist data to an Amazon DynamoDB table.
4 • RESTful APIs = Expose the Lambda function via an Amazon API Gateway as a RESTful API that the static site can call.
Important • Big issue with Wild Rydes = the EC2 instance needs more RAM
What was supposed to last a few hours actually turned out to take me a few days, spent troubleshooting, as there was a serious issue (Issue #1, see below) preventing me from moving forward and completing the project. Troubleshooting old projects is a classic, this one was released in 2020, and it is April 2023.
Long story short: it is possible to complete this project with no issue, if you make sure to work with a t2.small EC2 instance, instead of a t2.micro. This solves Issue #1 = RAM issue — error message = “Amplify CLI installation failed due to insufficient memory”.
Module 1 • Static Web Hosting
First step is using AWS Cloud9 IDE to create the development environment. This is where the EC2 instance selection is important, to avoid Issue #1:
New EC2 instance
Additional instance types
t2.small (2 GiB RAM + 1 vCPU).
Please Note = the t2.small is not part of the AWS Free Tier resources: it will cost a few pennies per hour, but it is worth it.
May 2023 Update = cost of the t2.small for 8 hours = I got billed $0.24
Next step is to setup the development environment and repository: Using the Cloud9 IDE Terminal, a repository is created, workshop files are cloned, website files are set up, and a first version is committed.
Note: repository is in CodeCommit. It could be created in GitHub, but this is an opportunity to try something different, and very well integrated.
Next step is to deploy the website — or web app, using AWS Amplify console. To allow Amplify Backend Deployment to access AWS resources, a new role is created in IAM, and the AWSCodeCommitReadOnly policy is attached to it. After review, the app is deployed.
Once deployed, the app is live and the website can be accessed from a browser. To verify that all is working well, a modification is made — in Cloud9 IDE — to the public/index.js homepage. After the git commit is pushed to the repository, AWS Amplify goes into a new cycle of provisioning, building, and deploying the update, accessible in a few minutes.
Module 2 • User Management
Install AWS Amplify CLI and use it to create an Amazon Cognito User Pool to manage the User accounts. 2 issues were identified in this module:
Issue #1 = RAM issue — error message = “Amplify CLI installation failed due to insufficient memory”. Amplify CLI could not install when the environment had a t2.micro EC2 instance. Resolution = install a t2.small EC2 instance.
Issue #2 = Not the right content in the ~/.aws/config file. After installing Amplify CLI, the content of the ~/.aws/config file was not as expected, and it must be edited before running “amplify init”. Resolution = use a text editor (vi, nano…) to edit the content of the ~/.aws/config file so it looks like this:
[profile default]
region = us-east-1
output = json
With a t2.small EC2 instance and a correctly edited ~/.aws/config file, the 2 issues have been resolved and it is possible to run “amplify init” and successfully install Amplify CLI. We then use the AWS Amplify Authentication module, which provides Authentication APIs, to create an Amazon Cognito User Pool, via the “amplify add auth” command.
After the changes were committed, AWS Amplify went into action to provision, build, and deploy a new update, this time including a backend to handle Users. It is now possible to create a new account, with an automatic confirmation email sent, with a code, necessary to validate the account. All that is managed by Amazon Cognito.
Once signed in, it is possible for a User to reach the /ride page, which is currently inactive.
Module 3 • Serverless Backend
Next step is to create an Amazon DynamoDB table. It will be used to record all User requests sent by the lambda function triggered each time a User calls for a dispatch. A new role is created in IAM, for the lambda function to be allowed access to the Amazon DynamoDB table.
A lambda function is created to handle the requests. The function code was provided for the Workshop. Basically, the function selects a unicorn from the fleet, records the request in a DynamoDB table, and responds to the front-end application with details about the dispatched unicorn.
The lambda function can be tested in the AWS Lambda console, to check if it works as designed:
Module 4 • RESTful APIs
Using Amazon API Gateway to create a new REST API, set up an Authorizer, connect the Amazon Cognito User Pool, test the connection with an authorization token. Then create a resource, a method, and finally deploy the API.
Need to update the src/config file to include the “invoke URL”, then commit the changes to the repository. AWS Amplify then provisions, builds, and deploys the update, which can finally be tested on a browser.
Module 5 • Clean Up — Leftovers
It is important to clean up after any hands-on project, to make sure that there a no resources incurring costs. For Free Tier users, the t2.small is not part of the Free Tier resources, which means it costs a few pennies each hour. Better to make sure everything is correctly cleaned up.
Once you are done cleaning up per AWS Workshop documentation, here are a few additional steps, to make sure you have a pristine clean up:
AWS Lambda = You may have noticed a few lambda functions related to amplify-wildrydes. Amplify takes a few minutes to clean up, the functions are visible until deleted. Check to make sure they are gone.
Message = There is no data to display.
CloudWatch = You can delete these 2 Log groups:
/aws/lambda/amplify-wildrydes-prod-XXXXXX-UserPoolClientLambda-XXXXXXXXXX
/aws/lambda/amplify-wildrydes-prod-XX-UpdateRolesWithIDPFuncti-XXXXXXXXXX
Message = No log groups / You have not created any log groups.
CloudFormation = You can delete this stack:
amplify-wildrydes-prod-XXXXXX
Message = No stacks / No stacks to display
AWS Amplify = You can delete the backend environment:
wildrydes
That’s it! All done! Congratulations! Hope it was fun.