Connect to an Amazon EC2 instance by using Session Manager
Created by Jason Cornick (AWS), Abhishek Bastikoppa (AWS), and Yaniv Ron (AWS)
Summary
This pattern describes how to connect to an Amazon Elastic Compute Cloud (Amazon EC2) instance by using the Session Manager, a capability of AWS Systems Manager. Using this pattern, you can run bash commands on an EC2 instance through a web browser. Session Manager doesn’t require that you open inbound ports and doesn’t require public IP addresses for EC2 instances. Additionally, it removes the need to maintain bastion hosts with different Secure Shell (SSH) keys. You can govern access to Session Manager with AWS Identity and Access Management (IAM) policies and configure logging, which records important information, such as instance access and actions.
In this pattern, you configure an IAM role and associate it to a Linux EC2 instance that you provision by using an Amazon Machine Image (AMI). You then configure logging in Amazon CloudWatch Logs and use Session Manager to start a session with the instance.
Although this pattern connects to a Linux EC2 instance in the Amazon Web Services (AWS) Cloud, you could use this approach to use Session Manager for connections with other servers, such as on-premises servers or other virtual machines.
Prerequisites and limitations
Prerequisites
An active AWS account.
Permissions to access the managed node. For instructions, see Control user session access to managed nodes.
VPC endpoints for
ssm
,ec2
,ec2messages
,ssmmessages
, ands3
. For instructions, see Create VPC endpoints in the Systems Manager documentation.
Architecture
Target technology stack
Session Manager
Amazon EC2
CloudWatch Logs
Target architecture
The user authenticates their identity and credentials through IAM.
The user initiates an SSH session through Session Manager and sends API calls to the EC2 instance.
The AWS Systems Manager SSM Agent, which is installed on the EC2 instance, connects to Session Manager and runs the commands.
For auditing and monitoring purposes, Session Manager sends the logging data to CloudWatch Logs. Alternatively, you can send log data to an Amazon Simple Storage Service (Amazon S3) bucket. For more information, see Logging session data using Amazon S3 (Systems Manager documentation).
Tools
AWS services
Amazon CloudWatch Logs helps you centralize the logs from all your systems, applications, and AWS services so you can monitor them and archive them securely.
Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the AWS Cloud. You can launch as many virtual servers as you need and quickly scale them up or down. This pattern uses an Amazon Machine Image (AMI) to provision a Linux EC2 instance.
AWS Identity and Access Management (IAM) helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them.
AWS Systems Manager helps you manage your applications and infrastructure running in the AWS Cloud. It simplifies application and resource management, shortens the time to detect and resolve operational problems, and helps you manage your AWS resources securely at scale. This pattern uses Session Manager, a capability of Systems Manager.
Best practices
We recommend that you read more about the security pillar of the AWS Well-Architected Framework and explore encryption options and apply the security recommendations in Setting up Session Manager (Systems Manager documentation).
Epics
Task | Description | Skills required |
---|---|---|
Create the IAM role. | Create the IAM role for the SSM Agent. Follow the instructions in Creating a role for an AWS service (IAM documentation) and note the following:
| AWS systems administrator |
Create the EC2 instance. |
| AWS systems administrator |
Set up logging. |
| AWS systems administrator |
Task | Description | Skills required |
---|---|---|
Connect to the EC2 instance. |
| AWS systems administrator |
Validate logging. |
| AWS systems administrator |
Troubleshooting
Issue | Solution |
---|---|
IAM issues | For support, see Troubleshooting (IAM documentation). |
Related resources
Complete Session Manager prerequisites (Systems Manager documentation)
Designing and implementing logging and monitoring with Amazon CloudWatch (AWS Prescriptive Guidance)