How to add triggers for a AWS Lambda function created using a CloudFormation template?
Solution 1
You can use cloudwatch rule to trigger your lambda function :
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyCloudWatchRule:
Type: "AWS::Events::Rule"
Properties:
Description: "Rule to trigger lambda"
Name: "MyCloudWatchRule"
EventPattern: <Provide Valid JSON Event pattern>
State: "ENABLED"
Targets:
- Arn: "arn:aws:lambda:us-west-2:12345678:function:MyLambdaFunction"
Id: "1234567-acvd-awse-kllpk-123456789"
Ref :
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#aws-resource-events-rule-syntax
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html
Solution 2
It's been a while so I imagine you've solved the problem, but I'll put in my 2 cents to help others.
It's best to use SAM (Serverless Application Model) for this kind of things. So use AWS::Serverless::Function
instead of AWS::Lambda::Function
In there, you can specify an EventSource
which accepts the following possible values:
- S3
- SNS
- Kinesis
- DynamoDB
- SQS
- Api
- Schedule
- CloudWatchEvent
- CloudWatchLogs
- IoTRule
- AlexaSkill
- Cognito
- HttpApi
SAM does the rest of the work. Follow this guide for the rest of the details: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-deploying.html
Solution 3
Nowadays, this issue is fixed by Amazon: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#aws-resource-events-rule--examples
Just create Lambda permissions like in the example.
Solution 4
Lambda function can be triggered by several AWS resources such as S3, SNS, SQS, API, etc. Checkout for the full list at AWS docs
I suggest you use Altostra Designer, which let you create and configure Lambda Function super quick and also choose what will trigger it.
trans1st0r
Updated on June 04, 2022Comments
-
trans1st0r almost 2 years
I am trying to create a lambda function from a CloudFormation template based on this example:
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-lambda.html
As can be seen from this link:
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html
there is no way to add a trigger for the lambda function (like a S3 upload trigger).
Is there a workaround to specify the trigger while writing the template?
-
Varun Chandak almost 5 yearsor can use
DependsOn
or add a wait time. -
Roelant about 3 yearsAlways nice to add the example for when the link get's deleted :)