?
Solved

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

Posted on 2016-08-24
6
Medium Priority
?
687 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 26

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 26

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
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 
LVL 26

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 33

Accepted Solution

by:
shalomc earned 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you know what to look for when considering cloud computing? Should you hire someone or try to do it yourself? I'll be covering these questions and looking at the best options for you and your business.
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
Suggested Courses

850 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