Automate EC2 instance resize with AWS Ops Automator
If you find yourself in a situation where you have to automate EC2 instance type change (vertical scaling) or create an EBS snapshot at specific time or time-based interval, then AWS Ops Automator might be the perfect solution for you. While the feature list of this tool isn’t impressively long, the AWS Ops Automator is a perfect tool to automate some of the simple, boring tasks that you’re probably performing manually anyway.
The Ops Automator is primarily designed to automate tasks that revolve around EC2 instances. The tool’s main features are:
- Schedule automated EC2 instance type change
- Schedule replacement of an EC2 instance
- Create, copy, and delete EBS snapshots on a defined schedule
- Tag EC2 instances depending on the instance’s CPU utilization
- Execute scheduled tasks across regions and different AWS accounts
Ops Automator is deployed using the AWS Cloudformation. The deployment is easy and it boils down to importing the official Cloudformation template and setting a couple of parameters. In the background, Ops Automator uses Lambda functions to handle task configuration, execution, resource selection, etc. Amazon DynamoDB is used for storing task-related data, Amazon SNS for handling event forwarding and notifications, and Amazon SQS for logging. While the logs are shipped to Amazon CloudWatch Logs, Amazon S3 is used for storing task output.
While all this looks complex and expensive, in reality, it isn’t. If you expect to trigger a couple of tasks every day, Ops Automator will cost you less than a couple of dollars per month.
You can initiate Ops Automator deployment with a single click, but we recommend that you follow up on the official deployment guide. Once you deploy Ops Automator to your primary AWS account, you must deploy the event forwarder template to each region where you intend to use Ops Automator.
If you wish to use Ops Automator to automate tasks on a secondary AWS account, then you must deploy the event forwarder template to applicable regions within that AWS account, and deploy the account role configuration template as well.
The account role configuration template will deploy necessary IAM roles that will enable Ops Automator to perform cross-account task execution.
Scheduling EC2 instance type change
Once deployed, Ops Automator stores task templates in an S3 bucket named ops-automator-configuration-[random_string]. To create an EC2 instance resize task, navigate to Ops Automator S3 bucket, and copy the Ec2ResizeInstance template URL.
The URL should be similar to this one:
Now head over to Cloudformation > Stacks > Create a new stack (with new resources) and paste the template URL. The stack deploy wizard will ask you to define various parameters. The key ones are these:
|Arbitrary stack/task name.
|30 4 13 1 FRI
|Cron-like syntax. Task will execute on Friday 2023-01-13 at 04:30 AM
|Region in which the task will execute.
|The task should execute on this AWS account.
|Enables/disables the task.
|An instance is resized according to the predefined list of desired instance types.
|Mandatory parameter. It’s not used for ReplaceByType resizing mode
|New instance size(s)
|The list of one or more desired EC2 instance types in preferred order.
|Try next in range
|If the first EC2 instance type on the list is unavailable, Ops Automator should try to set the subsequent instance type.
Once the task is defined, we need to associate it with the target EC2 instance. This is done by setting the OpsAutomatorTaskList tag on the EC2 instance. The tag value is a comma-separated list of tasks (Cloudformation stack names).
In our example, we need to add OpsAutomatorTaskList=ec2-example-resize tag.
And that’s it. When it’s time, Ops Automator will automatically shut down the target EC2 instance, perform instance type change, and start the EC2 instance again.
Details about task scheduling, execution, and possible errors are logged in OpsAutomator-logs CloudWatch logs group. If necessary, you can configure notifications via CloudWatch alarms based on the error keywords in the CloudWatch Logs.
Other use cases
All Ops Automator tasks are configured in the same way – using the provided task Cloudformation templates. You can associate one task to multiple EC2 instances or EBS volumes, and multiple tasks to a single EC2 instance or EBS volume.
For example, you can schedule an EC2 instance upgrade task before working hours, and an EC2 instance downgrade task after working hours to optimize costs.
Ops Automator also comes with a task template for automated EC2 vertical scaling depending on the instance’s CPU utilization. The task allows you to define high and low CPU usage thresholds and a range of desired EC2 instance types. Depending on the CPU usage, Ops Automator will automatically trigger the EC2 instance upgrade or downgrade tasks.
We don’t recommend using this scaling strategy if you have inconsistent CPU usage patterns or if your EC2 instance is processing jobs that shouldn’t be interrupted.
AWS Ops Automator is the perfect solution to automate a small set of boring tasks that would otherwise require you to perform them manually or possibly make you stay up late. Being able to deploy a single Ops Automator instance and use it to automate tasks across multiple AWS accounts is a nice feature.
If you need to automate something that Ops Automator doesn’t support out of the box, then you’re out of luck. You will probably want to find another tool for the job or develop a custom solution that fits your use case.