# Updating the launch configuration on EC2

**URL:** <https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 15, 2014, 9:48am UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956 "2014-08-15T09:48:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arangel\_Angov](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Arangel\_Angov](https://forum.ansible.com/u/Arangel_Angov)\
**Post date:** [August 15, 2014, 9:48am UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/1 "2014-08-15T09:48:26Z")

</div>

Hi,

I've been playing with EC2 autoscaling for a while and my goal is to  
start an instance update it and then save it to an AMI or use the  
instance AMI id in a launch configuration tied to an auto scaling group  
so that I have my instance up to date all the time and ready for scaling.

So I wanted to update the launch configuration using ec2\_lc so that I  
either use the newly generated AMI image or the image\_id of the running  
instance. The first doesn't work cause if I want to update the lc I  
actually need to delete both autoscaling group and lc (and this is not  
acceptable cause I have production instances running in the auto scaling  
group, so deleting it will terminate those instances). The second  
approach doesn't really work also cause I guess the ec2\_lc module  
doesn't support updating the lc or using the instance\_id of my updated  
instance in the lc, which is something that ec2 supports  
([http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/create-lc-with-instanceID.html](http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/create-lc-with-instanceID.html)).

I could use the ec2 tools inside ansible but just wanted to see if  
anyone had this issue before and how you solved it - without using Tower  
of course. 🙂

Thanks.

-Arangel

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [August 15, 2014, 5:19pm UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/2 "2014-08-15T17:19:43Z")

</div>

Basically you just need to create a new launch configuration and then update it in the autoscaling group. Any new instances created in the autoscaling group will then use the new launch configuration.

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![Arangel\_Angov](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Arangel\_Angov](https://forum.ansible.com/u/Arangel_Angov)\
**Post date:** [August 29, 2014, 12:27pm UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/3 "2014-08-29T12:27:20Z")

</div>

Thanks for the suggestion, that's what I ended up doing.

Now to figure out how to delete all my old launch configurations.

-Arangel

> **(attachments)**
>
> ![get_social.png](https://us1.discourse-cdn.com/ansible/original/2X/2/27b541b3e66fe55dbe9eaf0b617420598c3cd40d.png)  
> ![http___s2.gramble.com_images_icons_email_social-media-icons_Gramble_32x3....png](https://us1.discourse-cdn.com/ansible/original/2X/8/83ae9f03856d8948386dcdd51584c91fa60a26dc.png)  
> ![http___s2.gramble.com_images_icons_email_social-media-icons_twitter_32X3....png](https://us1.discourse-cdn.com/ansible/original/2X/d/d2237ec90dd12b8d55e52930797aa238382feb19.png)  
> ![http___s2.gramble.com_images_icons_email_social-media-icons_facebook_32x....png](https://us1.discourse-cdn.com/ansible/original/2X/6/6c9c91570e1d3d5495d610c531af7690fab6f3a9.png)  
> ![social-media-icons_linkedin_32x32.png](https://us1.discourse-cdn.com/ansible/original/2X/9/985ad3e2317e2fb88b1c320bde9fb14489d3f56d.png)  
> ![http___s2.gramble.com_images_icons_email_social-media-icons_instagram_32....png](https://us1.discourse-cdn.com/ansible/original/2X/e/e561873909044631f0bfeae406e2d75f1922e0eb.png)  
> ![http___s2.gramble.com_images_icons_email_social-media-icons_googleplus_3....png](https://us1.discourse-cdn.com/ansible/original/2X/3/3ffbc10580fdbd6ae05bcb323a60fdb0e27817de.png)

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [August 29, 2014, 12:34pm UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/4 "2014-08-29T12:34:07Z")

</div>

Here’s my cleanup script and the relevant part of a module I use to do this.

If there’s interest I can PR the module.

def delete\_launch\_configs(asg\_connection, ec2\_connection, module):  
changed = False

launch\_configs = asg\_connection.get\_all\_launch\_configurations()

# Delete all launch configurations that no longer have an attendant AMI

for config in launch\_configs:  
image\_id = config.image\_id  
images = ec2\_connection.get\_all\_images(image\_ids=[image\_id])

if not images:  
config.delete()  
changed = True

tasks:

- name: Obtain list of existing backup AMIs  
local\_action:  
module: ec2\_ami\_facts  
description: “{{ ami\_image\_name }}-backup”  
tags:  
environment: “{{ app\_environment }}”  
sorts:

- “-name”  
region: “{{ vpc\_region }}”  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”  
register: ami\_facts  
ignore\_errors: yes

- name: Remove all but the most recent backup AMI  
local\_action:  
module: ec2\_ami  
image\_id: “{{ item.id }}”  
state: absent  
delete\_snapshot: yes  
region: “{{ vpc\_region }}”  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”  
with\_items: ami\_facts.images[1:]

- name: Remove all launch configurations whose AMIs no longer exist  
local\_action:  
module: ec2\_lc\_cleanup  
region: “{{ vpc\_region }}”  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”

ec2\_ami\_facts is used to enumerate the AMIs that I want to delete. It’s in a PR somewhere but was turned down for inclusion in core.

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [September 2, 2014, 2:18am UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/5 "2014-09-02T02:18:35Z")

</div>

I don’t think you need a custom launch config cleanup module - but I understand you do need a way to tell it what launchconfigs to cleanup

ec2\_lc already has state=delete  
[https://github.com/ansible/ansible/blob/devel/library/cloud/ec2\_lc#L225](https://github.com/ansible/ansible/blob/devel/library/cloud/ec2_lc#L225)

then it’s just a matter of determining the names of the launchconfigs that need deleting.

Will

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [September 2, 2014, 2:20am UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/6 "2014-09-02T02:20:56Z")

</div>

That’s the rub… the “just a matter of” is the piece that the custom module does. 🙂

-scott

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [September 2, 2014, 3:32am UTC](https://forum.ansible.com/t/updating-the-launch-configuration-on-ec2/15956/7 "2014-09-02T03:32:22Z")

</div>

True, but a custom module that deletes launch configs without images seems less clean than a custom module that returns launch config facts that you could then iterate over:

- local\_action:  
module: ec2\_lc\_facts  
… credentials  
register: launch\_configs

- local\_action:  
module: ec2\_lc  
name: “{{item.name}}”  
… credentials  
state: absent  
with\_items: launch\_configs  
when: item.image != ami\_facts.images[0]

I’d favour adding a list state to ec2\_lc rather than a custom ec2\_lc\_facts module (and similarly for ec2\_ami), but you’re right, similar implementations have not made it through the pull review process. (Although ec2\_vol and rds have similar capability)
