Link to home
Start Free TrialLog in
Avatar of Tessando
TessandoFlag for United States of America

asked on

AWS CLI Batch File to Create EC2 Snapshot & AMI as Scheduled Task

I have two very simple, one line scripts to create snapshots of drives & create an AMI of an EC2 instance. Both of these scripts work when invoked manually, however only one of them works when I run it as a scheduled task.

The AMI creation script works perfectly when I invoke it via a Command line & when I run this as a scheduled task:

aws ec2 create-image --instance-id i-**************18b --name "server" --description "auto-server" --no-reboot

Open in new window


The EC2 Snapshot script works perfectly when invoked from the CLI tool, but won't work as a scheduled task:

aws ec2 create-snapshot --volume-id vol-*****9e6 --description "scheduled-task-auto-server-d"

Open in new window


Both Scheduled Tasks are using the same Windows user to run them and are configured identically. (I even had two people at work verify this). They also work in the same script, meaning that if I put both lines together I get the AMI and the snapshot. However, the reason my boss doesn't want to keep them in the same script is because he wants to take weekly snapshots of EBS drives and monthly AMI's.

Why am I getting different behavior when these run? Is there anything I could do to improve these simple scripts?

Thank you for your time.


...
Avatar of NVIT
NVIT
Flag of United States of America image

Are these scripts in a .bat file?

For the AMI creation script scheduled task, which works a scheduled task...
- Export the task.
- Make a new task for the EC2 script, importing the exported task. Change just the Action settings, to run the EC2 script.
Avatar of Tessando

ASKER

Great idea, thank you Nvit. I did this and it's still not working correctly. To be more specific, I did the export/import and changes you suggested and manually ran it from Scheduled Tasks (e.g. hit the "run" button) and setup a one-time trigger. The Scheduled Task completed successfully, but the Snapshots aren't showing up in Amazon. In my testing of the raw script (meaning outside of setting up a Scheduled Task), the snapshots showed up almost instantly & it's been over 10 minutes now.

I do like the idea of copying what I know works. Any other suggestions would be helpful. Anyone see anything weird in that line of code from the script?
Hmmm. I don't know why manual vs. task differs. Next step is to check with your provider.... Amazon?

Would the  --no-reboot switch be a factor?
This may shed some light... https://brianreiter.org/2013/12/10/backup-windows-server-on-ec2-powershell-awscli/
"... when schtasks runs the job with nobody logged in it gives the .DEFAULT environment instead of the environment of the user context of the scheduled task"
ASKER CERTIFIED SOLUTION
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After adding the configuration location in the script I was able to get this to work on two servers where I'd already setup the AWS CLI as the user that will run the scheduled task. As expected, it shows up almost immediately under snapshots. This might have been the secret sauce I was missing the entire time. Thanks for your help.