Search code examples
amazon-web-servicesamazon-ec2centosuser-datacloud-init

Run userdata on custom EC2 AMI created from centos 7


I use ami-3ecc8f46 available from https://aws.amazon.com/marketplace/pp/B00O7WM7QW.

If I use this AMI directly, and I specify a userdata script like:

#!/bin/bash

echo "Hello from userdata!"

.. commands

then everything works fine and the commands run. However, I want to make a custom AMI that is this centos7 AMI + some packages.

For example, I want to an ami-3ecc8f46 instance, install python 3.7, and re-image it as a custom AMI. When I do that, the userdata no longer runs, and curl http://169.254.169.254/latest/user-data returns a 404 error.

Unfortunately it seems like some combination of ec2/userdata/cloudinit has marked this instance as having already ran once and thus there is no need to provide or run the userdata any more. What is the easiest way to take a custom CentOS AMI, install some packages on top, and re-image is as a custom AMI that will still run my userdata scripts?


Solution

  • To create custom AMIs, you can use Packer.

    It has the advantages to define the build of your custom AMI as code (Infrastructure as Code). Userdata will be able to run in that custom AMI.

    Also have a look at an existing issue on SO for other solutions.