首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cloudwatchevent_rule默认为最新版本的lambda函数

cloudwatchevent_rule默认为最新版本的lambda函数
EN

Stack Overflow用户
提问于 2017-09-09 03:50:30
回答 2查看 722关注 0票数 2

我正在尝试自动创建一个lambda函数和cloudwatch规则。然而,似乎lambda ansible任务需要一个版本id才能将其自身附加到我的cloudwatchevent_rule函数。这会导致一个错误:

代码语言:javascript
复制
No target to arn:aws:lambda:us-east-
1:MYACCOUNTID:function:MYFUNCTIONNAME could be found on the rule 
MYFUNCTIONNAME.

如何更改它,使cloudwatch规则始终附加到我的lambda函数的最新版本:

代码语言:javascript
复制
- name: create cloudwatch rule
  cloudwatchevent_rule:
    name: 'name_for_rule'
    region: "{{region}}"
    description: 'trigger on new instance creation'
    state: present
    event_pattern: |-
      {
        "detail-type": [
          "AWS API Call via CloudTrail"
        ],
        "detail": {
          "eventSource": [
            "ec2.amazonaws.com"
          ],
          "eventName": [
            "RunInstances"
          ]
        }
      }
    targets:
      - id: "{{ lambda.configuration.version }}"
        arn: "{{ lambda.configuration.function_arn }}"
EN

回答 2

Stack Overflow用户

发布于 2017-09-17 19:49:00

我已经使用CloudWatch规则配置了一个Lambda函数来触发它。以下SAM模板还包含我所需的权限、策略和角色。如果不需要,请忽略这些。

代码语言:javascript
复制
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "AWS SAM template configuring lambda functions written in test package.",
  "Resources": {
    "OrchestratorTestLambdaFunction": {
      "DependsOn": [
        "LambdaPolicy"
      ],
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Handler": "com.test.TestClass::orchestrateTestLambda",
        "FunctionName": "OrchestratorTestLambda",
        "Runtime": "java8",
        "MemorySize": 256,
        "Timeout": 60,
        "Code": {
          "S3Bucket": "BATS::SAM::CodeS3Bucket",
          "S3Key": "BATS::SAM::CodeS3Key"
        },
        "Role": {
          "Fn::GetAtt": [
            "LambdaRole",
            "Arn"
          ]
        },
        "Description": "Lambda reads from SQS provided in the cloud watch."
      }
    },
    "LambdaRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": "LambdaRole",
        "AssumeRolePolicyDocument": {
          "Version": "2008-10-17",
          "Statement": [
            {
              "Sid": "",
              "Effect": "Allow",
              "Principal": {
                "Service": "lambda.amazonaws.com"
              },
              "Action": "sts:AssumeRole"
            }
          ]
        }
      }
    },
    "LambdaPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "lambda_policy",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Sid": "",
              "Effect": "Allow",
              "Action": [
                "sqs:DeleteMessage",
                "sqs:ReceiveMessage"
              ],
              "Resource": [
                {
                  "Fn::Sub": "arn:aws:sqs:eu-west-1:${AWS::AccountId}:TestUpdates"
                }
              ]
            },
            {
              "Sid": "",
              "Action": [
                "lambda:InvokeAsync"
              ],
              "Effect": "Allow",
              "Resource": "*"
            },
            {
              "Sid": "",
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
              ],
              "Resource": "arn:aws:logs:*:*:*"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "LambdaRole"
          }
        ]
      }
    },
    "PermissionForEventsToInvokeLambda": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Ref": "OrchestratorTestLambdaFunction"
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "events.amazonaws.com",
        "SourceArn": {
          "Fn::GetAtt": [
            "TestRule",
            "Arn"
          ]
        }
      }
    },
    "TestRule": {
      "Type": "AWS::Events::Rule",
      "Properties": {
        "Name": "TestRule",
        "Description": "Rule to Trigger OrchestratorTestLambdaFunction",
        "ScheduleExpression": "rate(1 minute)",
        "State": "ENABLED",
        "Targets": [
          {
            "Arn": {
              "Fn::GetAtt": [
                "OrchestratorTestLambdaFunction",
                "Arn"
              ]
            },
            "Id": "TestRuleV1",
            "Input": {
              "Fn::Sub": "{\"queueUrl\":\"https://sqs.eu-west-1.amazonaws.com/${AWS::AccountId}/TestUpdates\"}"
            }
          }
        ]
      }
    }
  },
  "Outputs": {
    "StackArn": {
      "Value": {
        "Ref": "AWS::StackId"
      },
      "Description": "Use this as the stack_arn in your cloud_formation_deployment_stack override."
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2019-11-20 18:51:48

我已经注意到从lambda ansible模块注册的function_arn输出不一致。

有些时候是

"function_arn": "arn:aws:lambda:zone:account:function:name"

其他时间是:

"function_arn": "arn:aws:lambda:zone:account:function:name:version"

因此,我构造了总是附加$LATEST版本的arn

代码语言:javascript
复制
  - cloudwatchevent_rule:
      profile: "{{ aws_profile }}"
      name: StartStop
      schedule_expression: cron(* * * * ? *)
      description: trigger my lambda
      targets:
        - id: StartStop
          arn: "arn:aws:lambda:{{aws_zone}}:{{aws_account_id}}:function:{{lambdadeploy.configuration.function_name}}:$LATEST"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46123670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档