AWS EC2 Instances - Stop/Start and AMI Management (xpost from /r/sysadmin)

Hello fellow sysadmins! Forgive me if this is the wrong sub to post but I’m trying to figure out the best way to manage the stopping/starting and AMI(snapshot) creation of EC2 instances. I inherited an environment where the previous guy had some powershell scripts running on one of the Windows instances that imported what looks like AWS tools, then did the start/stopping/ami creation on a reoccurring schedule through Task Scheduler in Windows. Issue is with the AMI creation scripts…they have been failing the past few weeks and I tracked the reason down to the account the scheduled task were running under was an old expired account, so I updated it to a service account he used for all the other scripts. Now every day the task has said it has been running successfully, but no AMIs. Start/Stopping has been flaky at best, but working for the most part.

My hope is one of you have tackled this task before and have a better way of approaching it, maybe through another AWS service that way I don’t have to rely on the Windows EC2 Instance to do this. If you need to see the script let me know I’ll post it, but in reality I’d like to move away from that to something more fault-tolerant/more logging, so I can troubleshoot more effectively.


Related discussions:

You can use aws lambda to control the turning off instances, automate the creation of AMI’s . also you can use the aws cli
and just have another machine running cron jobs that will perform any task you require.

In all likelihood, this service user doesn’t have the necessary permissions to create AMIs. I suggest you create a new user in IAM with just enough permissions to start/stop instances and create/delete(?) AMIs, and configure AWS Tools for Powershell with this new user’s access credentials.

I take snapshots via powershell script that runs in a scheduled task every night. I want to transition this to a scheduled lambda function but I haven’t had time to rewrite it in python.

Is that what lambda uses, python? Sorry for the noob question. I ask because I’m learning python with the intent to run local scripts that’ll automate some AWS work. If I can just use python in lambda though, that sounds like one less step.

Lambda can use Python, node.js, and Java.
Using scheduled lambda task will also give you the benefit of using IAM roles so you won’t have to worry about key management.
I can also tell you from experience that running task like this in lambda is essentially free as lambda has a pretty high free usage threshold.
I have created an entire suite of aws management tools in lambda using Python and boto3. Backups, ec2 scheduling, automated tagging, tag violation notification, backup cleanup, etc. Hopefully someday I will be able to opensource it.
Do some searches and you can find some very good and simple examples that you can build off of.
One thing to keep in mind is that lambda has a process limit of 5 minutes. This only becomes an issue if you need to deal with thousands (more then 20k) of resources.
Use tags to identify instances to backup, set schedules, etc

lambda supports python, nodejs, and java last I looked.

I would run the script manually and step through it and see why it’s failing. No need to reinvent the wheel unless you’re charged with revamping his strategy (this is an acceptable strategy though more expensive).

You might want to install AWS logs on the windows box, push that scripts logs to CloudWatch and setup some events/alarms to make sure you know when it fails.

I use skeddly.com to manage ec2 instances, take backups, delete old backups, etc.

Thanks for this post!

We use python lambdas to do this kind of thing. See https://github.com/LeafSoftware/python-lambder

I use www.ecloudgate.com to schedule start/stops, reboots, create ami’s. It’s been well worth the $15/month as I have ~75 servers that I start/stop every day and weekly ami creations.

This was indeed the issue. I reset the password on the old account and applied it to the task scheduler task, low and behold it worked. So now I’m just trying to figure out how to give the service account access credentials within the EC2 instance like you suggested.

If you have access to the AWS Management Console, you can open up IAM, find the user and check the attached policies to determine the permissions it has.

However, using user accounts and storing the access credentials on EC2 instances are not recommended practice. Typically, your scenario would be implemented by giving the instance running the scripts an IAM role. You can watch this slightly outdated short video

or follow the steps below:

  1. define an access policy
  2. creating an IAM Role
  3. define that the EC2 service can take on this role
  4. attaching the policy you created earlier
  5. launching your instance with this IAM Role (can’t attach Roles to already launched instances)
  6. running the PowerShell scripts on that instance