0
0
AwsHow-ToBeginner · 4 min read

How to Deploy AWS Lambda Using SAM CLI Quickly

To deploy a Lambda function using AWS SAM, write a template.yaml defining your function, then run sam build and sam deploy --guided commands. This packages your code and creates the Lambda with required permissions automatically.
📐

Syntax

The deployment process uses these main commands:

  • sam build: Prepares your Lambda code and dependencies.
  • sam deploy --guided: Packages and deploys your Lambda, guiding you through setup options.

The template.yaml file defines your Lambda function, runtime, handler, and permissions.

yaml
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python3.9
      CodeUri: ./src
      Description: My Lambda function
      MemorySize: 128
      Timeout: 10
      Policies: AmazonDynamoDBReadOnlyAccess
💻

Example

This example shows a simple Python Lambda deployment using SAM. It includes a template.yaml and a basic handler.

yaml and python
template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple Lambda deployment with SAM
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python3.9
      CodeUri: ./src
      Description: A simple hello world Lambda
      MemorySize: 128
      Timeout: 5

src/app.py:

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }
Output
Successfully packaged artifacts and wrote output template to file. Waiting for stack create/update to complete... Successfully created/updated stack - HelloWorldFunctionStack
⚠️

Common Pitfalls

Common mistakes when deploying Lambda with SAM include:

  • Not running sam build before deploy, causing missing dependencies.
  • Incorrect Handler or Runtime settings in template.yaml.
  • Forgetting to configure permissions or policies, leading to runtime errors.
  • Not using --guided on first deploy, missing important prompts.

Always validate your template with sam validate before deploying.

yaml
Wrong Handler example:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: wrong.handler
      Runtime: python3.9
      CodeUri: ./src

Correct Handler example:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python3.9
      CodeUri: ./src
📊

Quick Reference

CommandPurpose
sam buildBuilds your Lambda function and dependencies
sam deploy --guidedPackages and deploys Lambda with setup prompts
sam validateChecks your template.yaml for errors
sam logs -n FunctionNameShows logs for your deployed Lambda function

Key Takeaways

Write a valid template.yaml defining your Lambda function before deploying.
Run sam build to prepare your code and dependencies.
Use sam deploy --guided on first deploy to configure deployment settings.
Validate your template with sam validate to catch errors early.
Check Lambda logs with sam logs to debug after deployment.