Serverless Framework: Creating an SQS Queue Example

Posted by Miguel Lopez on Sat 10 June 2023 in serverless

Technical Stack: AWS, Lambda, Serverless Framework, SQS Queue

Read: 10 minutes

Pre-Requisites

To deploy the following Serverless Framework template, you will need the following:

  • An AWS account.
  • AWS CLI installed locally.
  • API credentials for your AWS account configured in your AWS CLI locally by running aws configure.
  • Serverless framework installed locally via npm -g install serverless.

Creating an SQS Queue

Using Serverless Framwework, the following serverless.yml example creates these resources in AWS:

service: derezzed-base-infrastructure
frameworkVersion: '3'

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  runtime: 'python3.9'
  region: 'us-west-2'
  deploymentBucket:
    name: serverless-deployments
    serverSideEncryption: AES256
  tags:
    stage: ${opt:stage, 'dev'}

resources:
  Resources:
    DerezzedMessagesQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: ${opt:stage, self:provider.stage}-derezzed-messages
        DelaySeconds: 0
        MessageRetentionPeriod: 345600 # 4 days
        VisibilityTimeout: 30

  Outputs:
    sqsArn:
      Value: !GetAtt DerezzedMessagesQueue.Arn
    sqsUrl:
      Value: !GetAtt DerezzedMessagesQueue.QueueUrl

Deploying the SQS Queue

Run the following command to deploy the Serverless RDS Cluster:

sls deploy --stage dev

You'll see the following output when creating this cluster. A fresh database deployment can take up to ~5 minutes.

Running "serverless" from node_modules

Deploying to stage dev

      derezzed-base-infrastructure  deployed  10s

Run sls outputs --stage dev to see the outputs of the deployment. You'll see the following:

Running "serverless" from node_modules

derezzed-base-infrastructure:
  sqsUrl: https://sqs.us-west-2.amazonaws.com/xxxxxxx/dev-derezzed-messages
  sqsArn: arn:aws:sqs:us-west-2:xxxxxx:dev-derezzed-messages

Connect your queue to your Lambda Function

IAM access to SQS is automatically granted to your Lambda Function with the events block. You can use the following serverless.yml to connect your SQS queue to your Lambda Function:

functions:
  sample:
    handler: handler.example
    events:
      - sqs:
          arn: !GetAtt DerezzedMessagesQueue.Arn

For a full list of SQS attributes you can use in your serverless.yml, read the Serverless Framework SQS Docs.

Other Serverless Framework Examples

Check out the other Serverless Framework example projects I've created: