Here we will see how to define the sam serverless function in AWS sam or cloud formation template.

SAM Serverless Function:

Make sure you have already installed SAM.

A simple serverless function goes like the following.

template.yaml
Resources:  
  ServerlessFucntion:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: sample-serverless-example
      Description: "A simple serverless example in SAM"
      CodeUri: .serverless_example
      Handler: src.lambda_handler

The above AWS::Serverless::Function resource creates a serverless function.

Here the CodeUri is a location where the function code resides. Handler is the actual function with the code that triggers when the lambda is being accessed.

Add events to the Function:

Events are used to trigger the lambda. If we attach an event to the lambda whenever the event gets triggered the will be executed.

For example, if I would like to attach a cloud watch schedule that triggers the lambda every 60 secs.

template.yaml
Resources: 
  ServerlessFucntion: 
    Type: AWS::Serverless::Function 
    Properties: 
       FunctionName: sample-serverless-example 
       Description: "A simple serverless example in SAM" 
       CodeUri: .serverless_example 
       Handler: src.lambda_handler
       Events:
        ScheduleEvent:
          Type: Schedule
          Properties:
            Schedule: rate(60 minutes)
            Name: "sample-scheduler"
            Description: "event to trigger lambda for every 60 minutes"
            Enabled: false

An AWS lambda supports the following events to be attached.

  • S3
  • SNS
  • Kinesis
  • DynamoDB
  • SQS
  • Api
  • Schedule
  • CloudWatchEvent
  • EventBridgeRule
  • CloudWatchLogs
  • IoTRule
  • AlexaSkill
  • Cognito
  • HttpApi
  • MSK
  • MQ

Every event has its own defined set of properties and it needs to be configured as per the requirement.

Add Policies to the Function:

We can attach one or more policies to a lambda function. For example, if I want to attach DynamoDBFullAccess and APIGatewayInvokeFullAccess that goes the following.

template.yaml
Resources: 
  ServerlessFucntion: 
    Type: AWS::Serverless::Function 
    Properties: 
       FunctionName: sample-serverless-example 
       Description: "A simple serverless example in SAM" 
       CodeUri: .serverless_example 
       Handler: src.lambda_handler
       Events:
        ScheduleEvent:
          Type: Schedule
          Properties:
            Schedule: rate(60 minutes)
            Name: "sample-scheduler"
            Description: "event to trigger lambda for every 60 minutes"
            Enabled: false
       Policies:
        - AmazonSSMFullAccess
        - AmazonAPIGatewayInvokeFullAccess

Defining the common properties:

increasing the memory, set timeout and environment properties can be considered as common properties that we can set as Globals in the template. The Globals can be applied to all the functions within the template.

template.yaml
Globals:
  Function:
    Timeout: 600
    Runtime: python3.8
    Environment:
      Variables:
        STAGE: DEV

If you would like to bound these properties to a specific function that has to be defined within the function definition itself under the Properties section like the following.

template.yaml
Resources: 
  ServerlessFucntion: 
    Type: AWS::Serverless::Function 
    Properties: 
       FunctionName: sample-serverless-example 
       Description: "A simple serverless example in SAM" 
       CodeUri: .serverless_example 
       Handler: src.lambda_handler
       Runtime: python3.8
       Timeout: 600
       Environment:
          Variables:
             STAGE: DEV

Complete yaml file.

template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

  Globals:
    Function:
      Timeout: 600
      Runtime: python3.8
      Environment:
        Variables:
          STAGE: DEV
Resources: 
  ServerlessFucntion: 
    Type: AWS::Serverless::Function 
    Properties: 
       FunctionName: sample-serverless-example 
       Description: "A simple serverless example in SAM" 
       CodeUri: .serverless_example 
       Handler: src.lambda_handler
       Events:
        ScheduleEvent:
          Type: Schedule
          Properties:
            Schedule: rate(60 minutes)
            Name: "sample-scheduler"
            Description: "event to trigger lambda for every 60 minutes"
            Enabled: false
       Policies:
        - AmazonSSMFullAccess
        - AmazonAPIGatewayInvokeFullAccess

References:

Happy Learning 🙂