launch templates and AWS image builder using old user-data

0

I'm using AMI image builder to build the base image for an AMI, when the AMI is released it updates the AMI on a launch template however, if I try to update the User data script which is on the LT, there's a weird issue where the LT user-data gets updated but it doesn't select the new AMI, it just blank which makes the launch template using the old version of the template with the older AMI.

When I run the AMI builder it'll update the Launch template but will use the old version of the user data script.

joet
asked a year ago320 views
1 Answer
0

It seems like the issue might be due to the order of operations during the updating of the Launch Template. When you're updating the Launch Template with the new AMI, make sure you're also specifying the updated user-data script at the same time.

Here's an example of how you might do this using the AWS CLI:

aws ec2 create-launch-template-version --launch-template-id lt-0abcd1234ef56789a --source-version $old_version --launch-template-data file://launch-template-data.json

In this command, lt-0abcd1234ef56789a is the ID of the launch template you're updating and $old_version is the version of the launch template you're using as a base. The launch-template-data.json file should include the ID of the new AMI and the updated user-data script.

The launch-template-data.json file would look something like this:

{
    "ImageId": "ami-0abcd1234ef56789a",
    "UserData": "IyEvYmluL2Jhc2gKCnN1ZG8gYXB0LWdldCB1cGRhdGUgLXkKc3VkbyBhcHQtZ2V0IHVwZ3JhZGUgLXkKc3VkbyBhcHQtZ2V0IGluc3RhbGwgLXkgZ2l0"
}

In this file, ami-0abcd1234ef56789a is the ID of the new AMI and IyEvYmluL2Jhc2gKCnN1ZG8gYXB0LWdldCB1cGRhdGUgLXkKc3VkbyBhcHQtZ2V0IHVwZ3JhZGUgLXkKc3VkbyBhcHQtZ2V0IGluc3RhbGwgLXkgZ2l0 is the Base64-encoded user-data script.

Remember to replace the above details with your own AMI ID, Launch Template ID and UserData script. The UserData script should be Base64 encoded.

It's also important to note that when you create a new version of a launch template, the default version of the launch template doesn't automatically update to the new version. You can set the new version as the default using the modify-launch-template command:

aws ec2 modify-launch-template --launch-template-id lt-0abcd1234ef56789a --default-version $new_version

In this command, lt-0abcd1234ef56789a is the ID of the launch template and $new_version is the version number of the new version you just created.

profile picture
EXPERT
answered a year ago
  • I'm doing this in terraform, can't seem to find 'source-version' under launch template?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions