Skip to content

AWS Maintenance task for stopping and starting particular EC2s.

0

I'm trying to set up a maintenance window which will patch the all the EC2s (windows and linux) in a region - where at a point, some instances are running and some are stopped. I have created a maintenance window that will run every month at a particular date to apply the patches and to register the targets, I am making use of resource groups (linux and windows patch group).

The first task I want to add is to start all the stopped instance in that region. Some instances which may be in stopped state this month, can be in running state the next month. Second - Check for updates Third - Update SSM agent and apply the patches Fourth - Power off all the instances which were in stopped state before

I am new to AWS, and confused on how to execute the first and the second task. I guess for the first task, I can maybe use the AWS document - AWS-StartEC2Instances, is that right? Does it start the stopped instances, while ignoring the already running instances? And for the fourth one, maybe I can first get the uptime from both linux and windows by running a command through ssm - and then use the 'total hour' value of the uptime to perform the shutdown (if it less that 4 hours or so). But I don't know how I will use that value to do this or if it is even feasible?

I would really appreciate it if someone can suggest a cost effective solution to execute these tasks.

asked 2 years ago700 views

2 Answers
1
Accepted Answer

Your approach to setting up a maintenance window for patching EC2 instances is on the right track. Let's go through your tasks and provide some guidance:

  1. Starting stopped instances: Yes, you can use the AWS-StartEC2Instances document for this task. This document will start the stopped instances in the specified region while ignoring the already running instances. You don't need to worry about accidentally restarting running instances.

  2. Checking for updates: This step is typically part of the patching process and doesn't require a separate task. The patching operation will automatically check for available updates.

  3. Updating SSM agent and applying patches: You can use the AWS-RunPatchBaseline document for this task. It will update the SSM agent if necessary and apply the appropriate patches based on your patch baseline configuration.

  4. Powering off previously stopped instances: Your idea of using uptime to determine which instances to shut down is clever, but there's a simpler and more reliable method. Instead of relying on uptime, you can use instance tags to mark the instances that were initially stopped. Here's a suggested approach:

a. Before starting the instances (in task 1), use a custom automation document or AWS Lambda function to add a tag (e.g., "MaintenanceWindowStarted: True") to all stopped instances. b. After patching, use another custom automation document or Lambda function to stop all instances with the "MaintenanceWindowStarted: True" tag, then remove the tag.

This method ensures you're only stopping the instances that were initially stopped, regardless of their uptime.

To implement this solution cost-effectively:

  1. Use AWS Systems Manager Maintenance Windows to schedule and manage your tasks.
  2. Leverage AWS Systems Manager Run Command to execute scripts and commands on your instances.
  3. Use AWS Lambda functions for custom logic (like tagging instances) if needed.
  4. Utilize resource groups to target your instances efficiently.

This approach minimizes the need for additional resources and leverages AWS's native capabilities, keeping costs down while providing a robust patching solution.
Sources
Stop and start Amazon EC2 instances - Amazon Elastic Compute Cloud
Setting up Maintenance Windows - AWS Systems Manager
Step 1: Create the maintenance window using the AWS CLI - AWS Systems Manager

answered 2 years ago

EXPERT

reviewed 2 years ago

1

Hi,

The full workflow of what you want to achieve is detailled here: https://medium.com/globant/patching-of-windows-and-linux-ec2-instances-using-aws-ssm-b409784a5bd7

Please, follow this article to implement your own case. In particular, focus on the section(s) about the maintenance window: you should leverage it to achieve what you want.

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.