Ansible: запуск AWS Лямбда-функция через правило события Cloudwatch - PullRequest
0 голосов
/ 27 января 2020

Я пытаюсь вызвать функцию Lambda с правилом события Cloud Watch. Сначала я создаю лямбда-функцию, а после правила события в конце концов даю правило события для вызова лямбда-функции.

Если я проверяю созданное правило события, все выглядит как ожидалось, то есть цель указана в правиле. Однако, если я проверяю лямбда-функцию, правило события не устанавливается как триггер и для этого.

Ниже приводится книга, которую я использую:

---
- hosts: localhost

  vars:
    lambda_py_file: "%s_instances.py"
    lambda_zip_file: "%s_instances.zip"
    lambda_handler: "%s_instances.lambda_handler"
    lambda_name: "kkdb_%s_instances"
    lambda_desc: "%s instances"
    tag_name: "LF KKDB %s_ec2"
    ec2_functions:
      - start_ec2
      - stop_ec2

  tasks:
    - name: hint regarding tags
      debug:
        msg: 
        - "Available tags are:"
        - "'summer': Configures the start/stop of the Instances for German summer time"
        - "'winter': Configures the start/stop of the Instances for German winter time"
        - ""
        - "Note: If NO tag is choosen the default is winter"
      tags: always

    - name: "Package EC2 Lambda functions"
      archive:
        path: './{{ lambda_py_file | format(item) }}'
        dest: '{{ lambda_zip_file | format(item) }}'
        format: zip
      loop:
        "{{ ec2_functions }}"
      tags:
        - always

    - name: Create EC2 start stop lambda function
      lambda:
        name: '{{ lambda_name | format(item) }}'
        description: '{{ lambda_desc | format(item) }}'
        zip_file: '{{ lambda_zip_file | format(item) }}'
        runtime: 'python3.8'
        role: "arn:aws:iam::{{ account_id }}:role/KKDB-EC2-Start-Stop-Scheduler"
        handler: '{{ lambda_handler | format(item) }}'
        memory_size: 128
        timeout: 300
        state: present
        region: "{{ region }}"
        tags:
          InspectorScan: "true"
          Application ID: "APP-31013"
          Name: "{{ tag_name | format(item) }}"
          WANumber: "355811"
          Environment: "KKDB DE TI-11"
          Role: "application"
          SupportContact: "kkdb3ls.de@capgemini.com"
          Department: "FIK/S4"
          TaggingVersion: "1.0"
          Project ID: "ICTO-24823 - KKDB"
          PackageVersion: "{{ package_version }}"
      loop: "{{ ec2_functions }}"
      tags: always

    - name: Switch to summer time
      cloudwatchevent_rule:
        name: "kkdb_{{ item.name }}_instances_event_rule"
        schedule_expression: "cron({{ item.cron_expr }})"
        description: "{{ item.desc }}"
        targets:
          - id: "kkdb_{{ item.name }}_instances"
            arn: "arn:aws:lambda:{{ region }}:{{ account_id }}:function:kkdb_{{ item.name }}_instances"
      with_items:
          - { name: start_ec2, desc: "Starts stopped EC2 instances", cron_expr: "{{ start_ec2_cron_summer }}" }
          - { name: stop_ec2, desc: "Stops running EC2 instances", cron_expr: "{{ stop_ec2_cron_summer }}" }
          - { name: start_rds, desc: "Starts stopped RDS instances", cron_expr: "{{ start_rds_cron_summer }}" }
          - { name: stop_rds, desc: "Stops running RDS instances", cron_expr: "{{ stop_rds_cron_summer }}" }
      tags: summer

    - name: Switch to winter time
      cloudwatchevent_rule:
        name: "kkdb_{{ item.name }}_instances_event_rule"
        schedule_expression: "cron({{ item.cron_expr }})"
        description: "{{ item.desc }}"
        targets:
          - id: "kkdb_{{ item.name }}_instances"
            arn: "arn:aws:lambda:{{ region }}:{{ account_id }}:function:kkdb_{{ item.name }}_instances"
      with_items:
          - { name: start_ec2, desc: "Starts stopped EC2 instances", cron_expr: "{{ start_ec2_cron_winter }}" }
          - { name: stop_ec2, desc: "Stops running EC2 instances", cron_expr: "{{ stop_ec2_cron_winter }}" }
          - { name: start_rds, desc: "Starts stopped RDS instances", cron_expr: "{{ start_rds_cron_winter }}" }
          - { name: stop_rds, desc: "Stops running RDS instances", cron_expr: "{{ stop_rds_cron_winter }}" }
      tags: winter

    - name: Allowing CloudWatch Event(s) to trigger Lambda function(s)
      lambda_policy:
        function_name: "kkdb_{{ item }}_instances"
        statement_id: "kkdb_{{ item }}_instances_lambda-cloudwatch-trigger"
        action: "lambda:InvokeFunction"
        principal: "events.amazonaws.com"
        source_arn: "arn:aws:lambda:{{ region }}:{{ account_id }}:function:kkdb_{{ item }}_instances"
        region: "{{ region }}"
        state: present
      with_items: 
        - start_ec2
        - stop_ec2
      tags: always

Кто-нибудь сталкивался с этой проблемой раньше?

...