Lesson 2 of 5 AWS Systems Manager

Running Commands and Opening Sessions with Systems Manager

This lesson explains how Run Command executes documents on many nodes at once and how Session Manager gives an interactive shell without SSH keys or open ports.

Learning outcomes

After completing this lesson, you will be able to:

  • Run commands at scale with rate controls.
  • Open logged sessions with Session Manager.
  • Remove SSH access from a fleet.

Run Command

Run Command executes an SSM document of type Command, such as AWS-RunShellScript, AWS-RunPowerShellScript, AWS-ConfigureAWSPackage or AWS-InstallApplication, on chosen managed nodes. Targets can be instance IDs, tags or resource groups. Rate control sets concurrency (how many nodes at once, as a number or percentage) and an error threshold (how many failures stop the command). Output can be written to S3 and CloudWatch Logs, because the console shows only the first 2,500 characters. Commands are logged in CloudTrail, and notifications can be sent to SNS. No SSH access or open inbound ports are required.

aws ssm send-command --document-name AWS-RunShellScript \
  --targets Key=tag:Role,Values=web \
  --parameters 'commands=["sudo systemctl restart nginx"]' \
  --max-concurrency 10% --max-errors 1 \
  --cloud-watch-output-config CloudWatchOutputEnabled=true,CloudWatchLogGroupName=/ssm/runcommand

2,500characters

Command output shown in the console

Scenario questions

Running commands safely

Exam skills 3.2.1

Choose Run Command settings for each situation.

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

Session Manager

Session Manager opens a browser or CLI shell session to a managed node through the SSM Agent. Inbound ports such as 22 and 3389, bastion hosts and SSH keys are not needed, and access is controlled with IAM policies such as ssm:StartSession scoped by instance tags. Session preferences can encrypt sessions with a KMS key, run sessions as a specific operating system user, set idle timeouts and log session activity to S3 and CloudWatch Logs; session start and end are recorded in CloudTrail. Port forwarding sessions tunnel a local port to a port on the instance or to a remote host such as an RDS database, and SSH and SCP can also be tunnelled through Session Manager.

The following table compares the ways to reach a shell on an instance.

MethodInbound portCredentialsAuditing
Session ManagerNoneIAMSession logs to S3/CloudWatch, CloudTrail
EC2 Instance Connect22 (or Instance Connect Endpoint for private subnets)IAM pushes a 60-second keyCloudTrail
SSH with key pairs22Long-lived private keyOS logs only
Bastion host22 on the bastionKeys on bastion and targetsOS logs only

The following diagram shows a session to a private instance and a port-forwarding session to RDS.

Session Manager access. An operator allowed ssm:StartSession, scoped by instance tags, opens a shell through Systems Manager; the SSM Agent connects outbound, so no inbound ports, bastion hosts or SSH keys are needed. Sessions can be encrypted with KMS and logged to S3 and CloudWatch Logs, and port forwarding can reach a remote host such as an RDS database.
Because the agent connects outbound, the instance's security group needs no inbound rule for the session.
True or false

Session Manager

Exam skills 3.2.1 · 4.1.1

Check facts about Session Manager.

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

Removing SSH access

A security audit requires that no security group allows port 22 and that all shell activity is recorded. The team confirms all instances are managed nodes, configures Session Manager preferences to send session logs to an encrypted CloudWatch log group and S3 bucket and to require KMS encryption, and grants engineers an IAM policy that allows ssm:StartSession only on instances tagged with their team. The inbound SSH rules are then removed. Engineers who need to reach a private database use a port forwarding session with the AWS-StartPortForwardingSessionToRemoteHost document.

Check yourself

Which Run Command settings limit how many nodes run a command at once and when it stops?

Show answer

Concurrency and error threshold rate control.

Exam Tips

Remember for the exam

  • Run Command targets nodes by tag with concurrency and error threshold rate control.
  • Send Run Command output to S3 or CloudWatch Logs for full output.
  • Session Manager needs no inbound ports or SSH keys; access is controlled by IAM.
  • Log session activity to S3 and CloudWatch Logs through session preferences.
  • Port forwarding sessions reach private databases without a bastion.