The term “Serverless” gives us wings in computing, especially when you don’t wanna host a server for simple tasks. When I say tasks, I mean micro tasks that needs to be accomplished or completed for a purpose or an operation.
Earlier I wrote an article on how to stop an RDS using lambda function.
Consider this, if that function executes successfully, how will you know that RDS has been stopped? What if there was an error may be due to invocation exception or that lambda function doesn’t have permission set to touch RDS.
Now you got the real requirement for “Delayed service status checking/monitoring using AWS services viz., AWS Lambda and AWS SQS”.
How do we achieve?
The key feature we are going to use here is “Amazon SQS Delay Queues”. A lambda function runs on short span. And most of the AWS services stops/starts on a several minutes span. To achieve status checking we will add a delayed message to the SQS queue. An another lambda function will be looking on this delayed queue on regular interval of 5 or 10 minutes or even shorter than that.
SQS Delay queue allows message to be hidden for sometime at the time of pushing. So, when the first lambda function starts/stops a service (say it tried to start or stop an RDS service), upon success, it will push a message to the above mentioned delayed queue.
The actual message becomes visible for the second lambda function after the delay time. If the second status monitoring function finds the “In-progress” status, then it will not modify/remove the message. This cycle continues till the service becomes started/stopped depending up on your requirement.
When the service status has been changed, that monitoring lambda function will dequeue that message.
A reference from StackOverFlow
Before you begin, take a look at an answer for a discussion at StackOverFlow page at https://stackoverflow.com/a/47705808.
How to delay a message to AWS SQS?
Delay queues let you postpone the delivery of new messages to a queue for a number of seconds. By default a message will have 0 min delay, that is “no delay” at all. The maximum limit at the time of writing this page is set by AWS as “15 minutes”.
Also note that this delay will be set at queue level. That is, you can’t customize the delay time for individual messages. So, go to SQS service dashboard in the AWS console. Click on “Create simple queue” button to get started.
How to push a message to AWS SQS in Lambda?
You can use lambda to push a message to a SQS queue. To access a SQS queue, your new lambda function needs the following appropriate permissions set using the below roles:
sqs:ReceiveMessage
sqs:DeleteMessage
sqs:GetQueueAttributes
Include sqs package and use the following code block in the first lambda function.
await sqs.sendMessage(message)
.promise()
.then(data=>{
// process data here
})
.catch(err=>{
// process error here
})
Let me know what you think in comment.
How useful was this post?
Click on a star to rate it!
No votes so far! Be the first to rate this post.
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!