Home > OS >  How to run custom setup for AWS EC2 instance?
How to run custom setup for AWS EC2 instance?

Time:08-18

I have seen this is possible in GCP and other clouds, but the "Advanced details" section in AWS EC2 doesn't seem to do anything to me.

I want to ensure I have the latest security dependencies and install custom monitoring agents.

I have tried pasting my scripts or pre-setup commands in the "User data" section. I don't see any sign of this being executed in the background. I check the history, running processes, and monitoring section.

My question is, how to do it for AWS EC2? Is the approach different compared to other clouds?

NOTE: In GCP, there is both pre-config and post-config script.

UPDATE 1:

export ENV=test
sudo yum update -y
sudo yum install docker -y

aws s3 cp s3://app-deployment-my-app/ec2-runner-$ENV.sh ec2-runner.sh
sudo systemctl start docker
sudo sh ec2-runner.sh
sudo chmod  x ec2-runner.sh
sudo /bin/bash -c 'echo "*/5 * * * * ec2-user sh /home/ec2-user/ec2-runner.sh" >> /etc/crontab'
sudo systemctl restart crond.service

CodePudding user response:

Cloud-init takes multiple user-data formats. If you want your user-data to be interpreted as a Unix script then it must begin with #!

ADDITION:

  • Keep in mind that #! should be used with the runner of your choice, for amzn2-ami-kernel-5.10-hvm-2.0.20220719.0-x86_64-gp2 it was #!/usr/bin/bash.
  • Server is available before cloud-init is finished, so the server is decoupled from the setup job. To ensure that cloud-init is/isn't running, try listening to cloud-init output sudo tail -f /var/log/cloud-init-output.log
  • Related