Connecting to EC2 with Session Manager (No SSH Required)

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.

Takahiro Iwasa
2 min read

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.

Session Manager connection flow from the AWS CLI to the SSM Agent on an EC2 instance

Configuration

To use Session Manager, attach an IAM role containing the AmazonSSMManagedInstanceCore policy to the EC2 instance (line 30).

template.yaml
AWSTemplateFormatVersion: 2010-09-09
Resources:
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: 443
ℹ️ Note

If your EC2 instances are in private subnets, set up the following VPC endpoints:

  • com.amazonaws.region.ssm
  • com.amazonaws.region.ec2messages
  • com.amazonaws.region.ssmmessages

For details, see the AWS re:Post Knowledge Center article.

Deploy the stack:

Terminal window
aws cloudformation deploy \
--template-file template.yaml \
--stack-name ec2-session-manager \
--capabilities CAPABILITY_NAMED_IAM

Testing

Start a session with the instance by running:

Terminal window
aws ssm start-session --target i-xxxxxxxxxxxxxxxxx

The output will indicate a successful login:

Starting session with SessionId: your-session-id
sh-4.2$

Cleaning Up

Delete the stack to remove the provisioned resources:

Terminal window
aws cloudformation delete-stack --stack-name ec2-session-manager

Conclusion

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.

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.