I tried creating and deploying an AWS Lambda Functions using SAM CLI

I tried creating and deploying an AWS Lambda Functions using SAM CLI

Clock Icon2024.09.27

Introduction

Hello, I’m Hemanth from the Alliance Department. In this blog, I will guide you through the process of building an AWS Lambda service using the AWS Serverless Application Model (SAM) CLI. This process is essential for developers looking to build, test, and deploy Lambda functions using SAM, which simplifies the entire development lifecycle for serverless applications.

AWS

Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking, Database, Management Tools, and Security.

AWS Lambda

A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.

AWS SAM

The AWS Serverless Application Model (SAM) is an open-source framework that simplifies the process of building and deploying serverless applications on AWS. SAM provides shorthand syntax to define AWS resources, making it easier to develop applications that use Lambda, API Gateway, DynamoDB, and more.

Demo

First, install the AWS Toolkit extension in Visual Studio Code. This tool simplifies AWS development and allows you to interact with AWS services directly from the IDE.
Screenshot 2024-09-27 at 15.53.57
After installation, connect to AWS by selecting your AWS profile in the left-hand panel of VS Code. You'll be able to view and manage AWS services directly from the editor.
Screenshot 2024-09-27 at 15.57.31
To create a new SAM project, open the command palette by pressing Cmd + Shift + P or Ctrl + Shift + P on Windows, and search for "AWS: Create Lambda SAM Application."
Screenshot 2024-09-27 at 15.58.54
For this demo, we will select the following settings:
Runtime - nodejs18.x, architecture - x86_64 Then, select the AWS SAM Hello World template, which provides a basic Lambda project structure.
Screenshot 2024-09-27 at 16.01.41
First sample hello-world project has been successfully created.
Screenshot 2024-09-27 at 16.07.37
Once your project is created, run the build command to generate the build artifacts. This step prepares your code and dependencies for deployment.
Screenshot 2024-09-27 at 16.33.59
Next, deploy your Lambda function using the SAM CLI command sam deploy. This command uploads your build artifacts to AWS and creates the necessary resources such as the Lambda function, API Gateway, and IAM roles.
Screenshot 2024-09-27 at 16.40.08
Screenshot 2024-09-27 at 16.40.33
After deployment, navigate to the AWS Management Console to verify that your Lambda function has been successfully created.
Screenshot 2024-09-27 at 16.42.31
Screenshot 2024-09-27 at 16.42.58
Finally, check the API Gateway to ensure it has been properly linked to your Lambda function.
Screenshot 2024-09-27 at 16.43.57
Once the setup is complete, you have a fully functioning serverless service built using AWS Lambda and SAM CLI.

Cleaning up the resources
sam delete --stack-name sam-lambda

Conclusion

By following this guide, you have successfully built, tested, and deployed an AWS Lambda service using AWS SAM CLI. This approach simplifies the development and deployment of serverless applications while providing a robust, scalable, and efficient infrastructure. With SAM's local testing capabilities and seamless integration with AWS services, it's a powerful tool for developers looking to build serverless solutions at scale.

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.