Solved

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

Posted on 2016-08-24
6
368 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 24

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 24

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 24

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the wake of AWS' S3 outage, we want to discuss the importance of storage and data diversification in the event of a hack, crash, or system disruption. We spoke with Experts Exchange’s COO Gene Richardson for a deeper understanding.
When speed and performance are vital to revenue, companies must have complete confidence in their cloud environment.
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.
This Micro Tutorial will explain how to export DynamoDB tables in Amazon Web Services.

717 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