I am developing an application with Amazon AWS and what I am trying to achieve is to attach a lambda function to DynamoDB table, so after a new row is added ,the lambda function is triggered.
In the above mentioned lambda function I want to add the functionality to send a push notification with Amazon SNS service, but I could not find any information in their documentation if that is possible and if it is, what exactly needs to be done to get it working? What I found in their documentation is that you can attach lambda function trigger to a SNS topic, which means that after a notification is pushed then the function is called, but what I am interested in is to send a push notification directly from lambda function. I would appreciate if someone shed some light on this topic for me.
Yes, you can call any of the API functions from Lambda. Perhaps if you posted some code and the errors you are getting you could get more specific help.
Related
everyone.
I'm interested in get the content of SQS Message using Lambda. Let me explain my infrastructure: I have an EC2 instance that has a script like that bellow. So, it script will send to SQS the message containing instance ID.
#!/bin/bash
INSTANCE_ID=$(curl http://*.*.*.*/latest/meta-data/instance-id)
REGION=$(curl http://*.*.*.*/latest/meta-data/placement/availability-zone | sed '$s/.$//')
QUEUE-URL=$(...)
aws sqs send-message --queue-url "${QUEUE-URL}" --message-body "${INSTANCE_ID}" --region "${REGION}"
The ideia is: when the SQS recieve the message, I would like to trigger a Lambda Function to modificate this instance. But, for that, I need the instance ID. I have searching a lot and unfortunately, I couldn't understand very well how I could get the instance ID, using AWS Lambda, from the SQS Message mentioned above.
I've been trying to solve this problem, but as I don't understand Lambda so much, I searched for many solutions and tested then. Unfortunately, I had no success. So, I interested to learn more about this service.
If someone's could help me with that, I'd be very greateful.
The AWS Lambda function can be configured with the Amazon SQS queue as a 'trigger'.
When a message is sent to the SQS queue, the Lambda function will be invoked. The message both will be available in the Lambda function body.
The code would look something like:
def lambda_handler(event, context):
for record in event['Records']:
body = record['body']
print("From SQS: " + body)
It is possible that multiple messages are passed to the Lambda function, so it first loops through each Record, then extracts the passed-in information in the body parameter.
The print() will show the contents of the message body in CloudWatch Logs. Check to make sure it contains what you expect. Then, add code that uses that value.
There is no need for your Lambda function to specifically call SQS -- this is handled automatically by the AWS Lambda service, which will then delete the message from the queue after your Lambda function successfully completes.
#John's answer Is all new you need.
Since you are new to AWS. I would tell you answer in a way that will help you to debug for future too.
You need to first make sure sns topic target is configured as trigger.
In your lambda function, there is parameter irrespective of any language event. This parameter contains the information about the event which triggered the lambda
Inside the event parameter there is not all the information, like source, event information, in this information you will get Your instance id.
Just try logging event -> then records and you will get your instance id.
Then you can play with your Instance ID
Tip : In the console there is lambda test event where you can generate a sample event form different aws services, sns is also there. You can visualise the sample event too before testing with real event form sns .
Docs for reference - https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html#gettingstarted-concepts-event
As per my existing solution I have two lambda function which gets triggered by the different SQS message and create a folder structure in S3.
Now, I have the requirement where I need to use the single SQS message to trigger both the lambda function.
Is it possible to trigger multiple lambda function via a single SQS message if yes then can you please explain the process and how efficient it would be?
If is there any other approach I can follow please let me know.
Thanks!
No, you can't do that. The best way is to create fan out setup with SNS + two SQS queues.
Otherwise, you have to develop other solution, e.g. one lambda gets triggered by sqs, and then invokes the second one passing the message as input.
I'm having 2 lambda functions in python. I want the first lambda function to publish a message to SNS topic after executing and this SNS topic after publishing the message should trigger the second lambda function. How can i do this, please help me out as I'm new to amazon-web-services
Yes for sure you can do this, browse to Lambda trigger in top left corner of the Lambda function screen. Here you will all trigger options.
I have to monitor a website if it works - response looks like following:
{"response":
{"time": 1457564305},
"stat": "OK"
}
And need sending mail alert to me if stat is not OK.
Seems this can be done with Lambda using node.js and CloudWatch.
I tried to create Lambda function and execute through CloudWatch but only see the function was triggered. Seems I need to push some metrics?
I'm newbie to node.js and Lambda (handler, metrics, etc.).
Would anyone share some hints on where I should start with?
Thanks a lot.
Not sure what you mean by "only see the function was triggered".
Yes, you can cause a Lambda function to be invoked on a schedule. That Lambda function could issue a request to the website and trigger a notification to an SNS topic if it believes the site to be down. You would subscribe (by email address or SMS) to the SNS topic for notifications. Here's an example of how to do this.
Quick question: Is it possible to trigger the execution of a Step Function after an SQS message was sent?, if so, how would you specify it into the cloudformation yaml file?
Thanks in advance.
The first think to consider is this: do you really need to use SQS to start a Step Functions state machine? Can you use API gateway instead? Or could you write your messages to a S3 bucket and use the CloudWatch events to start a state machine?
If you must use SQS, then you will need to have a lambda function to act as a proxy. You will need to set up the queue as a lambda trigger, and you will need to write a lambda that can parse the SQS message and make the appropriate call to the Step Functions StartExecution API.
I’m on mobile, so I can’t type up the yaml right now, but if you need it, I can try to update with it later. For now, here is detailed walkthrough of how to invoke a Step Functions state machine from Lambda (including example yaml), and here is walkthrough of how to use CloudFormation to set up SQS to trigger a Lambda.
EventBridge Pipes (launched at re:Invent 2022) allows you to trigger Step Functions State Machines without need for a Lambda function.
https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes.html
You can find an example here:
https://github.com/aws-samples/aws-stepfunctions-examples/blob/main/sam/demo-trigger-stepfunctions-from-sqs/template.yaml