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.
AWS 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.
Configuration
To use Session Manager, attach an IAM role containing the AmazonSSMManagedInstanceCore policy 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
For details, see the AWS re:Post Knowledge Center article.
Deploy the stack:
aws cloudformation deploy \ --template-file template.yaml \ --stack-name ec2-session-manager \ --capabilities CAPABILITY_NAMED_IAMTesting
Start a session with the instance by running:
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
After attaching the AmazonSSMManagedInstanceCore managed policy to the EC2 instance role, you can open a shell with aws ssm start-session without an SSH key pair or bastion host. No inbound rule on port 22 is required, reducing the number of access credentials and network paths that must be managed compared with SSH-based access.
The instance must be able to reach the ssm, ec2messages, and ssmmessages endpoints. Instances without internet access need the corresponding VPC endpoints before a session can be established.
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
Containerizing a FastAPI backend and deploying it to a single Lambda function with Lambda Web Adapter and AWS CDK.
