Stop / Start instances assigned to a Maintenance Windows ID

0

I'm fairly new to AWS and just started working on setting up a Maintenace Window which runs AWS_RunPatchBaseLine at a specific time of the day when EC2 instances will be in a "Stopped" state.

The Maintenace Window will have registered instances based on Tags. The tags specify the time when the instance can be patched. So i'll require 3 tasks in the MW...

  1. Start Instance,
  2. Run AWS_RunPatchBaseLine,
  3. Stop Instance

I cannot use "AWS-StartEC2Instance" and "AWS-StopEC2Instance" for the tasks as both only take the "instance Id" as a parameter and not Tags. So seems like I need to build a new automation document that starts/stops instances that have a specific tag.

I can see that the Maintenance Window Target ID details references the Tags. I have a python script that can stop/start instances based on the instance Id, but what i would like to have is a Document (for the task) that is be able to retrieve the Maintenance Window Target ID and then use Python within the same document to start/stop the instances listed within the Target ID. Is this feasible?

Kal
asked a year ago290 views
2 Answers
0

You can configure and use Automation document like "AWS-StartEC2Instance" and "AWS-StopEC2Instance" in Maintenance window with Tags.

I would recommend you to configure parameter for Instance ID with below value, select registered targets with Tags and do not need to specify Instance ID other than below value:

{{RESOURCE_ID}}

I hope this solution will fix your issue.

AWS
Ajay
answered a year ago
  • Thanks for this. So i have cloned the AWS-StartEC2Instance runbook and then replaced the text in the document for {{InstanceIds}} with {{RESOURCE_ID}}. When i execute this document i get the message - "Failed to resolve input: RESOURCE_ID to type StringList. RESOURCE_ID is not defined in the Automation Document."

    1. Do i need to declare this as a parameter in the document?
    2. How does the new document i create get the value for {{RESOURCE_ID}} if i need to run this automated via the Maintenance Window?
  • You need to declare {{RESOURCE_ID}} under the "Instance ID" parameter text box while registering the Automation task in maintenance window. You do not need to declare or modify content/code of AWS-StartEC2Instance automation document.

0

Thank you for your question. If I understand your goal correctly, you would like to run maintenance window at specific point in time on specific instances. Currently, you are using "Tags" to encode when an instance can be patched. If this is the case, I think you can try to use the flags: (1) --schedule, (2) --duration and (3) --schedule-timezone to encode the time when maintenance window runs when using the "ssm create-maintenance-window" command. Hope this helps!

AWS
answered a year ago
  • I need to simply start and stop an EC2 instance based on the Tag that is assigned to it rather than using the Instance ID. So i can't use the built in runbook - "AWS-StartEC2Instance" as this only has parameter for instance id and not for any tags.

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.

Guidelines for Answering Questions