Build an API in less than 30 minutes with ChatGPT and AWS Application Composer — Part 2

Accelerating API Development with Serverless Infrastructure and AI-Powered Code Generation

Ivan Dario Bello Gonzalez
Globant
9 min readApr 26, 2023

--

Picture created by Midjourney

Introduction

In the first part of this article, we explored the benefits of using AWS Application Composer and ChatGPT to build a simple API quickly and easily. We used AWS Application Composer to create the infrastructure for the API by visually connecting AWS services on a canvas, and then we used ChatGPT to generate the code for the API. We saw that this approach can save time and resources while still providing high-quality and secure APIs.

In the second part of the article, we will deploy that API using AWS Serverless Application Model (SAM) and test the API using Postman to ensure that it works correctly. When we are finished, we will clean up the infrastructure to avoid incurring unnecessary costs, and provide some concluding remarks about the benefits and limitations of this approach.

By the end of this article, you will have a better understanding of how to leverage the power of AWS services and ChatGPT to create fully functional APIs quickly and efficiently. So, let’s dive into the next section and see how we can deploy the API using SAM!

Deploy the API

After generating the code with ChatGPT, the next step is to deploy the application using AWS SAM, an open-source framework for building and deploying serverless applications on AWS. AWS SAM provides a simplified way of defining AWS resources needed for your application, including a CLI to create, deploy, and manage serverless applications.

To complete this task, locate the working directory that was created before (aws_composer_api), and look for the src folder. Inside this folder, update the application code in the directories corresponding to each method:

This shows the package of files generated and organized by the AWS Application Composer.

This directory structure was created by AWS Application Composer. To proceed with the deployment, copy and paste the code generated by ChatGPT into thehandler.pyfile, in the DELETE, GET, POSTand PUT directories, as shown in the image above. This task can be done using an IDE or text editor.

Now, from a terminal, run the command sam buildin the aws_composer_api directory This command does a local build of the AWS SAM application and its dependencies in a folder called .aws-sam/build. This command also downloads the required dependencies, generates deployment packages, and prepares them for deployment:

sam build

After the build finishes, execute the command sam deploy — guided which will guide us through the process of deploying our AWS SAM application. This command will ask us to input all the necessary information such as AWS region, stack name, and CloudFormation capabilities:

sam deploy - guided
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Not found
Setting default arguments for 'sam deploy'
=========================================
Stack Name [sam-app]:
AWS Region [us-east-1]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: y
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: y
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]: y
Post may not have authorization defined, Is this okay? [y/N]: y
Get may not have authorization defined, Is this okay? [y/N]: y
Put may not have authorization defined, Is this okay? [y/N]: y
Delete may not have authorization defined, Is this okay? [y/N]: y
Save arguments to configuration file [Y/n]: y
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:

We will accept the default configuration of the stack and respond affirmatively to the prompted questions:

The image shows the output of the SAM deployment—guided command, which shows the resources that will be created.

At the end, agree to deploy the changesets, taking note that AWS Application Composer created additional permissions that enable the interaction with other AWS resources, such as AWS CloudWatch:

Deploy this changeset? [y/N]: y

After the deployment process is completed, a success message will be displayed as follows:

Successfully created/updated stack - sam-app in us-east-1

Now that we have built the API with ChatGPT and AWS Application Composer, we can visualize the AWS resources for the application using the AWS Lambda application, as shown in the image below. The Resource Management Console can be accessed by clicking on the Resources section. Moreover, in the Monitoring tab, we can see the deployment history and the application logs, allowing us to have detailed information about its performance. The logs for each Lambda can be conveniently accessed from the monitoring tab:

The application deployed in AWS is displayed in the AWS Lambda application menu, in the monitoring tab you can see the logs for the resources.

Congratulations 🎉 on building an API using ChatGPT and AWS Application Composer! We have explored the power of AWS Application Composer and how it can be used to develop serverless applications quickly and efficiently. We have also seen how ChatGPT can be used to generate code for building APIs with ease, the serverless pattern possibilities are virtually limitless.

When using AWS Application Composer, it is useful to examine the underlying configurations it made, including the creation of the required roles to invoke the Lambda functions, the API Gateway deployment and the logging configuration to be able to trace the Lambda functions execution in CloudWatch. This information can be viewed in CloudFormation Designer, as shown in the image below:

The template visualization created by AWS Application Composer opened by AWS CloudFormation, designer is shown.

In this image, you can see all the resources that the AWS Application Composer has configured. There were a total of 20 resources deployed using the SAM.

By examining the roles created by the AWS Application Composer, developers can gain insight into the permissions required for their applications to work properly. The CloudFormation Designer provides a visual representation of the resources deployed by the application, which can help developers understand the relationships between different components.

Testing the API

Now we will test each method using Postman, a popular tool for API testing. To be able to start, first, copy the endpoint obtained from the AWS Lambda application in the overview tab, API endpoint section:

The application deployed in AWS is displayed in the AWS Lambda application menu

Testing the POST method

To test the POST method, we can use ChatGPT to generate a JSON payload with real values by requesting it to:

share the JSON payload to test the POST function with real values

This is the example JSON payload produced by ChatGPT:

{
"name": "John Smith",
"balance": 1500.50,
"account_type": "savings",
"interest_rate": 2.5
}

Once we have the endpoint and the payload, open Postman and create a new request. In the request, paste the endpoint in the URL field, select the POST method from the dropdown menu, and then paste the generated JSON payload in the Body tab and click on the Send button to execute the request and see the response:

The image shows the result of using the POST method in Postman

To further validate the item creation process, we can go to the AWS Management Console and select DynamoDB from the list of services. From there, locate the table created for our API and select the Explore items option. This will allow us to inspect the data in the table, including the item that was recently created through our API endpoint. By performing this additional step, ensure that the data was persisted correctly in the database and that our API is fully functional:

The item created by the application is displayed in the AWS console in the DynamoDB service

Testing the GET method

To test the GET method, first open a new tab in Postman and select GET from the dropdown menu. Then, add the API Gateway URL to the text box and include a parameter in the key column with the name id with the value of the ID generated by the API in the POST response. Once these parameters are set up, click on the Send button and receive back the attributes for that particular ID. This enables us to retrieve specific data from the API and test that it is functioning correctly:

The image shows the result of using the GET method in Postman

Testing the PUT method

To test the PUT method, open a new tab in Postman by clicking on the plus sign in the top bar. From the dropdown menu, select PUT, and then add the API Gateway URL in the text box. Similar to the GET method, add a parameter with a key named id with the value of the ID of the item to update. After that, go to the Body tab and add a JSON payload with the items to update. To verify that the update was successful, check the table and make sure that the item was updated with the new values:

The image shows the result of using the PUT method in Postman

Testing the DELETE method

Finally, to test the DELETE method, open a new tab in Postman by clicking on the plus sign in the top bar. Next, select DELETE from the dropdown menu and enter the API Gateway URL in the text box. In the Params tab, add a new parameter with a key named id with the value of the ID of the item to delete.

After that, click on the Send button to send the request to the API endpoint. This will result in the deletion of the item from DynamoDB:

The image shows the result of using the DELETE method in Postman

We have conducted successful testing of the API endpoints using Postman and confirmed that they are functioning as expected. Furthermore, we leveraged ChatGPT to generate the JSON payloads for the testing process.

However, it is crucial to clean up the underlying infrastructure to avoid unnecessary costs and ensure security.

Deleting the API infrastructure

To delete the infrastructure created during this demo, locate the working directory aws_composer_api that was created before. Inside that directory, execute the command:

sam delete

This command will delete the stack sam-app in the us-east-1 region and the S3 artifacts. When prompted with a question asking if we want to delete the stack and artifacts, answer yes to proceed with the deletion: Upon successful deletion, you will receive a message saying Deleted successfully, this is a useful tool for removing AWS resources that were created during the deployment of an application using AWS SAM.

sam delete
Are you sure you want to delete the stack sam-app in the region us-east-1 ? [y/N]: y
Are you sure you want to delete the folder sam-app in S3 which contains the artifacts? [y/N]: y
- Deleting S3 object with key sam-app/e31fd81239a9642a47f6d7e951e9d9c0
- Deleting S3 object with key sam-app/689aa356235fa9d987d636f79c8bc146
- Deleting S3 object with key sam-app/da606319d78aeb815d35f28a2965f472
- Deleting S3 object with key sam-app/c5603324138f5264e59e588b64f94285
- Deleting S3 object with key sam-app/215bd341037f789ee7cc6fd3770b5ba2.template
- Deleting S3 object with key sam-app/6258bb70c45e19b234ec25d256e440ba
- Deleting S3 object with key sam-app/7372763f1bc6a0b28c351cc60a42ffcd.template
- Deleting Cloudformation stack sam-app
Deleted successfully

The successful fusion of AWS Application Composer and ChatGPT opens new doors in the realm of API development, shedding light on a more efficient and streamlined way of building software applications. Both novice and experienced developers stand to benefit from this powerful duo, accelerating their development processes and encouraging a focus on critical aspects such as code optimization and robust security measures.

By incorporating the advantages offered by AI-driven technologies, software development teams can better adapt to the ever-changing needs of the industry, fostering an environment where the possibilities for innovation are virtually limitless. Forward-looking organizations will recognize the potential of embracing these cutting-edge tools to strengthen their development practices and stay ahead of the curve in an increasingly competitive market.

References

Acknowledgments

I would like to take a moment to express my gratitude to the editors who have helped improve the quality and coherence of this article. Thanks for your time.

--

--

Ivan Dario Bello Gonzalez
Globant

Cloud Engineer, Sr | AWS x 3 | GCP x 1 | HashiCorp x 1 | Master's student in Information and Communication Sciences - last semester.