3

I've created a Lambda function that I want called every 5 mins, or once a day, or whatever. How do I set that up with troposphere?

I can't find an example anywhere.

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108

1 Answers1

14

For the next person, here's the working code that I finally came up with (in this scenario, I'm trying to run a "report"):

from troposphere import (
    Join, Ref, GetAtt, awslambda, iam, events, Template
)

template = Template()

report_lambda = template.add_resource(awslambda.Function(
    "MyLambda",
    Code=awslambda.Code(
        S3Bucket="my-bucket"
        S3Key="lambdas/my-lambda.jar"
    ),
    Description="Lambda task that runs every 5 minutes.",
    FunctionName="MyFunction",
    Handler="com.mycompany.MyLambda::handleRequest",
    Runtime="java8",
    Timeout=120,
    Role=GetAtt("MyLambdaRole", "Arn"),
    MemorySize=512
))

report_rule = template.add_resource(events.Rule(
    "MyRule",
    ScheduleExpression="rate(5 minutes)",
    Description="My Lambda CloudWatch Event",
    State="ENABLED",
    Targets=[
        events.Target(
            "MyLambdaTarget",
            Arn=GetAtt(report_lambda.title, "Arn"),
            Id="MyLambdaId"
        )
    ]
))

template.add_resource(awslambda.Permission(
    'MyInvokePermission',
    FunctionName=GetAtt(report_lambda.title, 'Arn'),
    Action='lambda:InvokeFunction',
    Principal='events.amazonaws.com',
    SourceArn=GetAtt(report_rule.title, 'Arn'),
))

print(template.to_json())
Nicolas
  • 53
  • 1
  • 2
  • 11
Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108