简体   繁体   English

cloudwatch事件触发cloudformation

[英]cloudwatch event trigger cloudformation

I am having some trouble writing the cloudformation script for cloudwatch event trigger to kick off my lambda script, I know I can do it through the console but my requirement is that I need to provision everything in cloudformation. 我在编写用于cloudwatch事件触发器的cloudformation脚本以启动我的lambda脚本时遇到了一些麻烦,我知道我可以通过控制台来完成它,但是我的要求是我需要在cloudformation中提供所有内容。 I followed the documentation and it still haven't worked for me and I kept getting the error: 我遵循了文档,但对于我来说仍然无效,并且不断出现错误:

Template contains errors.: Invalid template property or properties [rPermissionForEventsToInvokeLambda, rLambdaScheduledRule] 模板包含错误。:无效的模板属性或属性[rPermissionForEventsToInvokeLambda,rLambdaScheduledRule]

can someone point out what is the issue with this part of my cloudformation script? 有人可以指出我的cloudformation脚本的这部分是什么问题吗? I followed the document almost to the letter and still having error, even the example in the documentation have the same error when I tried to validate it. 我几乎按照文档进行操作,但仍然有错误,即使在尝试验证文档时,文档中的示例也有相同的错误。 my cloudformation code is below, any help is appreciated! 我的cloudformation代码在下面,感谢您的帮助!

rLambdaScheduledRule:
  Type: AWS::Events::Rule
  Properties:
    ScheduleExpression: rate(1 hour)
    State: ENABLED
    Targets:
      Ref: 
        Fn::ImportValue:
          Fn::Sub: rUploadLambda
    Action: lambda:InvokeFunction
rPermissionForEventsToInvokeLambda: 
  Type: AWS::Lambda::Permission
  Properties: 
    FunctionName: 
      Ref: 
        Fn::ImportValue:
        Fn::Sub: rUploadLambda
    Action: lambda:InvokeFunction
    Principal: events.amazonaws.com
    SourceArn: 
      Fn::GetAtt: 
        - rLambdaScheduledRule
        - Arn

1) You must export the Lambda function ARN in the CloudFormation template in which you create the lambda function. 1)您必须在创建Lambda函数的CloudFormation模板中导出Lambda函数ARN You need to pass the Lambda function ARN as input to the cloudwatch event (The AWS::Events::Rule Targets attribute requires a resource ARN). 您需要将Lambda函数ARN传递给cloudwatch事件( AWS::Events::Rule Targets属性需要资源ARN)。

See a sample script below: 请参阅下面的示例脚本:

Resources:
  # Create Controlled Lambda Function
  myLambda:
    Type: "AWS::Lambda::Function"
    Properties: 
      Code:
        S3Bucket: "lambda-bucket"
        S3Key: "myhandler.zip"
      Description: "Lambda handler"
      FunctionName: "myhandler"
      Handler: myhandler.myhandler
      MemorySize: 128
      Role: "arn:aws:iam::xxxxxxxxxxx:role/myLambdaExecutionRole-NC7FA7TUSZ5B"
      Runtime: "python3.6"
      Timeout: 10

# Output of the cf template
Outputs:
  myLambdaArn:
    Description: Arn of the my_lambda_function
    Value: !GetAtt myLambda.Arn
    Export: 
      Name: !Sub "${AWS::StackName}-LambdaArn"

2) When you create the CloudWatch Event , you need to pass the ARN of the lambda function created in Step1 as the Target . 2)创建CloudWatch Event时 ,需要将在步骤1中创建的lambda函数的ARN作为目标传递。

See a sample script below: 请参阅下面的示例脚本:

Resources:
  # Cloudwatch event to trigger lambda periodically
  rLambdaScheduledRule:
    Type: "AWS::Events::Rule"
    Properties: 
      Description: "CloudWatch Event to trigger lambda fn"
      ScheduleExpression: "rate(1 hour)"
      State: "ENABLED"
      Targets:
        - 
          Arn: 
            Fn::ImportValue:
              !Sub "${NetworkStackName}-LambdaArn"
          Id: "targetevent_v1"

  PermissionForEventsToInvokeLambda: 
    Type: "AWS::Lambda::Permission"
    Properties: 
      FunctionName: 
        Fn::ImportValue:
          !Sub "${NetworkStackName}-LambdaArn"
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn: 
        Fn::GetAtt: 
          - rLambdaScheduledRule
          - Arn

The value of ${NetworkStackName} should be the StackName from Step1. ${NetworkStackName}的值应为Step1中的StackName Some of the issues you need to correct in your template: 您需要在模板中纠正的一些问题:

  • correct the Targets property of resource rLambdaScheduledRule . 更正资源rLambdaScheduledRuleTargets属性。
  • remove Action property from resource rLambdaScheduledRule . 从资源rLambdaScheduledRule删除Action属性。
  • correct the FunctionName property of resource rPermissionForEventsToInvokeLambda . 更正资源rPermissionForEventsToInvokeLambdaFunctionName属性。

Keeping above sample as reference, correct your template and try again. 保留上面的示例作为参考,更正您的模板,然后重试。

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

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