Easily Provide Remote Access To EC2 Instance Using AWS Session Manager

  • Wed Sep 11 2024
  • Sibtay Shah

In the current cloud-based as well as traditional environments, securely managing remote access to servers is of paramount importance. Traditional methods work, but they often have complexities that can lead to security vulnerabilities and operational nightmares.

The approach presented in this article focuses on a simplified method to setup secure and fully auditable remote access to your ec2 instances.

It utilizes the Session Manager; a feature of AWS Systems Manager to provide remote access. By using this method, bastion hosts, security group rules management, and SSH key handling are eliminated, resulting in a more efficient and secure solution

Introducing AWS Session Manager

The AWS Session Manager service is part of the AWS Systems Manager and allows users to securely connect to EC2 instances without requiring SSH keys or opening inbound ports. This feature makes access management easier, scalable and thus improves security.

Benefits of Using AWS Session Manager

  • There is no need for bastion hosts
  • Security group rules management is not required
  • Access control is done via IAM policies
  • Full session logging for auditing purposes

Managing access across your entire AWS Organization can be centralized with this solution, which is particularly beneficial for organizations with multiple AWS accounts and/or regions.

Step-by-Step Guide to Setting Up AWS Session Manager

Launching Your EC2 Instance

To start using AWS Session Manager, the first step is to launch an EC2 instance. Here’s how:

  1. Select an Amazon Linux AMI that includes the SSM agent to launch an EC2 instance. As promised, you don't have to manage any keys with Session Manager, so do not select any key pair

2. Select a private subnet to emphasize the importance of isolation. Create a new security group without any inbound rules. (Another promise kept: No security group rules handling required)

Once the instance is launched, it will take some time to become available

Configuring IAM Role for Session Manager

For Session Manager to work, it is necessary to link an IAM role to your EC2 instance. This role grants the necessary permissions for Systems Manager agent running on the instance to perform tasks.

1. Create a new IAM role for the EC2 instance.

2. Attach the Amazon SSM Managed Instance Core policy to this role.

3. Assign the role to your launched instance.

After a few minutes, your instance should register with Systems Manager, allowing you to connect using Session Manager.

Enabling Logging

To enable session logging:

1. Go to the Systems Manager console.

2. Under Node Management, select Session Manager.

3. Go to the Preferences tab and configure CloudWatch logging or S3 logging.

Create a log group for CloudWatch and in CloudWatch logging select the log group you created.

Don’t forget to grant the EC2 instance permissions to push logs to CloudWatch. This involves adding an inline policy that allows the role to put log events into the specified log group.

Granting Permissions to Your EC2 Instance

Open the security section on ec2 and look for the role assigned to that instance. Click on the role to add permissions

To push logs to CloudWatch, the instance role must have permission to do so. Provide the following permissions to the role associated with the instance:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "Logs: DescribeLogStreams"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:DescribeLogGroups"
            ],
            "Resource": "*"
        }
    ]
}

After adding these permissions, wait for few seconds to allow IAM to propagate the changes. Then connect to the instance and run some commands.

After that go to the CloudWatch log group that you setup earlier for session logging. There you should see the commands you just executed in the most recent log stream

Scaling The Solution

As your organization grows, it can become difficult to manage individual IAM roles and permissions for each and every ec2 instance.To streamline the process, AWS provides default host monitoring options:

1. Enable default host monitoring from the Fleet Manager.

2. Select the default IAM role for host management.

This allows you to launch new instances without needing to attach roles manually since it ensures that all new instances inherit the necessary permissions for Session Manager access automatically thus allowing you to scale while reducing the operational overhead

Multi-Account Management with Quick Setup

For organizations using multiple AWS accounts, managing access can become increasingly complex. However, AWS provides a Quick Setup feature that simplifies this process:

1. Access the management account in the AWS Organizations console.

2. Navigate to Fleet Manager and select Account Management.

3. Use the Quick Setup to enable default host management across all accounts.

By implementing this setup, you can ensure that all instances across all accounts in your organization are configured for secure access through Session Manager, enhancing both security and manageability.

Conclusion

Using AWS Session Manager for provisioning and managing remote access to your ec2 instances is an excellent solution that is more secure, scalable and manageable compared to other alternatives.

Need help with setting the Session Manager for your organization? Reach out to us for this or any other AWS needs you might have and work with an AWS Certified Architect to address your needs. Contact us