1

I am trying to provide bash command for bootstrapping my ec2 instance during creation time in the following way

aws ec2 run-instances --image-id ami-0000025f7c02a13b2 --count 1 --instance-type t2.micro --user-data '#!/bin/bash\nyum install git -y'

I can spin up the ec2 but I cannot get the bash script to work. In the logs I see the following

/bin/bash\nyum: bad interpreter: No such file or directory

which makes me feel like the string is formatted wrong.

2
  • I tested on my terminal and the command executed without any errors. Commented Jan 21, 2021 at 21:20
  • you mean the install git command or spinning up the ec2 command. I can spin up the ec2 just fine but the bash script I provide using user-data does not get executed Commented Jan 21, 2021 at 21:26

2 Answers 2

3

Try adding a $ in front of your user data string.

aws ec2 run-instances --image-id ami-0000025f7c02a13b2 --count 1 --instance-type t2.micro --user-data $'#!/bin/bash\nyum install git -y'
Sign up to request clarification or add additional context in comments.

1 Comment

Worked!! Thank you! I spent so much time on it. Should've asked the question a lot earlier
1

If you intend to load a long script, it would be better to load the script from a file like this:

aws ec2 run-instances --image-id ami-abcd1234 --count 1 --instance-type m3.medium \
--key-name my-key-pair --subnet-id subnet-abcd1234 --security-group-ids sg-abcd1234 \
--user-data file://my_script.txt

and you file should be like this:

#!/bin/bash
yum update -y
service httpd start
chkconfig httpd on

See more details about loading data from a file while working with aws cli from this link

2 Comments

thanks! where is the file being loaded from. Can it be an s3 path
most welcome:) the file would be in the same location where you run this command, if you need to load from s3, I guess you will have to download it first from s3 to the current location and then use it --user-data file://$(aws s3 cp s3://bucket/folder/file.txt). I did not try this before, but you can give it a go :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.