Serverless application
As part of Cloud Mastery Bootcamp, we are going to create a completely serverless application today. An Amazon API Gateway is connected to a static webpage hosted on Amazon S3 by the application. Shop owners can manually enter product purchase data into the DynamoDB Table via the website.
The AWS Lambda function and API are triggered when data is input into the webform running on S3, causing the data to be placed as a message in a SQS queue.
The message in the queue is subsequently processed by a different Lambda function, which also adds the data to the DynamoDB table.
The solution architecture is seen in the following image:
Create a table in DynamoDB.
The DynamoDB table that will house the front-end transactions will first be created.
1. Select “Create Table” from the DynamoDB console.
2. Select the “ProductPurchases” table and input the partition key, “ProductPurchaseKey.”
3. After swiping down, select “Create Table.”
Create the SQS Queue.
To store the messages from the front end, we must first establish the SQS Queue.
1. Go to the SQS console and select Create Queue first.
2. Name the queue “ProductPurchasesDataQueue” and leave it as a Standard Queue.
3. Scroll down and click create
Create the IAM Role
1. Select the Create policy option from the IAM console.
2. Locate and copy the contents of “lambda-policy-for-sqs}ddb.json” under the JSON tab
3. Call the permissions policy “pushPurchasesToQueue” and then click “Create Policy.”
4. Next, this policy needs to be linked to a role. In the IAM console, choose Roles, then click “Create Role.”
5. Choose Lambda under “Use Case” and press the next button.
6. Click next after attaching the newly generated policy, “pushPurchasesToQueue,” to filter the permissions policy.
7. Next, we will select “Create Role” and give the role the name “pushPurchasesToQueue.”
Create the initial Lambda Function.
The first Lambda function, which processes messages from the queue and inserts the order entries into the DynamoDB table.
1. Select the Create Function option from the Lambda console.
2. We will set the runtime to “Node.js 16.x” and call the Lambda function “productPurchasesDataHandler.”
3. Select “Change Default Execution Role,” then “Use and Existing Role,” making use of the “pushPurchasesToQueue” that we previously chose.
4. Select “Create Function”
5. The code can now be added. Choose “Upload from” under “Code” and pick the zip file.
6. Click on save after uploading the DCTProductPurchasesTracking.zip file which contains an index file and a jason package.
Set up the SQS Queue.
The Lambda function we just created will be triggered by the SQS Queue, which we will first configure.
1. Go SQS once more and select the “ProductPurchasesDataQueue” queue.
2. Select “Configure a Lambda Trigger” and “Lambda Triggers.”
Create the Balancer
The load balancer that will be positioned in front of the instances will then be constructed.
1. Scroll down the page, select “Create Load Balancer,” and then click on the load balancer icon on the left.
2. Choose the Network Load Balancer and give “ServiceProviderNLB” to the NLB.
3. Verify that the marking reads “internal.”
4. Choose the Service Provider VPC by scrolling down and checking the boxes for us-east-1a and us-east-1b. Be careful to choose the PRIVATE subnets for each AZ.
3. Click “Save” after selecting the lambda function that we just built.
Run the test code
To verify that messages sent to the queue are correctly processed by Lambda and added to the DynamoDB table, we will use aws cloudhshell
1. Launch AWS CloudShell, upload the downloaded zip file, and unzip it
2. After unzipping the files, change directory to the build-a-serverless-app\Part-1 folder, which contains the “message}body-1.json” file.
3. Adjust the command’s queue URL in the instructions.md file. Run the command after that.
**THE-QUEUE-URL** aws sqs send-message –queue-url — file://message-body-1.json
for message-body
4. Run the same command 4 times with changing the file number each time
5. We should be able to the item in the Dynamo table
Create the IAM Role
We should next proceed to generate the second Lambda execution role.
1. Select the Create policy option from the IAM console.
2. Fill up the JSON box with the contents of the “lambda-policy.json” file (found in the Part-2 folder), being sure to change your account number.
3. After calling the permissions policy “productPurchasesSendMessage,” click “Create Policy.”
4. Next, this policy needs to be linked to a role. In the IAM console, choose Roles, then click “Create Role.”
5. Choose Lambda under “Use Case” and press the next button.
6. Click next after attaching the newly generated policy, “productPurchasesSendMessage,” to filter the permissions policy.
7. After that, we will click “Create Role” and name the role “productPurchasesSendMessage.”
Create the second Lambda function
This will be our second Lambda function creation. Order data will be gathered from the frontend API by this function, which will then queue up the data for further processing.
1. Open the Part}2/DCTProductPurchaseForm\backend folder with AWS CloudShell, then change the index.js file there.
2. Save the file after updating the index.js queue URL with your SQS queue URL.
3. Use the command “zip -r backend.zip *” to zip the files.
4. Choose the node.js 16.x runtime when creating an AWS Lambda function with the name “productPurchasesSendDataToQueue.”
5. For the Lambda execution role, pick the “productPurchasesSendMessage” that we previously built.
6. select “Create Function.”
7. Download and upload the backend.zip file to Lambda
Create the REST API
Now, let’s build the REST API.
1. Open the API Gateway console and select the REST API Build option.
2. Choose “new api” and give it the name “productPurchase.” then select Create API.
3. Select “create resource” from the actions menu, name the resource “productpurchase,” and make sure the resource path is /productpurchase.
4. Select “Create Resource” after turning on Cross Origin Resource Sharing (CORS).
5. To create the PUT method, click “Create Method” under “Actions,” choose “PUT” from the dropdown menu, and then check the box.
6. Choose the Lambda function “productPurchasesSendDataToQueue” and check the box to activate the Lambda Proxy integration. Save and then select OK.
7. Click “Deploy API” under “Actions,” select “New Stage,” and give the stage the name “dev.” Select “Deploy.”
8. Click “Generate SDK” after choosing the “JavaScript” platform under “SDK Generation.”
9. Use CloudShell to upload the zip file.
10. To copy the SDK files to the frontend directory, execute the commands found in the instructions.md file.
Create the S3 Bucket
The S3 bucket that will house the static website’s code will now be created.
1. Go to the S3 console and select “Create Bucket” first.
2. To ensure that the bucket is globally unique, name it “product-purchases-webform-12345,”
3. Select “Create Bucket” after disabling and acknowledging that all public access has been blocked.
4. Select the properties tab after the bucket has been created, enable static website hosting, and enter “index.html” for the index document.
5. Add the permissions statements from the “frontend-bucket-policy.json” file to the Permissions section.
6. Next, we’ll use CloudShell to upload the frontend code.
7. Open your command prompt or terminal and go to the “DCTProductPurchaseForm” directory.
8. Use the command to copy the files to S3 from the frontend directory (with your bucket name given).
s3/product-purchases-webform-12345; aws s3 sync./
9. The website’s code should be present if we look at the S3 bucket
10. Copy the link found in static website hosting and paste it in a new tab
11. Enter the necessary information
12. It should show in the Dynamo table