Is it possible to create an instance-store AMI using the ec2_ami module?

302 views Asked by At

I am using Ansible 2.1.3.0 at the moment and I am trying to figure out how to make an instance-store AMI using the ec2_ami module.

It looks like it is not possible to do this if the instance has instance-store root - I am getting the Instance does not have a volume attached at root (null)" no matter what I do.

So I am wondering how could I make an intance-store AMI (using the ec2_ami module) from an EBS-backed instance? The documentation does not allow me to map the first volume to be ami, and second needs to be ephemeral0. I was going through the ec2_ami module source code and it seems like it has no support for this but I may have overlooked something...

When I do curl http://169.254.169.254/latest/meta-data/block-device-mapping/ on the source EC2 instance, I am getting the following:

ami
ephemeral0
1

There are 1 answers

3
oucil On

I'm on 2.2 and using ec2 rather than ec2_ami, but this would accomplish what you're looking for...

- name: Launch instance
  ec2:
    aws_access_key: "{{ aws.access_key }}"
    aws_secret_key: "{{ aws.secret_key }}"
    instance_tags: "{{ instance.tags }}"
    count: 1
    state: started
    key_name: "{{ instance.key_name }}"
    group: "{{ instance.security_group }}"
    instance_type: "{{ instance.type }}"
    image: "{{ instance.image }}"
    wait: true
    region: "{{ aws.region }}"
    vpc_subnet_id: "{{ subnets_id }}"
    assign_public_ip: "{{ instance.public_ip }}"
    instance_profile_name: "{{ aws.iam_role }}"
    instance_initiated_shutdown_behavior: terminate
    volumes: 
      - device_name: /dev/sdb
        volume_type: ephemeral
        ephemeral: ephemeral0
  register: ec2

Pay special attention to the instance_initiated_shutdown_behavior param as the default is stop, which is incompatible with instance-store based systems, you'll need to set it to terminate to work.