?
Solved

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

Posted on 2016-08-24
6
Medium Priority
?
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

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 25

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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 25

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

Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

Question has a verified solution.

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

Most of the applications these days are on Cloud. Cloud is ubiquitous with many service providers in the market. Since it has many benefits such as cost reduction, software updates, remote access, disaster recovery and much more.
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint …
This Micro Tutorial will explain how to export DynamoDB tables in Amazon Web Services.
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

770 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