top of page
Search

Deploy and Update an AWS Lambda Function Using AWS SAM CLI – A Complete Guide

  • Writer: Mohammed  Juyel Haque
    Mohammed Juyel Haque
  • Jun 26
  • 3 min read

🔰 What is AWS Lambda?

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You just write the function, and AWS handles the infrastructure, scaling, and execution.

💡 Why Lambda?

  • No server management

  • Auto-scaling

  • High availability

  • Pay-per-use pricing

  • Tight integration with AWS services (API Gateway, S3, DynamoDB, SNS, etc.)


🛠️ Why Use AWS SAM CLI?

The AWS Serverless Application Model (SAM) CLI simplifies the entire lifecycle of serverless applications:

  • Initialize projects

  • Build and test locally

  • Deploy and update in the cloud

It uses CloudFormation under the hood and allows local simulation using Docker.

📦 Prerequisites

Before you begin, ensure:

⚠️ Important: Docker must be installed and running before using sam local invoke or sam local start-api.


✅ Step 1: Initialize a New SAM Project

sam init

Choose

1 - AWS Quick Start Templates
2 - Custom Template Location

Creates

✅ Step 2: Explore the Project


Edit hello_world/app.py:

import json
def lambda_handler(event, context):
   
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "Hello World",
        }),
    }

Template snippet in template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  hello-world-lambda

  Sample SAM Template for hello-world-lambda

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.13
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

✅ Step 3: Build the Project

       sam build

This compiles your Lambda and prepares deployment artifacts.


✅ Step 4: Test the Lambda Locally

You can verify Docker is running by:

dokcer info

🔹 Invoke the Lambda manually:

        sam local invoke HelloWorldFunction

🔹 Run the API locally:

      sam local start-api

Hit the api  http://127.0.0.1:3000/hello into postman.


✅ Step 5: Deploy to AWS

First-time deployment:

sam deploy --guided

✅ Step 6: Update the Lambda Code

Change app.py:

import json
def lambda_handler(event, context):
   
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "Hello Juyel",
        }),
    }

Then rebuild and redeploy:

sam build
sam deploy

✅ Step 7: Clean Up Resources

sam delete

🧰 Quick Command Summary

Command

Purpose

sam init

Create a new SAM project

sam build

Compile and prepare for deploy

sam local invoke

Run Lambda locally (Docker required)

sam local start-api

Emulate API Gateway locally

sam deploy --guided

First-time deployment with setup

sam deploy

Fast deploy after configuration saved

sam delete

Remove all AWS resources

🎯 Conclusion

With AWS Lambda and SAM CLI, deploying serverless applications becomes fast and reliable. By leveraging Docker for local testing, and CloudFormation for repeatable deployments, you ensure your development process is both scalable and production-ready.


 
 
 

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加*

© 2024 Mohammed Juyel Haque. All rights reserved.

bottom of page