AWS - SAM Serverless Function Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we will see how to define the sam serverless function in AWS sam or cloud formation template.
Make sure you have already installed SAM.
A simple serverless function goes like the following.
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.
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.
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.
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.
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
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.
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.
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.
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
AWS serverless function
AWS SAM Lambda example
Install AWS CLI on windows
Happy Learning 🙂

Python – AWS SAM Lambda Example
[Fixed] – Error: No changes to deploy. Stack is up to date
[Fixed] – Malformed Lambda proxy response –  status 502
Javascript – How to listObjects from AWS S3
How to install AWS CLI on Windows 10
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
Java AWS – How to read messages from SQS
How to connect AWS EC2 Instance using PuTTY
Java AWS – How to Create SQS Standard and FIFO Queues
Java AWS – How to Send Messages to SQS Queues
What are Python default function parameters ?
How add files to S3 Bucket using Shell Script
Different ways to use Lambdas in Python
Python raw_input read input from keyboard

Python – AWS SAM Lambda Example
[Fixed] – Error: No changes to deploy. Stack is up to date
[Fixed] – Malformed Lambda proxy response –  status 502
Javascript – How to listObjects from AWS S3
How to install AWS CLI on Windows 10
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
Java AWS – How to read messages from SQS
How to connect AWS EC2 Instance using PuTTY
Java AWS – How to Create SQS Standard and FIFO Queues
Java AWS – How to Send Messages to SQS Queues
What are Python default function parameters ?
How add files to S3 Bucket using Shell Script
Different ways to use Lambdas in Python
Python raw_input read input from keyboard
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows