Create an EC2 launch template

In this step, you are going to create a Launch Template to specify configuration parameters for launching instances with EC2 Auto Scaling in this workshop. Click here to learn more about Launch Templates
  1. Open launch-template-data.json on the Cloud9 editor and examine the configuration, you will notice some of the parameters have a placeholder value %variableName%: %instanceProfile%, %instanceSecurityGroup% and %ami-id%. Cloud9 Editor

  2. The variable %ami-id% should contain the latest Amazon Linux 2 AMI, and instanceProfile and instanceSecurityGroup need to be populated with the resources created by your CloudFormation stack; which are available as Stack Outputs. We can pull the latest Amazon Linux 2 AMI with the AWS CLI, and as we have loaded our CloudFormation stack outputs as environment variables on a previous step, for convenience we can use the following commands to update your configuration file:

    # First, this command looks up the latest Amazon Linux 2 AMI
    export ami_id=$(aws ec2 describe-images --owners amazon --filters 'Name=name,Values=amzn2-ami-hvm-2.0.????????-x86_64-gp2' 'Name=state,Values=available' --output json | jq -r '.Images |   sort_by(.CreationDate) | last(.[]).ImageId')
    sed -i.bak -e "s#%instanceProfile%#$instanceProfile#g" -e "s/%instanceSecurityGroup%/$instanceSecurityGroup/g" -e "s#%ami-id%#$ami_id#g" -e "s#%UserData%#$(cat user-data.txt | base64 --wrap=0)#g" launch-template-data.json
  3. Your configuration file should now have the variables populated. If you don’t see the file updated on the Cloud9 editor, click on it and you will get a message box indicating the file has changed. In that case, click on Keep remote.

  4. Create the launch template from the launch template config you just updated:

    aws ec2 create-launch-template --launch-template-name myEC2Workshop --launch-template-data file://launch-template-data.json
  5. Browse to the Launch Templates console and check out your newly created launch template. It will look similar to the template below: launch-template

  6. Verify that the contents of the launch template are correct:

    aws ec2 describe-launch-template-versions --launch-template-name myEC2Workshop
  7. Take a look at the user-data script configured on the launch template to understand what will be installed on the instances while being bootstrapped.

    aws ec2 describe-launch-template-versions  --launch-template-name myEC2Workshop --output json | jq -r '.LaunchTemplateVersions[].LaunchTemplateData.UserData' | base64 --decode