Issue
I'm creating EC2 Launch Template using AWS CDK as the code written below,
const userData = ec2.UserData.forLinux();
userData.addCommands(
'yum -y update',
);
new aws_ec2.LaunchTemplate(this, 'LaunchTemplate', {
machineImage: ec2.MachineImage.lookup({
name: 'RHEL-8.6.0_HVM-20220503-x86_64-2-Hourly2-GP2',
}),
userData: userData,
...
});
and somehow the Launch Template provisioned with an unexpected user data as written below,
#!/bin/bash
yum -y update
set +e
PKG_CMD=`which yum 2>/dev/null`
set -e
if [ -z "$PKG_CMD" ]; then
PKG_CMD=apt-get
else
PKG_CMD=yum
fi
$PKG_CMD update -y
set +e
$PKG_CMD install -y ruby2.0
RUBY2_INSTALL=$?
set -e
if [ $RUBY2_INSTALL -ne 0 ]; then
$PKG_CMD install -y ruby
fi
AWS_CLI_PACKAGE_NAME=awscli
if [ "$PKG_CMD" = "yum" ]; then
AWS_CLI_PACKAGE_NAME=aws-cli
fi
$PKG_CMD install -y $AWS_CLI_PACKAGE_NAME
TMP_DIR=`mktemp -d`
cd $TMP_DIR
aws s3 cp s3://aws-codedeploy-us-east-1/latest/install . --region us-east-1
chmod +x ./install
./install auto
rm -fr $TMP_DIR
The problem is it seems like yum doesn't recogizes aws-cli
, means that this codes are useless and I want to write my own codedeploy agent installation commands, so how can I remove this auto-generated user data?
Solution
You didn't specify this in the question, but it seems that you're using this launch template in Code Deploy, specifically as a deployment group.
When you create a ServerDeploymentGroup
construct in CDK, it automatically adds those commands to the UserData to set up the instance for Code Deploy.
Here's the code that does this:
So this is expected, you need this UserData to install the Code Deploy agent.
These commands are added to the UserData after you attach your ASG to the deployment group, that's why they appear after the command you specified.
Answered By - gshpychka Answer Checked By - Timothy Miller (WPSolving Admin)