简体   繁体   English

通过 cloudformation 的 AWS Lambda 计划事件源

[英]AWS Lambda scheduled event source via cloudformation

I already have my lambda / roles defined in cloudformation and would love to also use it to add a scheduled eventsources... are there any docs or examples around?我已经在 cloudformation 中定义了我的 lambda / 角色,并且也很想用它来添加预定的事件源......周围有任何文档或示例吗?

Use Aws::Event::Rule with a ScheduleExpression and a AWS::Lambda::PermissionAws::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"
      ]
    }
  }
}

I solved same problem.我解决了同样的问题。

"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"] }
  }
}

Unfortunately, configuring scheduled event sources for lambda functions is currently not supported by CloudFormation.遗憾的是,CloudFormation 目前不支持为 lambda 函数配置计划事件源。 You will need to deploy your lambda using CloudFormation and then manually configure your scheduled events.您将需要使用 CloudFormation 部署您的 lambda,然后手动配置您的计划事件。

CloudFormation does support an AWS::Lambda::EventSourceMapping resource type. CloudFormation 确实支持AWS::Lambda::EventSourceMapping资源类型。 However, this resource is limited configuring Kinesis or DynamoDB streams, so this is likely not helpful to you.但是,此资源仅限于配置 Kinesis 或 DynamoDB 流,因此这可能对您没有帮助。

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


**Update - as of April 2016, this is now supported using CloudWatch Events - https://aws.amazon.com/about-aws/whats-new/2016/04/amazon-cloudwatch-events-now-supported-in-aws-cloudformation-templates/ **更新 - 自 2016 年 4 月起,现在支持使用 CloudWatch Events - https://aws.amazon.com/about-aws/whats-new/2016/04/amazon-cloudwatch-events-now-supported-in -aws-cloudformation-模板/

The YAML Version 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"

As of this week (18 April 2016) it is now possible to add a scheduled CloudWatch event rule that will trigger your Lambda function.从本周(2016 年 4 月 18 日)开始,现在可以添加将触发您的 Lambda 函数的计划 CloudWatch 事件规则。

The AWS::Event::Rule has a ScheduleExpression field for the cron-style schedule and a Targets array which can accept a Lambda function ARN. AWS::Event::Rule有一个用于 cron 样式计划的ScheduleExpression字段和一个可以接受 Lambda 函数 ARN 的Targets数组。

AWS supports periodic run through sourcedetails . AWS 支持定期运行 sourcedetails

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

If you use function name as如果您使用函数名称作为

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

and you not specify the function then it'll throw you "Template is not valid: Template error: instance of Fn::GetAtt references undefined resource TagWatcherFunction"如果您没有指定函数,那么它会抛出“模板无效:模板错误:Fn::GetAtt 实例引用未定义的资源 TagWatcherFunction”

So instead of function name you can directly specify the "lambda ARN".因此,您可以直接指定“lambda ARN”而不是函数名称。 you can see example below你可以看到下面的例子

"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"
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM