AWS Lambda通过cloudformation安排事件源

时间:2015-12-18 18:40:15

标签: amazon-web-services amazon-cloudformation aws-lambda

我已经在cloudformation中定义了我的lambda / roles,并且还希望用它来添加预定的事件源......是否有任何文档或示例?

7 个答案:

答案 0 :(得分:30)

Aws::Event::RuleScheduleExpressionAWS::Lambda::Permission

一起使用
// rule to periodically call the lambda
"TagWatcherRule": {
  "Type": "AWS::Events::Rule",
  "Properties": {
    "ScheduleExpression": "rate(10 minutes)",
    "Targets": [
      {
        "Id": "TagWatcherScheduler",
        "Arn": {
          "Fn::GetAtt": [
            "TagWatcherFunction",
            "Arn"
          ]
        }
      }
    ]
  }
},
// role may call the lambda
"InvokeLambdaPermission": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": {
      "Fn::GetAtt": [
        "TagWatcherFunction",
        "Arn"
      ]
    },
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceArn": {
      "Fn::GetAtt": [
        "TagWatcherRule",
        "Arn"
      ]
    }
  }
}

答案 1 :(得分:9)

不幸的是,CloudFormation目前不支持为lambda函数配置预定事件源。您需要使用CloudFormation部署lambda,然后手动配置计划的事件。

CloudFormation支持AWS::Lambda::EventSourceMapping资源类型。但是,此资源仅限于配置Kinesis或DynamoDB流,因此这可能对您没有帮助。

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html

**更新 - 截至2016年4月,现在使用CloudWatch Events支持此功能 - https://aws.amazon.com/about-aws/whats-new/2016/04/amazon-cloudwatch-events-now-supported-in-aws-cloudformation-templates/

答案 2 :(得分:8)

我解决了同样的问题。

"RoleForLambdaStopEC2Instances" : {
  "Type": "AWS::IAM::Role",
  "Properties": {
    "AssumeRolePolicyDocument": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Principal": {
            "Service": "lambda.amazonaws.com"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    },
    "Policies": [
      {
        "PolicyName": "LambdaStopEC2InstancesPolicy",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "ec2:StopInstances"
              ],
              "Resource": [
                "arn:aws:logs:*:*:*",
                "arn:aws:ec2:*"
              ]
            }
          ]
        }
      }
    ],
    "Path": "/"
  }
},
"LambdaStopEC2Instances": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
      "S3Bucket": "XXXXXXXXXXXXXXXXX",
      "S3Key": "XXXXXXXXXXXXXXXXXX"
    },
    "Handler": "stopEC2Instances.handler",
    "Role": { "Fn::GetAtt" : ["RoleForLambdaStopEC2Instances", "Arn"] },
    "Runtime": "nodejs4.3",
    "Timeout": "5"
  }
},
"StopEC2InstancesRule": {
  "Type" : "AWS::Events::Rule",
  "Properties" : {
    "Name" : "StopEC2Instances",
    "ScheduleExpression" : "cron(0 13 ? * MON-FRI *)",
    "State": "ENABLED",
    "Targets": [{
      "Arn": { "Fn::GetAtt": ["LambdaStopEC2Instances", "Arn"] },
      "Id": "stopEC2Instances"
    }]
  }
},
"LambdaInvokePermission": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName" : { "Fn::GetAtt" : ["LambdaStopEC2Instances", "Arn"] },
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceAccount": { "Ref" : "AWS::AccountId" },
    "SourceArn": { "Fn::GetAtt": ["StopEC2InstancesRule","Arn"] }
  }
}

答案 3 :(得分:4)

截至本周(2016年4月18日),现在可以添加将触发Lambda功能的计划CloudWatch事件规则。

AWS::Event::Rule有一个用于cron样式计划的ScheduleExpression字段和一个可以接受Lambda函数ARN的Targets数组。

答案 4 :(得分:1)

AWS supports periodic run through sourcedetails

 EventSource: "aws.config"
 MaximumExecutionFrequency: Twelve_Hours
 MessageType: "ScheduledNotification"

答案 5 :(得分:0)

YAML版本

ScheduledRule: 
  Type: AWS::Events::Rule
  Properties: 
    Description: "ScheduledRule"
    ScheduleExpression: "rate(10 minutes)"
    State: "ENABLED"
    Targets: 
      - 
        Arn: 
          Fn::GetAtt: 
            - "LambdaFunction"
            - "Arn"
        Id: "TargetFunctionV1"
PermissionForEventsToInvokeLambda: 
  Type: AWS::Lambda::Permission
  Properties: 
    FunctionName: 
      Ref: "LambdaFunction"
    Action: "lambda:InvokeFunction"
    Principal: "events.amazonaws.com"
    SourceArn: 
      Fn::GetAtt: 
        - "ScheduledRule"
        - "Arn"

答案 6 :(得分:0)

如果将函数名称用作

"FunctionName": {
      "Fn::GetAtt": [
        "TagWatcherFunction",
        "Arn"
      ]
    }

,而您不指定该函数,它将抛出您 “模板无效:模板错误:Fn :: GetAtt实例引用了未定义的资源TagWatcherFunction”

因此,可以直接指定“ lambda ARN”来代替函数名称。 您可以在下面看到示例

"TagWatcherRule": {
  "Type": "AWS::Events::Rule",
  "Properties": {
    "ScheduleExpression": "rate(10 minutes)",
    "Targets": [
      {
        "Id": "TagWatcherScheduler",
        "Arn": {
          "Fn::GetAtt": [
            "TagWatcherFunction",
            "Arn"
          ]
        }
      }
    ]
  }
},
// role may call the lambda
"InvokeLambdaPermission": {
  "Type": "AWS::Lambda::Permission",
  "Properties": {
    "FunctionName": "arn:aws:lambda:<region>:<awsid>:function:<lambd name>",
    "Action": "lambda:InvokeFunction",
    "Principal": "events.amazonaws.com",
    "SourceArn": {
      "Fn::GetAtt": [
        "TagWatcherRule",
        "Arn"
      ]
    }
  }
}