title: Create AMI image as part of a cloudformation stack
body: <p>I want to create an EC2 cloudformation stack which basically can be described in the following steps:</p>
  
  <p>1.- Launch instance</p>
  
  <p>2.- Provision the instance</p>
  
  <p>3.- Stop the instance and create an AMI image out of it</p>
  
  <p>4.- Create an autoscaling group with the created AMI image as source to launch new instances.</p>
  
  <p>Basically I can do 1 and 2 in one cloudformation template and 4 in a second template. What I don't seem able to do is to create an AMI image from an instance inside a cloudformation template, which basically generates the problem of having to manually remove the AMI if I want to remove the stack.</p>
  
  <p>That being said, my questions are:</p>
  
  <p>1.- Is there a way to create an AMI image from an instance INSIDE the cloudformation template?</p>
  
  <p>2.- If the answer to 1 is no, is there a way to add an AMI image (or any other resource for that matter) to make it part of a completed stack?</p>
  
  <p>EDIT:</p>
  
  <p>Just to clarify, I've already solved the problem of creating the AMI and using it in a cloudformation template, I just can't create the AMI INSIDE the cloudformation template or add it somehow to the created stack.</p>
  
  <p>As I commented on Rico's answer, what I do now is use an ansible playbook which basically has 3 steps:</p>
  
  <p>1.- Create a base instance with a cloudformation template</p>
  
  <p>2.- Create, using ansible, an AMI of the instance created on step 1</p>
  
  <p>3.- Create the rest of the stack (ELB, autoscaling groups, etc) with a second cloudformation template that updates the one created on step 1, and that uses the AMI created on step 2 to launch instances.</p>
  
  <p>This is how I manage it now, but I wanted to know if there's any way to create an AMI INSIDE a cloudformation template or if it's possible to add the created AMI to the stack (something like telling the stack, "Hey, this belongs to you as well, so handle it").</p>