Solved

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

Posted on 2016-08-24
6
121 Views
Last Modified: 2016-08-25
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.


...
0
Comment
Question by:Tessando
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 41769576
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.
0
 

Author Comment

by:Tessando
ID: 41769596
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?
0
 
LVL 23

Expert Comment

by:NVIT
ID: 41769605
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?
0
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
LVL 23

Expert Comment

by:NVIT
ID: 41769607
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"
0
 
LVL 32

Accepted Solution

by:
shalomc earned 500 total points
ID: 41770138
Which user is used to run the scripts?

dir %UserProfile%\.aws

shows the contents of the configuration folder, which is, as you can see, specific per user profile.

put the command in a bat file, and add the following:

set AWS_CONFIG_FILE=<location of config file>

Open in new window


Consider also adding these lines to the .bat file:
set aws_access_key_id=AWS access key.
set aws_secret_access_key=AWS secret key

Open in new window

0
 

Author Closing Comment

by:Tessando
ID: 41770304
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.
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Monitoring systems evolution, cloud technology benefits and cloud cost calculators business utility.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now