Setting up a Lambda function to be triggered and send a message to SQS

Using Python & Boto3

Today we're going to be setting up a lambda function to be triggered and send a message to SQS.

In order to complete this task we will need the following:

  • An AWS account with the necessary permissions
  • Boto3 installed
  • (I'll be using cloud 9 for this tutorial)

Step 1: The SQS queue

We're going to be creating our SQS queue with a python script in our cloud 9. https://boto3.amazonaws.com/v1/documentation/api/latest/guide/sqs.html This is the sample tutorial I borrowed the simple code from, it's just missing "import boto3".

Run this code to boot up your SQS queue. You'll see your SQS q ueue URL with the 5 second delay after running this code, (We'll need this later).

Step 2: The Lambda function

Head over to the Lambda dashboard in your AWS console and select "Create function". Keep "author from scratch" as your main option and select a runtime of python 3.7.

With these few settings configured, create your function.

Step 3: Adding the Lambda trigger

From the Lambda dashboard, we'll select "Add trigger" and select "API Gateway". Choose "Create a new API" and "HTTP API", with "Open" security. Then add the trigger.

Step 4: Permissions

Now we have to allow the right permissions, so we'll head to the "Configuration" tab and navigate to "Permissions". Select your role and click on the plus sign next to your role name.

On the right-hand side select "Edit" and "Add additional permissions". Select "SQS" as your service. For your access level select read and write. Now we'll select "Resources" and "Add ARN". We're going to add the ARN of our SQS queue, to find that you can head back to your SQS dashboard and select the one we've made for this project.

Then we'll go ahead and review or changes and save them.

Step 5: Lambda script

We'll make our way back to Lambda and enter a script that invokes a message to our SQS with the current date and time in your zone. The code looks like this:

NOTE: You'll have to change the http Queue URL to match your personal information.

You can get your SQS queue URL from your Cloud9 where we ran our code earlier.

Now your Lambda code should look similar to this:

Step 6: Testing

Time to see if we've done this correctly! Go ahead and hit "Deploy" to save your changes. Then we'll select "Test" to create a test. Give your test a name and keep your "Event sharing" as "Private". Under template select "apigateway-aws-proxy" and save!

Let's hit test, if done right you should get a status code of 200 like so:

Now let's go over to SQS to make sure we received the message. From the SQS dashboard, let's click on our function, and on the top right select "Send and receive messages". At the bottom of this page, we'll see "Poll for messages".

After clicking on "Poll for messages" we'll see a link/name:

And with everything working correctly you will see the current date and time!

I hope you've enjoyed following along with this article and even more so I hope it's been helpful!!

Have a blessed day.


Setting up a Lambda function to be triggered and send a message to SQS was originally published in Towards AWS on Medium, where people are continuing the conversation by highlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more