通过Ansible创建Cloudwatch事件规则成功但未调用

时间:2017-07-24 14:17:19

标签: ansible aws-lambda amazon-cloudwatch amazon-ecs

blog post启用在每个ecs主机上运行任务之后,我创建了一个Ansible角色,该角色成功创建了一个触发Lambda函数的Cloudwatch事件规则。

事件规则在AWS控制台中看起来很好,似乎在指标中触发,但lambda函数未运行(ecs服务未更改)。

当简单地编辑规则并保存而没有更改时规则开始起作用,并且ecs服务按预期更改。

这是我原来的剧本和角色,如果需要,我可以创建一个更简单的例子。

Playbook:

- name: "create lambda and cloudwatch event rules"
  hosts: localhost
  roles:
    - {
        role: aws/lambda/lookup,
        lambda: ecs-task-on-all-hosts,
        lambda_lookup_register_as: lambda_lookup
      }
    - {
        role: aws/cloudwatch/event/rule/create,
        event_rule: ecs-task-on-all-hosts,
        cluster: "{{ cluster }}",
        lambda_ecs_task_on_all_hosts_arn: "{{ lambda_lookup.arn }}"
      }

角色aws/lambda/lookup

- name: "lookup lambda {{ lambda }}"
  lambda_facts:
    region: "{{ region }}"
    query: config #right now everything we need is given here
    function_name: "{{ lambda }}"
  register: _lambda_function_details

- name: "set lambda lookup result facts"
  set_fact:
    "{{ lambda_lookup_register_as }}":
      arn: "{{ _lambda_function_details.ansible_facts.lambda_facts.function[lambda].function_arn }}"
      name: "{{ _lambda_function_details.ansible_facts.lambda_facts.function[lambda].function_name }}"

角色aws/cloudwatch/event/rule/createtasks/main.yml

- name: "include variables in file {{ event_rule }}.yml"
  include_vars: "files/{{ event_rule }}.yml"

- name: "verify mandatory parameters were provided"
  include_role:
    name: utilities/verify-parameters
  vars:
    mandatory: "{{ event_mandatory_parameters }}"
  when: event_mandatory_parameters is defined

- name: "create cloudwatch event rule"
  cloudwatchevent_rule:
    region: "{{ region }}"
    name: "{{ event_definition.name }}"
    description: "{{ event_definition.description }}"
    event_pattern: "{{ event_definition.event_pattern | to_json }}"
    targets: "{{ event_definition.targets }}"

正在使用的文件files/ecs-task-on-all-hosts

event_mandatory_parameters: [ "cluster", "lambda_ecs_task_on_all_hosts_arn" ]
event_definition:
  name: ecs-task-on-all-hosts
  description: Ensure a task is running on all hosts in the cluster
  event_pattern: |-
    {
      "source": [
        "aws.ecs"
      ],
      "detail-type": [
        "ECS Container Instance State Change"
      ],
      "detail": {
        "clusterArn": [
          "arn:aws:ecs:{{ region }}:{{ account_id }}:cluster/{{ cluster }}"
        ]
      }
    }
  targets:
    - id: lambda_ecs_task_on_all_hosts
      arn: "{{ lambda_ecs_task_on_all_hosts_arn }}"

提出了一个类似的问题here,但它有点不同,仍然没有答案,所以我提供了我可以在这里提供的所有细节。如果需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您需要添加Lambda函数策略,以允许CloudWatch Events调用您的Lambda函数。编辑CloudWatch Event的原因是它在后台添加了Lambda函数策略。

您可以使用aws lambda get-policy检查功能的政策。在编辑事件之前,您应该看不到任何策略,在编辑之后,您将看到允许CloudWatch Events调用该函数的策略。

您可以使用Ansible模块lambda_policy在Ansible中设置策略,例如

- name: allow CloudWatch to invoke the Lambda function
  lambda_policy:
    region: "{{ aws_region }}"
    function_name: ecs-task-on-all-hosts
    state: present
    statement_id: lambda-cloudwatch-event-rule
    action: lambda:InvokeFunction
    principal: events.amazonaws.com
    source_arn: "{{ event.rule.arn }}"

其中eventcloudwatchevent_rule的返回值。

来源: