Building a Serverless Application with GenAI in AWS

Andrea Giacobbe
Storm Reply
Published in
9 min readMar 27, 2024

Abstract

In this blog post, we propose a serverless application to demonstrate how to use AWS services to build and deploy in a fast, reliable and fully integrated way using a GenAI tool.

In particular, will be used:

  • Amazon CodeCatalyst, which allows for quick delivery of infrastructures offering blueprints for straightforward architectures and integrated CI/CD deployments.
  • Amazon CodeWhisperer, a GenAI service will be used for developing additional AWS components and writing application code to extend the blueprint proposed by Amazon CodeCatalyst.

The next diagram shows the whole architecture that you will create during this demo, which will be developed using Visual Studio Code editor (VS Code).

The next sections provide a brief overview of the services, followed by a demonstration.

What is Amazon CodeCatalyst?

Amazon CodeCatalyst is an Amazon integrated service for development teams seeking efficient application building, delivery, and scalability on AWS while adhering to organizational best practices at scale via blueprints, ensuring consistency and compliance across teams.

In Amazon CodeCatalyst, projects allow developers to cooperate and develop using shared continuous integration/continuous delivery (CI/CD) workflows and repositories.

NB: For the purpose of the demo, the Amazon CodeCatalyst will not be described in detail; however, a comprehensive article providing a deep dive will be published soon.

What is Amazon CodeWhisperer?

CodeWhisperer is an artificial intelligence tool by Amazon based on Large Language Models (LLMs) that analyzes English language comments and generates real-time code suggestions across various development environments, including VS Code. It supports a wide range of programming languages, including Python, Java, JavaScript, YAML and others.

Please note that a recent article has been published detailing the core functionalities of Amazon CodeWhisperer and its setup process with VS Code. As result, we will not be covering the setup of CodeWhisperer during the demo. For additional information, please refer to the following article: https://medium.com/storm-reply/write-less-produce-more-with-codewhisperer-a-review-and-tutorial-5694c14dbe73

Hands-On

During the demonstration, the following AWS resources will be created:

  • AWS Lambda function — a resource that invokes your code without provisioning or maintaining servers.
  • REST Amazon API Gateway — a resource that creates, publishes, maintains, monitors, and secures REST APIs at any scale.
  • IAM role and policy — a mechanism to control the access to AWS resources. In this case, the IAM policy will grant permissions to AWS Lambda to access the DynamoDB table.
  • Amazon DynamoDB table — a serverless NoSQL database service that enables the development of modern applications at any scale.

The AWS Lambda function and the Amazon API Gateway come with the Amazon CodeCatalyst blueprint we will choose during this demo. In addition to this, we want to demonstrate how easy is to create additional AWS resources (DynamoDB, the IAM role and policy) and write a simple code for the Lambda function with Amazon CodeWhisperer auto-suggestions. The whole infrastructure will be deployed with Amazon CodeCatalyst.

In another article will delve into the key features and various components of CodeCatalyst.

Steps:

  1. Setting up the project on Amazon CodeCatalyst
  2. Developing with Amazon CodeWhisperer to build additional AWS infrastructure components and writing the code of AWS Lambda function, that updates items into the DynamoDB table when a POST API is executed
  3. Deploying the application with CodeCatalyst and testing the application
  4. Cleaning up the resources
  5. Conclusion

Setting up the project on Amazon CodeCatalyst

In this section, the Amazon CodeCatalyst project will be created.

Prerequisite: For this demonstration, we assume the initial setup and space configuration for Amazon CodeCatalyst have already been completed using the AWS Builder ID sign-in. To configure Amazon CodeCatalyst and the space, please consult the AWS guide: https://docs.aws.amazon.com/codecatalyst/latest/userguide/setting-up-topnode.html

Ensure that your AWS Account, where you are creating the AWS resources, is associated with the CodeCatalyst space. Create the following AWS IAM roles in your AWS Account to authorize project workflows to access AWS account resources.

Establish an IAM role for building (e.g. test-project-build-role) and another for the deployment (e.g. test-project-deploy-role) with the following trust policy for both roles:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CodeCatalyst",
"Effect": "Allow",
"Principal": {
"Service": [
"codecatalyst-runner.amazonaws.com",
"codecatalyst.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}

For the build IAM role will be associated the following inline policy:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*",
"cloudformation:*"
],
"Resource": "*"
}
]
}

and for the deploy IAM role will be associated with the following inline policy:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"iam:PassRole",
"iam:DeleteRole",
"iam:GetRole",
"iam:TagRole",
"iam:CreateRole",
"iam:AttachRolePolicy",
"iam:DetachRolePolicy",
"cloudformation:*",
"lambda:*",
"apigateway:*",
"dynamodb:*",
"iam:CreatePolicy"
],
"Resource": "*"
}
]
}

After setting up the IAM roles, proceed to add them to your CodeCatalyst space account connection. Refer to this AWS guide on how to associate the IAM roles to the Amazon CodeCatalyst space: https://docs.aws.amazon.com/codecatalyst/latest/userguide/ipa-connect-account-addroles.html

In Amazon CodeCatalyst, there are three ways to create a project:

  • Start with a blueprint starting from a built-in project. A blueprint provides an initial configuration, such as a repository of sample source code, build scripts, deployment actions, and AWS resources therefore to speed up initial project setup and make it easier to adopt best practices.
  • Bring your own code, code existing in your Github repository.
  • Start from scratch, create an empty project.

For this demo, we select Start with a blueprint and search for the blueprint Serverless application model (SAM) API and click Next.

This blueprint is designed to create a project that uses the Serverless Application Model (SAM) to quickly create and deploy an API (includes an API Gateway and AWS Lambda).

Configure the repository as follows and leave the other parameters as default.

Name your project: my-sam-project
AWS account connection: your AWS Account ID
Deploy role: test-project-deploy-role
Build role: test-project-build-role
Runtime Language: Python 3.9
Code Repository name: my-sam-project
CloudFormation stack name: my-sam-project
Lambda function name: SamLambda

Optional: Before proceeding to create the project, you can check out the code and the workflow you are generating.

Finally, click on Create project.

Once the project is created, you will see the Overview page.

With the blueprint comes these main Amazon CodeCatalyst resources:

  • The source Git repository named my-sam-project that contains the following project assets:
    - SamLambda — Source code and other files for the Lambda function, in particular the hello_world folder that contains the code for the AWS Lambda function.
    - template.yaml — the template that defines the application’s AWS resources.
  • The workflow defined in .codecatalyst/workflows/build-and-release.yaml — A workflow is an automated procedure that defines how to build, test and deploy the application.
  • The environment — An abstraction of infrastructure resources that deploy applications. You can use environments to organize deployment actions into a production or non-production environment.

Developing with Amazon CodeWhisperer

Now, it is time to develop exploiting CodeWhisperer in VS Code.

Ensure you have logged in to CodeCatalyst with AWS Builder ID in VS Code, which should appear like this:

In VS Code, under the CODECATALYST toolkit, click on Clone Repository, then in the search bar, it should appear your created CodeCatalyst repository (my-sam-project) and clone it locally somewhere. Finally, open the repository into VS Code.

Also, make sure that Amazon CodeWhisperer is enabled in VS Code. Once activated, you will begin receiving code suggestions as you type. To manually trigger CodeWhisperer, use Option + C (MacOS) or Alt + C (Windows).

Open the file template.yaml that defines the AWS components of this serverless application (AWS Lambda and API Gateway come with the blueprint).

To define the DynamoDB table we can exploit Amazon CodeWhisperer, writing a comment in VS Code as shown below. Amazon CodeWhisperer will suggest you different snipped codes that you can navigate through with the arrows of your keyboard, accept single lines with the keys ⌘ + right arrow (on MacOS) or accept the whole suggestion with the tab key.

Then request CodeWhisperer, as demonstrated below, to define the IAM policy for accessing the DynamoDB table, the IAM role that will employ the IAM policy and be assigned to the Lambda function.

Finally, assign the IAM role to the Lambda resource and update the API Gateway method from GET to POST within the Lambda resource as follows.

In requirements.txt, add boto3 library.

Modify the code of the Lambda function (app.py) with the support of Amazon CodeWhisperer to store the items when executing HTTPS POST requests to API Gateway.

Let’s deploy the serverless application.

Deploying the application

The application deployment will follow the Amazon CodeCatalyst workflow defined in .codecatalyst/workflows/build-and-release.yaml (as described below).

Here are some details about the workflow:

  • Name of workflow: build-and-release
  • Trigger: the workflow is triggered by PUSH actions to the main branch on Git repository.
  • The workflow contains two Actions:
    - build_for_default_environment, the workflow builds the application and stores the build artifacts in an Amazon S3 bucket (the bucket will be created automatically during the build).
    - deploy_to_default_environment, after the build artifact is generated, it is used to deploy the application launching the AWS CloudFormation stack my-sam-project.
    NB: The account connection, associated with the Amazon CodeCatalyst space, and the IAM role to build test-project-build-role and deploy test-project-deploy-role are specified for the actions build_for_default_environment and deploy_to_default_environment, respectively.
  • Other parameters passed to the workflow by default:
    - Environment: default_environment
    -
    Region: us-west-2

To run the workflow from the terminal, navigate to your local Git repository and run the following commands on the main branch:

git add --all 
git commit -m "sam application"
git push

After the git push command, the workflow starts and if everything is successful, it should appear like this:

Two CloudFormation stacks are then generated:

  • my-sam-project, as shown below which contains the AWS resources created.
  • aws-sam-cli-managed-default, for the bucket S3 created used for the build artifacts.

Testing the application

To test that the application is working properly, run the following curl command from your terminal:

curl -X POST -H "Content-Type: application/json" -d '{"id": "1", "name": "John", "age": "30"}' "https://<Physical-ID-RestAPI>.execute-api.us-west-2.amazonaws.com/Prod/SamLambda"

where Physical-ID-RestAPI is generated after the creation of the API Gateway.

You should get a response like this:

{"message": "Event stored successfully!"}

Let’s check if the item is stored in the DynamoDB table.

Cleaning up the resources

At the end of this demo, you can remove the resources created:

  • AWS resources, you can quickly remove the AWS resources created (S3 bucket, AWS Lambda, API Gateway, DynamoDB, IAM role and IAM policy) by simply deleting the CloudFormation stacks (make sure to empty the S3 bucket before deleting it). For the IAM roles used for the CodeCatalyst purpose, you will need to remove them manually from the IAM AWS console.
  • To delete the CodeCatalyst project, you just need to select Delete project under Project settings and finally confirm the deletion.

Conclusion

In conclusion, this demonstration has illustrated the simplicity of creating a serverless application using Amazon CodeWhisperer and deploying it on an AWS Account with Amazon CodeCatalyst. These services facilitate the Software Development Life Cycle (SDLC) by supporting developers in the coding phase and streamlining the deployment phase.

In another article, we will delve into the main functionalities of Amazon CodeCatalyst to show how to maximize the capabilities of this powerful service.

Stay tuned!

--

--