CONNECT TO EC2 USING SESSION MANAGER- A SIMPLE GUIDE
Tags: aws, ec2, beginners, cloud
Published: 2023-08-16 22:26:55

To be able to connect to ec2 instance using session manager, SSM agent must be installed on this instance.

## some AMIs come with SSM agent already preinstalled:
Amazon Linux Base AMIs dated 2017.09 and later
Amazon Linux 2
Amazon Linux 2 ECS-Optimized Base AMIs
Amazon Linux 2023 (AL2023)
Amazon EKS-Optimized Amazon Linux AMIs
macOS 10.14.x (Mojave), 10.15.x (Catalina), and 11.x (Big Sur)
SUSE Linux Enterprise Server (SLES) 12 and 15
Ubuntu Server 16.04, 18.04, 20.04, and 22.04
Windows Server 2008-2012 R2 AMIs published in November 2016 or later
Windows Server 2016, 2019, and 2022

## you can also check these guides to install SSM agent on your instance:
- For macOS
https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-manual-agent-install-macos2.html

-For win server:
https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-win.html

-For Linux :
https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-manual-agent-install.html

---
<u>-first you need to create a role to enable ec2 to use AWS Systems Manager service core functionality.</u>

-Navigate to IAM, Roles then create role

![create role step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4w3bz7m47k6d33o9uv2.jpg)

-select EC2 then Next

![create role step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yseswbnz1dx1ohmostwe.jpg)

-search for AmazonSSMManagedInstanceCore policy ancd check it then Next

![create role step 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ju2inggfv72o60hmvtw.jpg)

- enter the role name then Next

![create role step 4.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwdr4ruhr37r9w77x24y.jpg)

![create role step 4.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5awjr2rrzjpoqko7x6b3.jpg)

role is now created

![role created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u22cb13l96vyixfg31ax.jpg)

-when launching EC2 instance at the end of the page click Advanced details

![EC2 launch 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7nf5a3tm0d647ce0chg.jpg)

-in advanced details select IAM instance profile and choose the role you created

![EC2 launch 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyv7x3uh356u2gv8sfzb.jpg)

-Navigate to EC2 ,instances. then check your EC2 instance and click connect

![connect 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqtii7rmldk5xb1tr68j.jpg)

-choose Session Manager then Connect

![connect 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46bdjayu26x7qkyri53q.jpg)

-you are now connected to your EC2 instance using session manager

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hsa6yepbytvvpztl65n.jpg)

**_if the EC2 instance was already created you have to modify its IAM role_**

-Navigate to EC2, Instances and click on your EC2 intance , Actions , Security then Modify IAm role

![EC2 already created 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbyl35egifxt24esvm1c.jpg)

-after that choose the role you created and click Update IAM role

![EC2 already created 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prb9ggr21u6htz3gb4hn.jpg)

_connecting to EC2 using session manager is more secure than using SSH as session manager doesnt need an open port to connect_