Running script on EC2 launch automatically during cluster creation

Viewed 209

I am creating a ECS cluster (EC2 launch type) using the ecs-cli. I want to run a script to modify vm.max_map_count setting in /etc/sysctl.conf once the EC2 instance is created. At the moment, I am doing it manually by ssh'ing into the instance and running the script as sudo.

Is it possible to run automation script on the EC2 instance created as part of cluster creation? Any reference/documentation will be really helpful.

Thanks

1 Answers

Since you've tagged your question with amazon-cloudformation I assume that you are defining your ECS container instances using CFN.

If so, you can use UserData in your AWS::EC2::Instance to execute commands when the instances are launched:

You are probably already using it to specify cluster name for the ECS agents running on your instances. So probably you already have something similar in your UserData;

echo ECS_CLUSTER=${ClusterName} >> /etc/ecs/ecs.config
echo ECS_BACKEND_HOST= >> /etc/ecs/ecs.config  

You can extend the UserData with extra commands that would modify /etc/sysctl.conf.

There are some other possibilities, such as using SSM State Manager to perform actions when your instances launch.

Related