Connecting to EC2 with Session Manager (No SSH Required)
This note describes how to use Systems Manager - Session Manager.
This note describes how to use Systems Manager - Session Manager.
- 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
To use Session Manager, you need an IAM role with the AmazonSSMManagedInstanceCore policy attached to your 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 using the following command:
aws cloudformation deploy \ --template-file template.yaml \ --stack-name ec2-session-manager \ --capabilities CAPABILITY_NAMED_IAMTesting
To start a session with your instance, run the following command:
aws ssm start-session --target i-xxxxxxxxxxxxxxxxxThe output will indicate a successful login:
Starting session with SessionId: your-session-idsh-4.2$Cleaning Up
Clean up all the AWS resources provisioned during this example with the following command:
aws cloudformation delete-stack --stack-name ec2-session-managerRelated posts
Debugging PHP Remotely on AWS EC2 with PhpStorm and Xdebug
This note describes how to debug PHP remotely using PhpStorm and Xdebug.
Securely Accessing EC2 Windows Instances via SSH Port Forwarding
This note describes how to securely access an EC2 Windows instance in a private subnet through a bastion host in a public subnet.
Running Proxy.py as a Lightweight HTTP Proxy on EC2
This note describes how to set up Proxy.py, a lightweight HTTP proxy server, on an EC2 instance.
Sign in with Slack Using Cognito User Pools and OIDC
This note describes how to federate Cognito user pools with Slack using OIDC to implement "Sign in with Slack".
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.
