|
| 1 | +## Overview |
| 2 | + |
| 3 | +AWS Systems Manager Session Manager is the recommended approach for connecting to bastion instances. It eliminates the need for SSH key pairs and provides better security, auditability, and access control. |
| 4 | + |
| 5 | +### Why Use SSM Instead of Key Pairs? |
| 6 | + |
| 7 | +Traditional SSH key pairs have several drawbacks: |
| 8 | + |
| 9 | +- **Security Risk:** Key pairs are often shared across teams, increasing the attack surface. |
| 10 | +- **Lack of Auditability:** Difficult to track who accessed the bastion and what commands were executed. |
| 11 | +- **Rigid Access Control:** Revoking access requires deleting the entire key pair, affecting all users. |
| 12 | +- **Management Overhead:** AWS doesn't store key pairs after creation - if lost, recovery is impossible. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Before connecting via SSM, ensure the following requirements are met: |
| 17 | + |
| 18 | +### 1. IAM Role Configuration |
| 19 | + |
| 20 | +The EC2 instance must have an IAM instance profile attached with the `AmazonSSMManagedInstanceCore` policy. This allows the SSM agent to communicate with AWS Systems Manager. |
| 21 | + |
| 22 | +### 2. SSM Agent Installation |
| 23 | + |
| 24 | +Ensure the SSM Agent is installed on your bastion host. Most Amazon Machine Images (AMIs) come with it pre-installed. |
| 25 | + |
| 26 | +To verify the SSM Agent status, run: |
| 27 | + |
| 28 | +```bash |
| 29 | +sudo systemctl status amazon-ssm-agent |
| 30 | +``` |
| 31 | +Example: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +### 3. Security Group Configuration |
| 36 | + |
| 37 | +With SSM, you can eliminate all inbound SSH traffic (port 22) in your Security Groups. Only outbound HTTPS (port 443) is required for communication with SSM. |
| 38 | + |
| 39 | +> **Note**!\ |
| 40 | +> This is a significant security improvement as it reduces the attack surface by closing the SSH port entirely. |
| 41 | +
|
| 42 | +## Connecting to the Instance |
| 43 | + |
| 44 | +### Via AWS Console |
| 45 | + |
| 46 | +1. Navigate to the EC2 dashboard |
| 47 | +2. Select your bastion instance |
| 48 | +3. Click **Connect** |
| 49 | +4. Choose the **Session Manager** tab |
| 50 | +5. Click **Connect** |
| 51 | + |
| 52 | +### Via AWS CLI |
| 53 | + |
| 54 | +1. Install the [Session Manager plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html) for AWS CLI |
| 55 | + |
| 56 | +2. Connect using the following command: |
| 57 | + |
| 58 | +```bash |
| 59 | +aws ssm start-session --target {instance-id} |
| 60 | +``` |
| 61 | + |
| 62 | +## Port Forwarding to Private Resources |
| 63 | + |
| 64 | +You can tunnel to private resources (like RDS databases) using SSM port forwarding: |
| 65 | + |
| 66 | +```bash |
| 67 | +aws ssm start-session \ |
| 68 | + --target {instance-id} \ |
| 69 | + --document-name AWS-StartPortForwardingSessionToRemoteHost \ |
| 70 | + --parameters '{"host":["your-rds-endpoint"],"portNumber":["5432"],"localPortNumber":["5432"]}' |
| 71 | +``` |
| 72 | + |
| 73 | +## Benefits of SSM |
| 74 | + |
| 75 | +- **Centralized Access Control:** Manage access via IAM policies - grant/revoke access without touching the instance. |
| 76 | +- **Quick Response:** Immediately terminate all sessions in case of security incidents. |
| 77 | +- **No Public IP Required:** Connect to instances in private subnets via VPC Endpoints. |
| 78 | +- **Full Auditing:** Log every session and command to CloudWatch Logs or S3 for compliance. |
| 79 | + |
| 80 | +## Considerations |
| 81 | + |
| 82 | +- **Latency:** Session Manager tunnels traffic through AWS APIs, which may introduce slight lag compared to direct SSH connections. |
| 83 | +- **Logging Costs:** While SSM is free, storing session logs in CloudWatch or S3 incurs costs. Consider configuring lifecycle rules or retention periods to manage costs. |
| 84 | + |
| 85 | +For more information, refer to the [AWS Session Manager documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html). |
0 commit comments