Register EC2 alarm during instance creation

I am able to create an ec2 cloudwatch alarm for existing instances by using the ec2 dynamic inventory script ie.

hosts: tag_Name_Build

  • name: Create CPU utilization metric alarm
    sudo: false
    local_action:
    ec2_metric_alarm
    dimensions=“InstanceId”=“{{ec2_id}}”

and running it as $ansible-playbook -i ec2-inventory.py add_alarms.yml

However I would really like to be able to attach these alarms when I initially create the instance. The problem I have run into is what value to use for the dimension attribute because {{ec2_id}} is not available. I’m looking for something similar to this:

  • name: Provision EC2 instances
    hosts: localhost
    connection: local
    gather_facts: False
    tasks:
  • name: Provision a set of instances
    ec2:

    count_tag:
    Name: Build
    instance_tags:
    Name: Build
    register: ec2

Attach alarms for new instances

  • name: Create CPU utilization metric alarm
    sudo: false
    local_action:
    ec2_metric_alarm
    dimensions=“InstanceId”=“{{ec2_id}}”

Has anyone found a solution for attaching alarms to newly created instances?

Many thanks

Toby