How To Access A Private EC2 Instance Using AWS SSM?

How To Access A Private EC2 Instance Using AWS SSM?

In this blog, you will learn how to use the AWS SSM(Systems Manager) session manager to access Private EC2 instances.

AWS Systems Manager Session Manager is a feature that allows you to securely connect to your EC2 instances without needing a public subnet, public IP, or SSH access.

AWS Systems Manager Workflow

The following image shows the private EC2 access workflow using the AWS Systems Manager.

The workflow diagram of the aws systems manager and private ec2 instance

Here is how the session manager works.

  1. The user initiates the SSM session from the local machine CLI. The local machine should have AWS CLI installed with Session Manager Plugin.
  2. The EC2 instance will have the SSM Agent inside it (most of the instances will have the agent by default, if not, need to install manually)
  3. The request will go to the AWS Systems Manager service first and inside the VPC (via VPC endpoints). The the SSM communicates with the SSM Agent present inside the instance.
  4. The EC2 instance sends session data to AWS Systems Manager, which then securely sends it to the local machine. This allows the user to access the EC2 instance without requiring a public IP address or an open SSH port.

Now, let's look at how to configure and set up Session Manager with a practical example.

Follow the steps below, where we create a VPC from scratch and configure all the necessary network settings, IAM roles, and permissions for Session Manager to work from both the AWS Web Console and a user's laptop using the AWS CLI.

Step 1: Create a VPC with Private Subnets.

Navigate to the VPC console and click Create VPC the button to create a new VPC, if you already have a VPC, you can use that as well.

the vpc creation for the ssm workflow

In the VPC creation page, choose the VPC and more option, which will help to create the required configurations in a simple manner.

Give a name for the VPC and choose the VPC CIDR range, for now I have chosen the range of 10.0.0.0/16

selecting vpc name and the cidr value for the vpc creation

Choose the required number of availability zones and public and private subnet counts.

💡
Here, you can see the NAT is enabled, but for SSM to work, NAT is not required, you can keep the value as None.

NAT will help the private network to access the internet without public IP
chossing the number of private and public subnets and its availability zones

You can see the preview diagram on the right side section, which will help to understand the architecture.

the vpc creation preview diagram from the aws console

The VPC creation will take a few minutes to complete.

the vpc components creation list and the success message

We have created two public and private subnets, but we are only going to use the private subnets.

the list of created public and private subnets

For this demo, I will choose the first private subnet which is situated in the us-west-2a region, but you can choose any private subnet.

Navigate to the Route tables of the VPC.

On the Routes tab, we can see one rule, which only helps to route the traffic internally, and the other is the NAT gateway, which is for accessing internet from private instances.

the routes of the private subnets

On the Subnet Associations tab, we can see that the two private subnets are associated.

describing the private subnet route tables to see the subnet associations

Step 2: Create a Private VPC Endpoint

Since we don't have internet access but want to connect the AWS SSM service securely, we need Private VPC Endpoints.

We are creating three endpoints: ssmssmmessages, and ec2messages.

  1. SSM VPC Endpoint: Primary endpoint for the communication, which helps send the commands to the SSM.
  2. SSM Messages VPC Endpoint: This is where you send and receive the communication output between SSM components such as the SSM Agent, Systems Service, etc.
  3. EC2 Messages VPC Endpoint: Send and receive communication between EC2 and other AWS services.

To create a VPC Endpoint, go to the VPC dashboard, navigate to the Endpoints, and click Create Endpoint.

the vpc endpoint creation dashboard

In the Endpoint settings, provide an endpoint name and select the AWS services from the Service category.

providing name and selectin the service type for the endpoint creation

In the Services section, select the service name com.amazonaws.us-east-1.ssm and select the VPC and the private subnets.

select the service and network settings such as vpc and private subnets

In the Security Groups section, I am choosing the default one of the VPC Security groups.

selecting the security group for the vpc endpoint creation

We have to create two more endpoints, same as above, one is SSM Messages.

the endpoint name for the second endpoint which is ssm messages endpoint

The third endpoint is for the EC2 Messages service.

the third endpoint creation for the ec2 messages endpoint

The endpoint creation will take a few seconds to complete after we see the status as active and more details about it on the Details tab.

the endpoints status and describing the more in-depth details

Step 3: Default Security Group

When creating a VPC, a Security group will also be created with a rule for internal routing.

describing the default security group inbound rules to ensure the routes

We can see that only one rule is present in the inbound traffic, which is only for internal incoming traffic.

describing the default security group outbound rules to ensure the routes

The outbound rule indicates that the traffic can go from the VPC to anywhere.

Step 4: Create an Instance Profile (IAM Role)

We need to create an instance profile for the private EC2 instance.

We already have an AWS-managed IAM Policy AmazonSSMManagedInstanceCore for the instance profile with the required permissions.

creating an instance profile for the ec2 instance to configure with the AWS SSM

We are creating an IAM Role with this IAM Policy AmazonSSMManagedInstanceCore

Select the Roles tab in the IAM Dashboard and click Create role to create a new one.

creating role with the aws ssm role

On the next page,

Choose the AWS service under the Trusted entity type. Under use case select EC2.

selecting trust entity for the role to create instance profile

On the next page, we need to select the IAM Policy AmazonSSMManagedInstanceCore to attach with the IAM Role.

selecting the ssm policy to attach with the instance profile

The next step is to provide a name to the Role and ensure the Trust Policy and the IAM Policy that we attach to the Role.

providing name for the role and ensuring the trust policy of the role

Now that the required IAM role is ready, we can check the configurations from the IAM console.

describing the instance profile after creating it

Step 5: Create a Private EC2 instance

We can create a private EC2 instance with the instance profile.

creating a private instance to check the aws systems manager

I am creating an Amazon Linux instance for demo purposes, but you can choose any.

The SSM Agent should be present in the instance, but most AWS instances come with it by default.

providing name and selecting the instance type for the instance creation

Please refer to the official documentation to view the list of preinstalled SSM agent AMIs.

selecting insrtance type and key for the instance creation

In the Network settings section, choose the correct VPC, private subnet, and security group.

selecting the network configurations such as vpc, private subnet and security group
In the Advanced details section, select the IAM Instance Profile that we created earlier.
in the advanced section, selecting the instance profile

Wait until the instance state is running.

describing the private instance to ensure the status

Step: AWS Systems Manager to access the Instance

After the EC2 instance creation is completed, it can be seen in the AWS Systems Manager dashboard under the Fleet Manager section.

systems manager dashboard to see the registered ec2 instance

We can get more detailed information if we click the Node ID.

connecting the ec2 instance using the aws session manager from the systems manager dashboard

To connect the instance via SSM, we can use the Start terminal section from the Node actions or the Session Manager section of the EC2 instance

connecting the ec2 instance using the aws session manager from the ec2 dashboard

Now, we can access the instance without accessing the subnet from the internet.

The access output of the ec2 instance via the systems manager

We haven't provided a key or even SSH access to this instance, so no other methods will work to connect with this private instance.

ec2 instance connection method with the warning message
We can see no public IP warning message in the EC2 Instance Connect method
ssh client connection method with the warning message

In the SSH client connect method, we can see the warning of the no key association.

How do we configure the SSM on the existing EC2 Instance?

If you already have a private EC2 instance but the instance profile was not attached when you created it, we can also attach the instance profile after the deployment.

I have created another Private EC2 instance with no instance profile.

list of ec2 instances in the ec2 dashboard

If we check the Session Manager section, we can't able to connect the instance using the SSM.

session manages section of the instance connection tab

We need to attach the instance profile with this existing Private EC2 instance.

To attach the IAM Instance Profile to an existing cluster, in the instance dashboard, select Security in the Actions tab and select Modify IAM role.

modifying the instance profile of the existing ec2 instance.

On the next page, select the Instance Profile we have already created and click the Update IAM role button.

instance profile selection of the existing ec2 instance.

The associated Instance Profile will take a few minutes to update with the EC2 instance.

After that, if you check the Session Manager tab of the connection dashboard, you will see the connect button.

connecting an existing ec2 instance with the ssm instance profile.

How to access the EC2 instance using AWS SSM from the local machine?

To access the instance from the local machine, need to install the SSM client.

For Mac,

brew install session-manager-plugin

Once the installation is completed, we can connect the instance from the local machine via CLI.

aws ssm start-session --target <INSTANCE_ID> --region <REGION>

We can perform port forwarding if you have installed something in the private instance and want to access its dashboard from the local machine.

For example, I have installed Prometheus in a private EC2 instance, and need to access the dashboard from the local machine.

To see the dashboard from the local machine by port forwarding, use the following command.

aws ssm start-session --target <INSTANCE_ID> --region <REGION> --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["9090"],"localPortNumber":["9090"]}'

The port number will be changed depending on the deployment inside the server.

the aws ssm session created notification on terminal

To access the dashboard, open any browser from the local machine, and paste the URL http://localhost:9090

the prometheus dashboard

Conclusion

We have explored only one use case of the AWS Systems Manager, but we can do various things that make it possible to use it as a secure configuration manager for the EC2 instances and for operation, application, and change management.

Check all the options the SSM provides and utilize them for your needs.

About the author
Arun Lal

Arun Lal

Arun Lal is a DevOps Engineer & AWS Community Builder, also an Expert in AWS infrastructure, Terraform automation, and GitLab CI/CD pipelines.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to DevOpsCube – Easy DevOps, SRE Guides & Reviews.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.