SOA-C02Chapter 49 of 104Objective 4.1

EC2 Instance Connect and Session Manager

This chapter covers two secure methods to connect to EC2 instances without managing SSH keys or exposing inbound ports: EC2 Instance Connect and AWS Systems Manager Session Manager. For the SOA-C02 exam, these topics fall under Domain 4.0 (Security) and are increasingly important as AWS pushes for passwordless, keyless access. Expect 5–8% of exam questions to touch on these technologies, often comparing them or testing prerequisites and IAM policies. Mastering these will help you answer questions about secure remote access, bastion host elimination, and audit logging.

25 min read
Intermediate
Updated May 31, 2026

The Secure Tunneled Access Pass

Imagine a high-security corporate building where employees use a special badge that not only unlocks the door but also creates a temporary, encrypted tunnel to a specific room. EC2 Instance Connect is like a one-time passcode sent to your phone that lets you into the building's lobby, but then you still need a key to the office door. Session Manager is like a secure, monitored, and logged video call from a security booth directly to the office computer—no one ever walks through the building. In the AWS world, the building is your VPC, the office door is the EC2 instance's SSH/RDP port, and the security booth is the AWS Systems Manager service. With Session Manager, you don't need a public IP, a bastion host, or even an open inbound port. Instead, the Systems Manager Agent (SSM Agent) on the instance initiates an outbound connection to the AWS Systems Manager service over HTTPS (port 443). The service then creates a bidirectional, encrypted tunnel between your browser or CLI and the instance. This is fundamentally different from SSH, where you initiate an inbound connection to the instance. Session Manager is like the security guard calling the office phone and patching you through—the office never reveals its direct number. This mechanism eliminates the attack surface of exposed SSH ports and simplifies access management by centralizing IAM permissions.

How It Actually Works

What Are EC2 Instance Connect and Session Manager?

EC2 Instance Connect (EIC) and AWS Systems Manager Session Manager are two distinct services that provide secure, auditable access to EC2 instances without requiring long-lived SSH keys or managing bastion hosts. Both are part of AWS's shift toward identity-based access, but they work differently.

EC2 Instance Connect allows you to connect to an instance using a one-time SSH key that is pushed to the instance for 60 seconds. You initiate the connection from the AWS Management Console, AWS CLI, or SSH client, and AWS temporarily injects a public key into the instance's ~/.ssh/authorized_keys file for the specified IAM user. You then use the corresponding private key (which you never see) to authenticate via SSH. The key is automatically removed after 60 seconds or when the connection closes.

Session Manager, part of AWS Systems Manager, provides a browser-based or CLI-based interactive shell (or RDP session) without needing SSH keys, a public IP, or an open inbound port. The SSM Agent on the instance establishes an outbound connection to the Systems Manager service over HTTPS (port 443). When you start a session, the service creates a secure, encrypted tunnel between your client and the instance. All commands run through the session are logged to Amazon CloudWatch Logs or S3.

How They Work Internally

EC2 Instance Connect Flow: 1. The user calls the SendSSHPublicKey API (or uses the console which calls this API). The request is authorized by IAM. 2. AWS generates an ephemeral RSA key pair. The public key is sent to the instance metadata service (IMDS) endpoint: http://169.254.169.254/latest/meta-data/public-keys/0/. 3. The ec2-instance-connect service (a daemon on the instance, pre-installed on Amazon Linux 2/2023 and Ubuntu 20.04+) polls the IMDS endpoint every 1 second. When it detects a new key, it appends it to the ~/.ssh/authorized_keys file of the IAM user's corresponding OS user (e.g., ec2-user for Amazon Linux). 4. The public key is valid for 60 seconds. After that, it is removed from IMDS and the authorized_keys file. 5. The user's SSH client uses the private key (provided by AWS or generated locally) to authenticate. The SSH connection must be initiated within 60 seconds. 6. Once connected, the session is a normal SSH session. The key persists for the duration of the connection (if the connection outlasts 60 seconds, the key is already removed, but the established session remains).

Session Manager Flow: 1. The SSM Agent (version 2.3.68.0 or later) on the instance maintains a persistent outbound WebSocket connection to the Systems Manager service endpoint (e.g., ssm.<region>.amazonaws.com). This connection uses TLS on port 443. 2. The user initiates a session via the AWS Management Console, AWS CLI (aws ssm start-session), or the Session Manager plugin. The request is authorized by IAM. 3. Systems Manager sends a message to the agent over the existing WebSocket connection, instructing it to start a shell (or RDP) and create a new WebSocket stream for the session. 4. The agent opens a pseudo-terminal (PTY) and connects it to the shell. All input/output is encrypted and tunneled through the Systems Manager service to the user's client. 5. The session is recorded. You can enable session logging to CloudWatch Logs or S3. You can also run shell scripts via aws ssm send-command without interactive access. 6. The agent does not require inbound ports—only outbound HTTPS to the Systems Manager endpoints. The instance does not need a public IP or a NAT gateway.

Key Components, Values, Defaults, and Timers

EC2 Instance Connect: - IAM permissions: ec2-instance-connect:SendSSHPublicKey required. Also ec2:DescribeInstances for the console. - Key validity: 60 seconds. The key is removed from IMDS and authorized_keys after this time. - Supported OS: Amazon Linux 2, Amazon Linux 2023, Ubuntu 16.04+, and others with the ec2-instance-connect package installed. - Private key: For CLI, you can generate your own key pair and send the public key via ssh-keygen -t rsa -f mykey then aws ec2-instance-connect send-ssh-public-key. For console, AWS generates the key pair and provides the private key for download (one-time view). - Port: Only SSH (port 22) is supported. No RDP support. - Instance must have: A public IP or be reachable via SSH from the client (e.g., via VPC peering, VPN, or Direct Connect). The SSH daemon must be running.

Session Manager: - SSM Agent version: Minimum 2.3.68.0. For RDP, version 3.0.654.0 or later. - Outbound endpoints: ssm.<region>.amazonaws.com, ssmmessages.<region>.amazonaws.com, and ec2messages.<region>.amazonaws.com. These must be reachable via HTTPS (port 443). - IAM permissions: ssm:StartSession, ssm:TerminateSession, ssm:ResumeSession. Also ssmmessages:CreateControlChannel, ssmmessages:CreateDataChannel, ssmmessages:OpenControlChannel, ssmmessages:OpenDataChannel (these are managed by AWS when using the default session manager policy). - Instance profile: The instance must have an IAM role that includes the AmazonSSMManagedInstanceCore policy (or equivalent). - VPC endpoints: To avoid internet access, you can create VPC endpoints for Systems Manager (com.amazonaws.<region>.ssm, com.amazonaws.<region>.ssmmessages, com.amazonaws.<region>.ec2messages). - Logging: Session logs can be streamed to CloudWatch Logs or S3. You can also encrypt logs with KMS. - Port forwarding: Session Manager supports port forwarding (aws ssm start-session --target <instance-id> --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["80"],"localPortNumber":["8080"]}'). This allows you to access applications on the instance without opening inbound ports. - Idle timeout: The default idle timeout for a session is 20 minutes. If no input is received, the session terminates. This can be configured in the session preference. - Run as: You can specify an OS user when starting a session (e.g., aws ssm start-session --target <instance-id> --document-name AWS-StartInteractiveCommand --parameters '{"command": ["sudo su - root"]}').

Configuration and Verification Commands

EC2 Instance Connect Setup: 1. Verify the instance has ec2-instance-connect installed:

# Amazon Linux 2/2023: pre-installed
# Ubuntu:
sudo apt-get update
sudo apt-get install ec2-instance-connect
2.

IAM policy for user:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2-instance-connect:SendSSHPublicKey",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "ec2:DescribeInstances",
            "Resource": "*"
        }
    ]
}
3.

Connect via CLI:

ssh-keygen -t rsa -f /tmp/mykey -N ""
aws ec2-instance-connect send-ssh-public-key \
    --instance-id i-1234567890abcdef0 \
    --availability-zone us-east-1a \
    --instance-os-user ec2-user \
    --ssh-public-key file:///tmp/mykey.pub
ssh -i /tmp/mykey ec2-user@<public-ip-or-dns>

Session Manager Setup: 1. Attach IAM role to instance with AmazonSSMManagedInstanceCore policy. 2. Install SSM Agent (if not pre-installed):

# Amazon Linux 2: pre-installed
# Ubuntu:
sudo snap install amazon-ssm-agent --classic
sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent
3.

Verify agent is running:

sudo systemctl status amazon-ssm-agent
4.

Check instance is managed in Systems Manager Fleet Manager.

5.

Connect via CLI (requires Session Manager plugin):

aws ssm start-session --target i-1234567890abcdef0
6.

Or via console: Navigate to Systems Manager > Session Manager > Start session.

Interaction with Related Technologies

Both EIC and Session Manager integrate with AWS Identity and Access Management (IAM) for authentication and authorization. EIC relies on SSH key exchange, but the key is ephemeral and tied to the IAM user. Session Manager eliminates SSH keys entirely and uses IAM policies to control who can start sessions and what actions they can perform.

Both can be used with AWS CloudTrail for auditing. EIC logs the SendSSHPublicKey API call. Session Manager logs session starts, stops, and the commands run (if logging is enabled).

Session Manager can also be used with AWS Config to enforce that instances use Session Manager instead of SSH (via a custom rule).

EIC does not work with instances in a private subnet unless you have a bastion host or VPN. Session Manager works natively in private subnets if VPC endpoints are configured.

Both are alternatives to traditional bastion hosts. Session Manager is generally preferred for its security and auditability, but EIC is simpler for existing SSH workflows.

Exam Traps

The exam often tests the prerequisites for each service. For EIC, a common trap is assuming the instance needs a public IP. Actually, EIC requires network connectivity for SSH (so a public IP or reachability via VPN/VPC peering). For Session Manager, a common trap is forgetting the instance needs outbound internet access or VPC endpoints. Another trap is that Session Manager does not support RDP by default—it requires the Session Manager plugin and the AWS-StartPortForwardingSession document or a dedicated RDP session document.

Also, remember that EIC pushes a public key, not a password. The private key is never stored on the instance. The key is valid for 60 seconds—if you take longer to SSH, you must request a new key.

Finally, Session Manager sessions can be terminated by an administrator via aws ssm terminate-session. The idle timeout is 20 minutes by default, but can be configured in session preferences.

Walk-Through

1

Install and Configure SSM Agent

Ensure the SSM Agent is installed and running on the EC2 instance. For Amazon Linux 2 and 2023, it is pre-installed. For Ubuntu, use `sudo snap install amazon-ssm-agent --classic`. The agent must have outbound HTTPS access to the Systems Manager endpoints (`ssm.<region>.amazonaws.com`, `ssmmessages.<region>.amazonaws.com`, `ec2messages.<region>.amazonaws.com`). If the instance is in a private subnet without a NAT gateway, create VPC endpoints for these services. Verify the agent status with `sudo systemctl status amazon-ssm-agent`. The agent version must be 2.3.68.0 or later for interactive sessions.

2

Attach IAM Role with Required Permissions

Attach an IAM role to the EC2 instance that includes the `AmazonSSMManagedInstanceCore` managed policy. This policy grants permissions for the agent to communicate with Systems Manager. The policy includes actions like `ssm:UpdateInstanceInformation`, `ssmmessages:CreateControlChannel`, `ssmmessages:CreateDataChannel`, `ec2messages:SendMessage`, etc. Without this role, the instance will not appear in Fleet Manager and sessions cannot be started. Verify the instance is managed by checking in the Systems Manager console under Fleet Manager.

3

Configure IAM Permissions for Users

Create an IAM policy that allows users to start sessions. The minimum required actions are `ssm:StartSession`, `ssm:TerminateSession`, and `ssm:ResumeSession`. Also include `ssm:DescribeSessions` for listing sessions. Attach this policy to IAM users or groups. For enhanced security, use conditions to restrict sessions to specific instances or tags. For example: `"Condition": {"StringEquals": {"ssm:resourceTag/Environment": "Production"}}`. Users also need the Session Manager plugin installed on their local machine to use the CLI.

4

Start a Session via Console or CLI

In the AWS Management Console, navigate to Systems Manager > Session Manager and click 'Start session'. Select the target instance from the list. You will get a browser-based shell. Alternatively, use the AWS CLI: `aws ssm start-session --target i-1234567890abcdef0`. This command opens an interactive session in your terminal. The session is encrypted end-to-end. All keystrokes and output are sent through the Systems Manager service. You can also use port forwarding to access applications: `aws ssm start-session --target i-1234567890abcdef0 --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["80"],"localPortNumber":["8080"]}'`.

5

Enable Session Logging and Auditing

To meet compliance requirements, enable session logging. In Systems Manager Session Manager preferences, you can stream session logs to CloudWatch Logs or S3. You can also encrypt logs with a KMS key. All session activity (commands run, output) is recorded. Additionally, CloudTrail logs API calls like `StartSession` and `TerminateSession`. For granular auditing, use AWS Config rules to ensure instances are configured for Session Manager. Session logs can be used for forensic analysis if a security incident occurs.

What This Looks Like on the Job

Enterprise Scenario 1: Replacing Bastion Hosts

A large financial services company had a fleet of 500 EC2 instances in private subnets across multiple VPCs. Previously, they used a bastion host with SSH keys to access instances. This created a single point of failure, key management overhead, and audit gaps. They migrated to Session Manager by attaching the AmazonSSMManagedInstanceCore role to each instance and creating VPC endpoints for Systems Manager in each VPC. They configured session logging to CloudWatch Logs with a retention policy of 7 years. The result: no more bastion hosts, no SSH keys to rotate, and full audit trails. Performance was acceptable with latency under 200ms for interactive sessions. The main challenge was ensuring all instances had the latest SSM Agent version, which they automated via AWS Systems Manager Patch Manager.

Enterprise Scenario 2: Temporary Vendor Access

A SaaS company needed to grant temporary SSH access to a third-party vendor for troubleshooting an EC2 instance. They used EC2 Instance Connect to push a one-time key. The vendor's IAM user had permissions to call SendSSHPublicKey only for specific instances tagged with VendorAccess=true. The key expired in 60 seconds, so the vendor had to connect immediately. The session was a normal SSH session, but the company could revoke access by removing the IAM permission. This was simpler than managing SSH keys for vendors. However, the instance had a public IP, which was acceptable for this non-production environment. For production, they would use Session Manager instead.

Enterprise Scenario 3: Automated Remediation with Session Manager

A DevOps team used Session Manager to run commands across hundreds of instances for incident response. They used aws ssm send-command to execute scripts (e.g., restart a service, collect logs) without interactive login. This allowed them to automate remediation playbooks. For example, if CPU usage exceeded 90%, a CloudWatch alarm triggered a Lambda function that ran a command via Systems Manager to kill the offending process. This was faster than SSHing into each instance. The commands were logged, providing an immutable record of actions. The team also used Session Manager's port forwarding to access internal web consoles without opening security groups.

How SOA-C02 Actually Tests This

Exactly What SOA-C02 Tests

This topic falls under Domain 4.0: Security, specifically Objective 4.1: 'Implement and manage security and compliance controls.' The exam expects you to:

Compare EC2 Instance Connect and Session Manager.

Identify prerequisites for each.

Troubleshoot common connectivity issues.

Interpret IAM policies and instance profiles.

Know default timers and limits (60-second key validity, 20-minute idle timeout).

Common Wrong Answers

1.

'Session Manager requires a public IP or NAT gateway.' Wrong. Session Manager works in private subnets if VPC endpoints are configured. The outbound connection goes to AWS service endpoints, not the internet.

2.

'EC2 Instance Connect works with RDP.' Wrong. EIC only supports SSH (port 22). For RDP, you need Session Manager with port forwarding or a dedicated document.

3.

'The SSH key used in EC2 Instance Connect is stored permanently on the instance.' Wrong. The key is ephemeral, valid for 60 seconds, and removed after that time or when the connection closes.

4.

'Session Manager supports password authentication.' Wrong. Session Manager uses IAM authentication, not OS passwords. The instance does not need to have SSH enabled.

Specific Numbers and Terms

60 seconds: The validity period of the public key in EC2 Instance Connect.

20 minutes: Default idle timeout for Session Manager sessions.

Port 443: The outbound port used by SSM Agent (HTTPS).

VPC endpoints: com.amazonaws.<region>.ssm, com.amazonaws.<region>.ssmmessages, com.amazonaws.<region>.ec2messages.

Minimum SSM Agent version: 2.3.68.0 for interactive sessions.

IAM managed policy: AmazonSSMManagedInstanceCore.

Edge Cases and Exceptions

If the ec2-instance-connect package is not installed, the SendSSHPublicKey API will succeed but the key will not be placed in authorized_keys. The instance must have the package.

Session Manager sessions can be terminated by an administrator using aws ssm terminate-session. The user gets a 'Session terminated' message.

If the instance is in a private subnet without VPC endpoints, the SSM Agent cannot communicate and the instance will not appear in Fleet Manager.

For EC2 Instance Connect, the instance must have a public IP or be reachable via SSH. If the instance is in a private subnet, you need a bastion host or VPN.

Eliminating Wrong Answers

When you see a question about connecting to an instance without managing SSH keys, the key differentiator is: does the instance need a public IP? If yes, consider EC2 Instance Connect. If no, consider Session Manager. Also, if the question mentions audit logging of commands, Session Manager is the answer because it can log to CloudWatch Logs or S3. If the question mentions a one-time key, it's EC2 Instance Connect.

Key Takeaways

EC2 Instance Connect pushes an SSH public key valid for 60 seconds; the private key is never stored on the instance.

Session Manager does not require a public IP or open inbound ports; it uses outbound HTTPS to Systems Manager endpoints.

For Session Manager in private subnets, create VPC endpoints for ssm, ssmmessages, and ec2messages.

The default idle timeout for Session Manager sessions is 20 minutes.

EC2 Instance Connect only supports SSH (port 22); for RDP, use Session Manager with port forwarding.

Session Manager can log all session commands to CloudWatch Logs or S3 for auditing.

The IAM managed policy for Session Manager is AmazonSSMManagedInstanceCore.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

EC2 Instance Connect

Requires SSH port (22) open inbound

Instance needs public IP or reachable via network

Uses ephemeral SSH keys (valid 60 seconds)

Supports only SSH (no RDP)

No agent required (uses ec2-instance-connect package)

Session Manager

No inbound ports required (outbound HTTPS only)

Works in private subnets with VPC endpoints

Uses IAM authentication, no SSH keys

Supports both SSH and RDP (via port forwarding or documents)

Requires SSM Agent (version >= 2.3.68.0)

Watch Out for These

Mistake

Session Manager requires the instance to have a public IP address.

Correct

Session Manager does not require a public IP. The SSM Agent initiates an outbound connection to AWS Systems Manager endpoints over HTTPS (port 443). If the instance is in a private subnet, you need VPC endpoints for Systems Manager, but no internet gateway or NAT gateway is required.

Mistake

EC2 Instance Connect pushes a password to the instance.

Correct

EC2 Instance Connect pushes an SSH public key, not a password. The public key is appended to the `~/.ssh/authorized_keys` file of the specified OS user. The corresponding private key is used by the SSH client to authenticate.

Mistake

The SSH key used in EC2 Instance Connect is permanent.

Correct

The public key is ephemeral and valid for only 60 seconds. After 60 seconds, it is removed from both the instance metadata service and the `authorized_keys` file. The key is not stored permanently.

Mistake

Session Manager can be used without an IAM role attached to the instance.

Correct

The EC2 instance must have an IAM role that includes the `AmazonSSMManagedInstanceCore` policy (or equivalent). Without this role, the SSM Agent cannot authenticate with the Systems Manager service, and the instance will not appear as a managed instance.

Mistake

EC2 Instance Connect works with Windows instances.

Correct

EC2 Instance Connect only supports SSH, which is not natively available on Windows. It does not support RDP. For Windows instances, use Session Manager with port forwarding or a dedicated RDP session document.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

How do I connect to an EC2 instance in a private subnet without a bastion host?

Use AWS Systems Manager Session Manager. Ensure the instance has an IAM role with the AmazonSSMManagedInstanceCore policy, the SSM Agent is running, and you have created VPC endpoints for Systems Manager (ssm, ssmmessages, ec2messages) in the VPC. Then start a session from the console or CLI. No public IP or bastion host is needed.

What is the difference between EC2 Instance Connect and Session Manager?

EC2 Instance Connect pushes a one-time SSH key to the instance and requires SSH access (port 22) and a public IP or network reachability. Session Manager establishes a secure tunnel via the SSM Agent and does not require any inbound ports or a public IP. Session Manager also supports RDP and command logging, while EC2 Instance Connect only supports SSH.

Can I use EC2 Instance Connect with Windows instances?

No, EC2 Instance Connect only supports SSH, which is not natively available on Windows. For Windows, use Session Manager with port forwarding or the AWS-StartPortForwardingSession document to connect via RDP.

How do I set up Session Manager for an EC2 instance?

Attach an IAM role with the AmazonSSMManagedInstanceCore policy to the instance. Ensure the SSM Agent is installed and running. If the instance is in a private subnet, create VPC endpoints for ssm, ssmmessages, and ec2messages. Then grant users permissions to start sessions (ssm:StartSession). Finally, start a session via the console or CLI.

What happens if the SSM Agent is not running on the instance?

The instance will not appear in Systems Manager Fleet Manager, and you cannot start a session. Check the agent status with `sudo systemctl status amazon-ssm-agent`. If it is not running, start it with `sudo systemctl start amazon-ssm-agent`. Also verify the instance has outbound connectivity to the Systems Manager endpoints.

Can I log all commands executed in a Session Manager session?

Yes, you can enable session logging in Systems Manager Session Manager preferences. You can stream logs to CloudWatch Logs or S3. You can also encrypt logs with a KMS key. This provides an audit trail of all commands run during the session.

How long is the SSH key valid in EC2 Instance Connect?

The public key is valid for 60 seconds. After 60 seconds, it is removed from the instance metadata and the authorized_keys file. You must establish the SSH connection within 60 seconds of calling SendSSHPublicKey.

Terms Worth Knowing

Ready to put this to the test?

You've just covered EC2 Instance Connect and Session Manager — now see how well it sticks with free SOA-C02 practice questions. Full explanations included, no account needed.

Done with this chapter?