Lambda and SQS

circle-info

file name = index.mjs

export const handler = async (event) => {
    // TODO implement
    const response = {
      statusCode: 200,
      body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
  };
  

circle-info

file name = serverless.yml

service: test

provider:
  name: aws
  runtime: nodejs20.x
  region: ap-south-1
  stage: dev

# Need to attach VPC to acces Private Resource
  vpc:
    securityGroupIds:
      - sg-1234 # SG-Development
    subnetIds:
      - subnet-a # Private Subnet 1 with Internet Gateway
      - subnet-b # Private Subnet 2 with Internet Gateway
      - subnet-c # Private Subnet 3 with Internet Gateway

functions:
  test-worker:
    maximumRetryAttempts: 0
    # maximumEventAge: 60
    handler: index.handler
    runtime: nodejs20.x
    timeout: 60
    memorySize: 256 # set to 256MB instead of 1024MB (the default)
  
    environment:
      ENV_NAME: ENV_VALUE
      SQS_QUEUE: 
        Ref: TestSqsQueue
    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - TestSqsQueue
              - Arn

resources:
  Resources:
    TestSqsQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "Test-sql"
        VisibilityTimeout: 60 #  Set the visibility timeout to 60 seconds
        MessageRetentionPeriod: 86400 # Set the message retention period to 1 day
        RedrivePolicy:
          deadLetterTargetArn: # Specify the ARN of a dead-letter queue if needed
            "Fn::GetAtt": ["TestDeadLetterQueue", "Arn"]
          maxReceiveCount: 5

    AioDeadLetterQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "Test-dead-letter"
        VisibilityTimeout: 60


Prerequisite

Serverless CLI Installation

Install the Serverless framework module via NPM:

Deployment

To deploy your Serverless service, use the following command:

Service Removal

If you need to remove your Serverless service, execute the following command:

Last updated