Lesson 2 of 3 Systems Manager Automation and Remediation

Running Automation at Scale Across Accounts and from Code

This lesson covers rate control, multi-account execution and starting runbooks from the CLI, SDKs and other services, so runbooks run consistently against many resources.

Learning outcomes

After completing this lesson, you will be able to:

  • Control concurrency and error thresholds.
  • Run runbooks across accounts and Regions.
  • Start runbooks from code and other services.

Rate control

A runbook with a target parameter (such as InstanceId) can run against many resources selected by tag, resource group or parameter values. Concurrency limits how many targets are processed at once and the error threshold stops the execution when too many fail, both as numbers or percentages. This protects production: a bad change stops after a few failures instead of affecting the whole fleet.

The following diagram shows an execution stopped by its error threshold.

Rate control for a runbook run against many targets. Targets are chosen by tag, here 40 instances tagged Env=prod. A concurrency of 10% processes four at a time, and an error threshold of 2 stops the execution after the second failure, so a bad change affects a few instances instead of the whole fleet.
The numbers are illustrative. Both settings accept either a count or a percentage.
Fill in the value

Rate control

Exam skills 1.2.3

Work out how rate control settings behave.

Log in to try this activity Interactive activities are free with an account.

Multiple accounts and Regions

Automation can run a runbook in several accounts and Regions from a central account. The central account needs the AWS-SystemsManager-AutomationAdministrationRole, and each target account needs the AWS-SystemsManager-AutomationExecutionRole trusted by the central account; both are usually deployed with CloudFormation StackSets. Targets are accounts or OUs plus Regions, with separate concurrency and error thresholds for locations. Runbooks can also be shared with specific accounts or the organisation.

The following diagram shows the roles in the central and target accounts.

Running Automation in several accounts and Regions. The central account uses the AWS-SystemsManager-AutomationAdministrationRole. Each target account has the AWS-SystemsManager-AutomationExecutionRole, which trusts the central account; both are usually deployed with StackSets. Targets are accounts or OUs plus Regions, with their own concurrency and error thresholds.
The accounts and Regions are illustrative. Without the execution role in a target account, that account fails while the others proceed.

Starting runbooks from code and services

Runbooks are started with the StartAutomationExecution API, so the CLI, any AWS SDK, a Lambda function or a script can run them. Executions can be monitored with GetAutomationExecution and are recorded in CloudTrail. Many services can start runbooks directly. The following table lists them.

TriggerHow
EventBridge ruleSystems Manager Automation target with an IAM role
CloudWatch alarmThrough EventBridge, or an OpsItem whose suggested runbook an operator runs
AWS Config ruleAutomatic or manual remediation action
Maintenance windowAutomation task
State ManagerAssociation that runs a runbook on a schedule
Security Hub, OpsCenterCustom action or OpsItem runbook
Any codeStartAutomationExecution API through an SDK or CLI
# CLI: restart every instance tagged App=checkout, 2 at a time, stop after 1 failure
aws ssm start-automation-execution --document-name AWS-RestartEC2Instance \
  --targets Key=tag:App,Values=checkout --target-parameter-name InstanceId \
  --max-concurrency 2 --max-errors 1

# Python (boto3)
import boto3
ssm = boto3.client("ssm")
run = ssm.start_automation_execution(
    DocumentName="AWS-CreateImage",
    Parameters={"InstanceId": ["i-0123456789abcdef0"], "NoReboot": ["true"]})
print(ssm.get_automation_execution(AutomationExecutionId=run["AutomationExecutionId"])
      ["AutomationExecution"]["AutomationExecutionStatus"])
Sort into groups

How should the runbook start?

Exam skills 1.2.3 · 3.2.2

Choose how each runbook should be started.

Log in to try this activity Interactive activities are free with an account.
Worked example

Weekly AMI backups across accounts

A company wants an AMI of every instance tagged Backup=weekly in 12 accounts and 2 Regions every Sunday. The administrator deploys the Automation administration and execution roles with StackSets, then creates a maintenance window in the central account with an Automation task that runs AWS-CreateImage against the tag across the target accounts and Regions, with 10% concurrency. AWS Backup would be the simpler choice for a pure backup requirement; Automation is chosen here because the team also runs pre-backup checks in the same runbook and deregisters AMIs older than eight weeks in a final step.

Check yourself

Which API starts a runbook from code?

Show answer

StartAutomationExecution.

Exam Tips

Remember for the exam

  • Rate control: concurrency and error threshold limit the blast radius of a runbook.
  • Multi-account Automation needs administration and execution roles, usually deployed with StackSets.
  • Start runbooks from code with StartAutomationExecution.
  • EventBridge, Config remediation, maintenance windows and State Manager can all run runbooks.