Connect to AWS EC2 Using AWS SSM Session Manager | Secure your EC2 by Enabling AWS SSM | AWS Demo
Table of Contents
Introduction
This tutorial will guide you through the process of connecting to an AWS EC2 instance using the AWS Systems Manager (SSM) Session Manager. This method allows you to securely access your EC2 instances in a private subnet without needing to use SSH over the internet. By following these steps, you can enhance the security of your EC2 instances and streamline your workflow.
Step 1: Create an IAM Role for the EC2 Instance
To enable SSM access, you need to create an IAM role that your EC2 instance will assume.
-
Open the IAM Console:
- Navigate to the IAM Management Console in AWS.
-
Create a New Role:
- Click on "Roles" in the sidebar.
- Select "Create role."
-
Select EC2 as the Trusted Entity:
- Choose "AWS service" and select "EC2" as the use case.
-
Attach Policies:
- In the permissions policies list, search for and select the
AmazonSSMManagedInstanceCore
policy. This policy allows SSM to manage the instance.
- In the permissions policies list, search for and select the
-
Configure Role Name and Tags:
- Provide a name for the role (e.g.,
SSM-EC2-Role
). - Optionally add tags for organization.
- Provide a name for the role (e.g.,
-
Create the Role:
- Review your settings and click "Create role."
Step 2: Attach the IAM Role to Your EC2 Instance
Now that you have created the IAM role, you need to attach it to your EC2 instance.
-
Open the EC2 Console:
- Navigate to the EC2 Management Console.
-
Select Your Instance:
- Locate and select the EC2 instance you want to connect to.
-
Modify IAM Role:
- Click on "Actions," then "Security," and select "Modify IAM role."
- Choose the role you created in Step 1 (
SSM-EC2-Role
) from the dropdown.
-
Apply Changes:
- Click on "Update IAM role" to apply the changes.
Step 3: Verify SSM Agent Installation
Ensure that the SSM Agent is installed and running on your EC2 instance.
-
Connect to Your Instance (if necessary):
- You may need to temporarily connect to your instance using SSH or another method.
-
Check SSM Agent:
- Run the following command to check the status:
sudo systemctl status amazon-ssm-agent
- If it’s not installed, you can find installation instructions in the AWS documentation.
- Run the following command to check the status:
-
Start the Agent (if needed):
- If the agent is not running, you can start it using:
sudo systemctl start amazon-ssm-agent
- If the agent is not running, you can start it using:
Step 4: Connect to Your EC2 Instance Using Session Manager
With the setup complete, you can now connect to your EC2 instance.
-
Open the Systems Manager Console:
- Navigate to the AWS Systems Manager console.
-
Access Session Manager:
- In the left navigation pane, click on "Session Manager."
-
Start a Session:
- Click on "Start session."
- Select the instance you want to connect to from the list.
-
Initiate Connection:
- Click on "Start session" to connect to your instance.
Conclusion
By following these steps, you have successfully connected to your AWS EC2 instance using AWS SSM Session Manager. This method enhances security by eliminating the need for SSH over the internet. Moving forward, consider exploring other features of AWS Systems Manager to further manage and automate your cloud infrastructure.