top of page
Search

AWS Lambda: With SAM CLI

  • Writer: Mohammed  Juyel Haque
    Mohammed Juyel Haque
  • Oct 22, 2024
  • 3 min read

Updated: Apr 4

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You simply upload your code, and Lambda handles everything required to run and scale your code with high availability. You can run code for virtually any type of application or backend service with zero administration.

Lambda supports multiple programming languages, including Python, Node.js, Java, and C#. In this post, we’ll explore the basics of AWS Lambda and provide example code, along with deployment steps using the AWS SAM CLI.



Key Features of AWS Lambda

  • Event-driven: AWS Lambda runs your code in response to events from various AWS services, such as S3, DynamoDB, Kinesis, or API Gateway.

  • Automatic scaling: AWS Lambda automatically scales your application by running code in response to each trigger.

  • Pay-as-you-go: You only pay for the compute time you consume, with no charge when your code is not running.

  • Flexible: Supports a variety of programming languages and runtime environments.

Getting Started with AWS Lambda Using SAM CLI

AWS Serverless Application Model (SAM) is a framework for building serverless applications on AWS. SAM provides you with a command-line interface (CLI) for managing Lambda functions, along with other AWS resources.

Step 1: Install AWS SAM CLI

To get started, ensure you have the AWS SAM CLI installed. You can follow the installation guide.

Step 2: Create a New SAM Application

Use the following command to create a new SAM application:

sam init

You'll be prompted to choose a template. Select the following options:

  • 1 - AWS Quick Start Templates

  • 1 - Python 3.x

  • MyHelloWorldApp (or any name you prefer)

  • Leave other options as default.

This command will create a new directory with the specified name containing a basic SAM application structure.

Step 3: Modify the Lambda Function

Navigate to the newly created directory

cd JuyelLambda

Open the hello_world/hello_world.py file and modify the code to return a "Hello, World!" message:

def lambda_handler(event, context):
 return {

        'statusCode': 200,

        'body': 'Hello, Juyel!'

    }

Step 4: Update the SAM Template

Open the template.yaml file in your project directory. This file defines the AWS resources that your application will use. You can add an API Gateway trigger for your Lambda function as follows:

Resources:
HelloWorldFunction:

    Type: AWS::Serverless::Function

    Properties:

      Handler: hello_world.lambda_handler

      Runtime: python3.x

      Events:

        HelloWorldApi:

          Type: Api

          Properties:

            Path: /hello

            Method: get

Step 5: Build Your SAM Application

Now that your function and template are ready, build your application using the SAM CLI:

sam build

Step 6: Deploy Your SAM Application

To deploy your application, use the following command:

sam deploy --guided

During the guided deployment, you will be prompted to enter some parameters, such as:

  • Stack Name: MyHelloWorldApp

  • AWS Region: Choose your preferred region (e.g., us-east-1).

  • Confirm changes before deploy: Y

  • Allow SAM CLI to create IAM roles: Y

Once you confirm, SAM will package your application, upload it to AWS, and create the necessary resources.

Step 7: Test Your API

After deployment, you’ll receive an API endpoint URL in the output. You can test it using a browser or tools like curl or Postman:

curl https://<api-id>.execute-api.<region>.amazonaws.com/Prod/hello

You should see the following response:

{
 "statusCode": 200,

    "body": "Hello, Juyel!"

}

Conclusion

AWS Lambda, combined with the AWS SAM CLI, offers a powerful and efficient way to build and deploy serverless applications. With SAM, you can manage your application’s lifecycle with simple commands, making it easy to deploy and scale.

In this post, I explored how to create a simple AWS Lambda function, configure it with API Gateway using SAM, and deploy it with the SAM CLI. With these basics, you're ready to dive deeper into serverless application development on AWS!

 
 
 

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación*

© 2024 Mohammed Juyel Haque. All rights reserved.

bottom of page