Running Commands and Automation Runbooks at Scale
Operational tasks such as restarting services, collecting diagnostics, creating images or remediating resources must often be performed across hundreds of nodes or many accounts. This lesson explains Systems Manager documents, Run Command with rate controls, Automation runbooks and their actions, multi-account and multi-Region execution, and maintenance windows for scheduled work.
Learning outcomes
After completing this lesson, you will be able to:
- Describe Systems Manager document types and how documents are shared and versioned.
- Explain how Run Command targets nodes and how concurrency and error thresholds control execution.
- Describe Automation runbooks, their common actions and how they are run across accounts and Regions.
- Explain how maintenance windows schedule tasks and how duration and cutoff work.
Systems Manager Documents
A document (SSM document) defines the actions Systems Manager performs, in JSON or YAML. Documents have types that correspond to capabilities: Command documents for Run Command and State Manager, Automation documents (runbooks), Session documents for Session Manager preferences, Package documents for Distributor and Policy documents for Inventory collection. AWS provides hundreds of documents, named with the AWS- or AWSSupport- prefix, such as AWS-RunShellScript, AWS-RunPatchBaseline and AWS-RestartEC2Instance. Custom documents are versioned, with a default version, and can be shared with specific accounts or publicly.
Run Command
Run Command executes a Command document on a set of managed nodes. Targets can be specified by instance IDs, tags, resource groups or all managed nodes. Two rate controls limit the impact of a command: concurrency is the number or percentage of nodes that run the command at the same time, and the error threshold is the number or percentage of failures after which Systems Manager stops sending the command to further nodes. Running a configuration change on 10% of nodes at a time with an error threshold of 2% limits a faulty script to a small part of the fleet.
Each node's result includes its status and exit code, as described in the Automated Testing module. The console shows only the first 2,500 characters of output, so full output should be sent to an S3 bucket or CloudWatch Logs. Run Command can publish status notifications to SNS and emits EventBridge events for command status changes. Every invocation is logged in CloudTrail, which makes Run Command an auditable replacement for running scripts over SSH.
aws ssm send-command --document-name AWS-RunShellScript \
--targets Key=tag:Application,Values=orders Key=tag:Environment,Values=prod \
--parameters 'commands=["systemctl restart orders-worker","systemctl is-active orders-worker"]' \
--max-concurrency 10% --max-errors 2% \
--cloud-watch-output-config CloudWatchOutputEnabled=true,CloudWatchLogGroupName=/ssm/run-command \
--comment "Restart workers after configuration change"Run commands safely
Automation Runbooks
Automation runs multi-step workflows called runbooks. Unlike Run Command, which runs scripts on nodes, a runbook can call any AWS API, run scripts, wait for conditions, branch, request approval and run commands on nodes as steps. Runbooks are used for routine operations, such as creating a golden AMI or rotating instances, and for remediation, such as restoring a public bucket to private. The commonly used actions are described below.
| Action | Purpose |
|---|---|
| aws:executeAwsApi | Call any AWS API operation and capture outputs |
| aws:executeScript | Run a Python or PowerShell script within the automation |
| aws:runCommand | Run a Command document on managed nodes |
| aws:waitForAwsResourceProperty and aws:assertAwsResourceProperty | Wait for or verify a resource state |
| aws:branch | Choose the next step based on a condition |
| aws:approve | Pause until designated IAM principals approve or reject |
| aws:createImage, aws:createStack, aws:changeInstanceState | Common resource operations |
| aws:executeAutomation | Run another runbook as a child |
| aws:invokeLambdaFunction | Invoke a Lambda function |
A runbook runs with the permissions of the caller or, preferably, of an IAM role passed in the AutomationAssumeRole parameter, which lets a user or an EventBridge rule start an automation that does more than the starter is allowed to do directly. Runbooks that target many resources support the same concurrency and error-threshold rate controls as Run Command.
Running Automation Across Accounts and Regions
An automation can run in multiple accounts and Regions from a central account. In the self-managed approach, the central account has the AWS-SystemsManager-AutomationAdministrationRole and each target account has the AWS-SystemsManager-AutomationExecutionRole, which trusts it; both are usually deployed with StackSets. With AWS Organizations, a delegated administrator for Systems Manager can target OUs. The execution specifies target locations (accounts or OUs, and Regions) with their own concurrency and error thresholds, so a remediation can roll out across an organisation in controlled waves.
Maintenance Windows
A maintenance window defines a recurring period, such as every Sunday from 02:00 for four hours, during which disruptive tasks may run. A window has a schedule (cron or rate expression with a time zone), a duration, and a cutoff, the number of hours before the end of the window after which no new tasks start, so that tasks finish within the window. Registered targets select nodes or resources, and registered tasks can be Run Command, Automation, Lambda or Step Functions tasks, each with a priority, rate controls and a service role. Patching, AMI creation and application restarts are typical maintenance window tasks.
AWS Systems Manager Change Calendar complements maintenance windows by defining periods when changes are allowed or blocked, such as a holiday freeze. Automations and State Manager associations can check the calendar and skip execution when it is closed.
Choose the Systems Manager capability
Fleet-Wide Diagnostic Collection
During an incident, engineers need the output of a diagnostic script from 800 application instances within minutes, without overloading the fleet, and the results must be kept for the post-incident review.
An engineer uses Run Command with a custom Command document that runs the diagnostic script, targeting instances by the Application tag with a concurrency of 20% and an error threshold of 5%, and sends output to an S3 bucket with a prefix for the incident. Because the instances are managed nodes, no SSH access is required, and CloudTrail records who ran the command. For the follow-up remediation, which must stop an instance, create an image and start it again, the team writes an Automation runbook with aws:changeInstanceState, aws:createImage and an aws:approve step, and runs it with an AutomationAssumeRole.
A Run Command execution must stop if more than three instances fail, and should never run on more than five instances at a time. Which settings achieve this?
Show answer
Set max-concurrency to 5 and max-errors to 3. When the fourth failure occurs, Systems Manager stops sending the command to further instances (invocations already in progress continue).
Exam Tips
Remember for the exam
- Run Command executes Command documents on targets selected by tags or resource groups; max-concurrency and max-errors limit impact; send full output to S3 or CloudWatch Logs.
- Automation runbooks orchestrate API calls, scripts, approvals and commands; use AutomationAssumeRole so starters need fewer permissions.
- Multi-account and multi-Region automation uses administration and execution roles or an Organizations delegated administrator, with per-location rate controls.
- Maintenance windows have a schedule, duration and cutoff and run Run Command, Automation, Lambda or Step Functions tasks; Change Calendar blocks changes during freezes.