3

I have an EC2 instance that I'm starting a very simple user data script:

#!/bin/bash
aws s3 cp s3://<bucket-name>/myconf.conf /etc/httpd/conf.d/myconf.conf

The instance has an associated IAM Role that allows access to the bucket and if I ssh into the running instance manually I can sudo execute the command to copy the file from S3 to the local filesystem.

However, if I delete the file, stop the instance, add the user data and start the instance again - then the file hasn't been copied down from S3 when I log back in.

Any ideas?

Thanks

5
  • Change aws s3 cp s3://<bucket-name>/myconf.conf /etc/httpd/conf.d/myconf.conf to aws s3 cp s3://<bucket-name>/myconf.conf /etc/httpd/conf.d/* and then see if the file gets copied? also can you paste permissions and user for /etc/httpd/conf.d directory Commented Sep 7, 2016 at 15:49
  • sudo execute is not the same as executing as root. Does your root account have .aws/credentials? Commented Sep 7, 2016 at 16:18
  • There is no /root/.aws directory or files but if I switch to root with sudo su - then I can execute the command OK: [[email protected] ~]# aws s3 cp s3://xxxxxx/myconf.conf /etc/httpd/conf.d/myconf.conf download: s3://xxxxxx/myconf.conf to ../etc/httpd/conf.d/myconf.conf Commented Sep 7, 2016 at 20:28
  • Permissions and user for conf.d as follows drwxr-xr-x 2 root root 4096 Sep 7 20:21 conf.d Commented Sep 7, 2016 at 20:29
  • I changed my user data script to the following: #!/bin/bash cp /home/ec2-user/foo.txt /home/ec2-user/bar.txt But even that didn't work Commented Sep 7, 2016 at 20:44

2 Answers 2

2

As part of the metadata included with Amazon EC2 instances, user data can be used to install packages or scripts. By default user data is executed once, at the first boot of the instance.

Resolution

  • In the Amazon EC2 console, choose the instance, Actions, Instance State, and then choose Stop.
  • Choose Actions, Instance Settings, and then choose View/Change User Data.
  • The following example is a shell script that writes "Hello World" to a file in the /tmp directory. Copy and paste into the User Data field, and then choose Save.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World." >> /tmp/sdksdfjsdlf
--//

Choose Actions, Instance State, and then choose Start.

After the cloud-init phase is complete, the user data commands that you've included should have executed on the instance.

Sign up to request clarification or add additional context in comments.

Comments

1

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html:

Important

User data scripts and cloud-init directives only run during the first boot cycle when an instance is launched.

2 Comments

Hi, thanks for that. I literally figured that out about 20 mins ago and now have the commands running successfully via /etc/rc.local
By default, user data scripts and cloud-init directives run only during the boot cycle when you first launch an instance but you can update your configuration to ensure that your user data scripts and cloud-init directives run every time you restart your instance.

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.