How to add triggers for a AWS Lambda function created using a CloudFormation template?
Asked Answered
G

7

8

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?

Gaelan answered 5/12, 2017 at 15:8 Comment(0)
H
5

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 :

Higgs answered 7/2, 2018 at 13:18 Comment(1)
what is meant by field id?Servo
T
5

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

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html

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

Typesetting answered 4/1, 2020 at 16:5 Comment(1)
Didn't answer the question. The other person is asking how to do this in cloud formation, and despite that being possible you suggested they do it via SAM instead. While that might be a good recommendation it would have been better to first answer the question, and then offer SAM as an alternative deployment method.Mennonite
C
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.

Cung answered 29/6, 2020 at 9:24 Comment(1)
Always nice to add the example for when the link get's deleted :)Faddish
B
1

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.

Bombard answered 4/5, 2021 at 11:5 Comment(0)
B
0

You need to add a NotificationConfiguration to the S3 bucket definition. However, this will lead to a circular dependency where the S3 bucket refers to the Lambda function and the Lambda function refers to the S3 bucket.

To avoid this circular dependency, create all resources (including the S3 bucket and the Lambda function) without specifying the notification configuration. Then, after you have created your stack, update the template with a notification configuration and then update the stack.

Bream answered 5/12, 2017 at 17:3 Comment(1)
or can use DependsOn or add a wait time.Nunnally
P
0

Here is a SAM based YAML example for CloudWatch log group trigger

    lambdaFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri:
          Bucket: someBucket
          Key: someKey
        Description: someDescription
        Handler: function.lambda_handler
        MemorySize:
          Ref: MemorySize
        Runtime: python3.7
        Role: !GetAtt 'iamRole.Arn'
        Timeout:
          Ref: Timeout
        Events:
          NRSubscription0:
            Type: CloudWatchLogs
            Properties:
              LogGroupName: 'someLogGroupName'
              FilterPattern: "" #Match everything

For S3 example event see https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-s3.html

Pigmy answered 25/2, 2022 at 3:33 Comment(0)
C
0

Probably a bit late, but as I stumbled over this entry on Stack Overflow while I was searching for a solution to this problem, I thought I post what the latest best practice using CloudFormation looks like. You can now use AWS::Lambda::EventSourceMapping to create f.e. a SQS to Lambda trigger, and it will then show up in the console like you would have created the trigger there manually. No need for SAM, EventBridge, etc.

Besides SQS you can create triggers for DynamoDB, MSK, Kinesis, DocumentDB and self managed Kafka, using this approach.

Link to the documentation: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html

Example:

SQSLambdaTrigger:
  Type: AWS::Lambda::EventSourceMapping
  Properties:
    EventSourceArn: !GetAtt YourSqsQueue.Arn
    FunctionName: !Ref YourLambdaFunction

Hope this helps

Countrybred answered 12/7 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.