Connecting to EC2 with Session Manager (No SSH Required)
Opening a shell on an EC2 instance through Systems Manager's Session Manager, with no SSH keys, bastion host, or open port 22 required.
Systems Manager - Session Manager provides shell access to EC2 instances without SSH:
- No SSH keys: Eliminates the need to manage and secure SSH keys.
- No bastion hosts: Removes the requirement for intermediary servers to access EC2 instances.
- No inbound rules on port 22: Improves security by avoiding open ports in your security group.
Building
Session Manager requires an IAM role with the AmazonSSMManagedInstanceCore policy attached to the EC2 instance (line 30).
AWSTemplateFormatVersion: 2010-09-09Resources: EC2: Type: AWS::EC2::Instance Properties: IamInstanceProfile: !Ref InstanceProfile ImageId: ami-0f310fced6141e627 InstanceType: t3.small SecurityGroups: - !Ref SecurityGroup
InstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Path: / Roles: - !Ref IamRole
IamRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: ec2.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore RoleName: ec2-role
SecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Example GroupName: ec2-security-group SecurityGroupIngress: - CidrIp: 0.0.0.0/0 FromPort: 443 IpProtocol: tcp ToPort: 443If your EC2 instances are in private subnets, set up the following VPC endpoints:
com.amazonaws.region.ssmcom.amazonaws.region.ec2messagescom.amazonaws.region.ssmmessages
Refer to the official knowledge for more details.
Deploy the stack:
aws cloudformation deploy \ --template-file template.yaml \ --stack-name ec2-session-manager \ --capabilities CAPABILITY_NAMED_IAMTesting
A session with the instance can be started with:
aws ssm start-session --target i-xxxxxxxxxxxxxxxxxThe output will indicate a successful login:
Starting session with SessionId: your-session-idsh-4.2$Cleaning Up
Delete the stack to remove the provisioned resources:
aws cloudformation delete-stack --stack-name ec2-session-managerConclusion
Attaching the AmazonSSMManagedInstanceCore managed policy to the EC2 instance role was enough to open a shell session with aws ssm start-session, with no SSH key pair or bastion host involved. That policy attachment is really the entire setup — the security group in this example only needs to allow outbound HTTPS rather than any inbound rule on port 22 — which is a meaningful reduction in what has to be managed and secured compared to a traditional SSH-based access pattern. The one prerequisite worth planning for is network reachability to the ssm, ec2messages, and ssmmessages endpoints: public subnets reach them without extra configuration, but private subnets need the corresponding VPC endpoints in place before the session will succeed.
Related posts
Debugging PHP Remotely on AWS EC2 with PhpStorm and Xdebug
Remote debugging a PHP application on EC2 with PhpStorm and Xdebug, from the server-side ini settings to the IDE's path mapping.
Securely Accessing EC2 Windows Instances via SSH Port Forwarding
Reaching an EC2 Windows instance in a private subnet through an SSH bastion host, keeping RDP off the public internet entirely.
Running Proxy.py as a Lightweight HTTP Proxy on EC2
Running Proxy.py on an EC2 instance and reaching it safely through an SSH tunnel, since the proxy has no authentication of its own.
Sign in with Slack Using Cognito User Pools and OIDC
Federating Cognito user pools with Slack over OIDC and wiring "Sign in with Slack" into a Next.js app with Amplify.
Deploying FastAPI on AWS Lambda with Lambda Web Adapter
This example guides you through the process of developing API backends with FastAPI using Lambda Web Adapter.
