AWS S3 Sync command at ec2 instance startup

Viewed 202

I want to run below command at my EC2 startup. This is an ubuntu 18.04 box. I have tried crontab (@reboot), User data startup script as per article User Data. I can see that command is being run in both cases (I create a output log file), but actually it does not complete sync. If I run manually this command in terminal this works perfectly fine.

aws s3 sync s3://mybucket s3://mybucket2

Yes I tried reboot and that aws link script as well. See below file structure and crontab expression. s3sync.log is empty one. Looks like cron run but it do not complete the command. or something else

-rw-rw-r-- 1 ubuntu ubuntu        0 Jun 16 14:55 s3sync.log
-rwxrwxr-x 1 ubuntu ubuntu       58 Jun 16 14:44 s3sync.sh

@reboot sh /home/ubuntu/s3sync.sh > /home/ubuntu/s3sync.log

Content of s3sync.sh

aws s3 sync s3://mybucket s3://mybucket2
1 Answers

Userdata will only ever execute the first time an instance boots.

You have 3 options:

  • The @reboot cron option
  • Create a systemd service to execute the task upon reboot
  • Wait for the instance state to become "running" and invoke an SSM command to trigger this, use CloudWatch Events to detect when EC2 instance state becomes running.
Related