Automate EC2 instances resize with AWS Ops Automator

EC2 instance

Automate EC2 instances resize with AWS Ops Automator

If you find yourself in a situation where you have to automate EC2 instances 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 AWS 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

Architecture

AWS 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, AWS 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.

ec2 instance

While all this looks complex and expensive, in reality, it isn’t. If you expect to trigger a couple of tasks every day, AWS Ops Automator will cost you less than a couple of dollars per month.

Deployment

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 AWS 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 AWS Ops Automator to perform cross-account task execution.

Scheduling EC2 instances type change

Once deployed, Ops Automator stores task templates in an S3 bucket named ops-automator-configuration-[random_string]. To create an EC2 instances resize task, navigate to Ops Automator S3 bucket, and copy the Ec2ResizeInstance template URL.

The URL should be similar to this one:
https://opsautomator-configuration-[some_random_string].s3.amazonaws.com/TaskConfiguration/Ec2ResizeInstance.template

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:

 

Parameter Example value Note
Stack name ec2-example-resize Arbitrary stack/task name.
Task interval 30 4 13 1 FRI Cron-like syntax. Task will execute on Friday 2023-01-13 at 04:30 AM
Regions eu-central1 Region in which the task will execute.
This account yes The task should execute on this AWS account.
Task enabled yes Enables/disables the task.
Resizing mode ReplaceByType An instance is resized according to the predefined list of desired instances types.
Assumed type t3.micro Mandatory parameter. It’s not used for ReplaceByType resizing mode
New instance size(s) t3.small,t2.small,t3.medium The list of one or more desired EC2 instance types in preferred order.
Try next in range yes 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 instances, perform instances type change, and start the EC2 instances 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.

Conclusion

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

If you need additional help with this, or if you are interested in a fully managed AWS infrastructure, feel free to contact us. We’ll gladly help you out!

Share this post