Connect to an Amazon EC2 instance by using Session Manager - AWS Prescriptive Guidance

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

Architecture

Target technology stack

  • Session Manager

  • Amazon EC2

  • CloudWatch Logs

Target architecture

Session Manager connects to an EC2 instance and sends log data to CloudWatch Logs or an S3 bucket.
  1. The user authenticates their identity and credentials through IAM.

  2. The user initiates an SSH session through Session Manager and sends API calls to the EC2 instance.

  3. The AWS Systems Manager SSM Agent, which is installed on the EC2 instance, connects to Session Manager and runs the commands.

  4. 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

TaskDescriptionSkills 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:

  1. For the AWS service, choose EC2.

  2. For Permissions Policies, choose AmazonSSMManagedInstanceCore.

  3. In Role Name, enter EC2_SSM_Role.

AWS systems administrator

Create the EC2 instance.

  1. Create the EC2 instance. Follow the instructions in Launch an instance (Amazon EC2 documentation) and note the following:

    1. In the Name and tags section, choose Add additional tags. In Key, enter Name, and in Value, enter Production_Server_One.

    2. Choose an Amazon Linux AMI that has the SSM Agent preinstalled. For a complete list, see AMIs with SSM Agent preinstalled (Systems Manager documentation).

    3. In the Advanced details section, in IAM instance profile, choose EC2_SSM_Role.

  2. Open the Systems Manager console at https://console.aws.amazon.com/systems-manager/.

  3. In the navigation pane, choose Fleet Manager.

  4. Verify that the instance appears in the list of managed nodes.

AWS systems administrator

Set up logging.

  1. Create a log group in CloudWatch Logs. Follow the instructions in Create a log group (CloudWatch Logs documentation). Name the new log group SessionManager.

  2. Configure logging for Session Manager. Follow the instructions in Logging session data using Amazon CloudWatch Logs (Systems Manager documentation) and note the following:

    1. Don’t select Allow only encrypted CloudWatch log groups.

    2. In Choose a log group from the list, choose SessionManager.

AWS systems administrator
TaskDescriptionSkills required

Connect to the EC2 instance.

  1. Start a session in the Systems Manager console. For instructions, see Start a session (Systems Manager documentation). For Target instances, choose the option button to the left of the Production_Server_One instance.

  2. After the connection is made, run several bash commands.

  3. In the Systems Manager console, end the session. For instructions, see End a session (Systems Manager documentation).

AWS systems administrator

Validate logging.

  1. In CloudWatch Logs, open the log stream for the log group. For instructions, see View log data (CloudWatch Logs documentation).

  2. In the log data, confirm that the commands you ran in the previous story are listed.

AWS systems administrator

Troubleshooting

IssueSolution

IAM issues

For support, see Troubleshooting (IAM documentation).

Related resources