Exploring EC2 Instance Connect for SSH Connectivity Without Public IP
Ashish Kasaudhan
AWS Ambassador & Community Builder, 46x Certification, Cloud-Native & Platform Engineering Expert | Kubernetes, Docker, CI/CD, & Automation Specialist | DevOps, FinOps, SecOps, & GitOps Professional | Multi Cloud Expert.
I am excited to introduce you to the fascinating world of EC2 Instance Connect. In this blog, we will delve into the concept of establishing secure SSH connectivity to Amazon EC2 instances without the need for a public IP address. Join me on this journey as we explore the benefits, implementation considerations, and best practices of leveraging EC2 Instance Connect to enhance security and streamline remote access to your EC2 instances. Get ready to unlock a new level of secure and efficient connectivity in the cloud!
What is EC2 Instance Connect:
EC2 Instance Connect provides a solution by allowing SSH access to EC2 instances without requiring a public IP address. Instead of relying on a public IP, EC2 Instance Connect establishes secure connections through private network paths, ensuring that access to the instance is restricted to authorized users while maintaining a high level of security. This capability is particularly useful for instances located in private subnets or network environments where isolation from the internet is crucial to mitigate potential security risks.
Possible Business Use Case:
A possible business use case for EC2 Instance Connect, where a public IP is not required for SSH connectivity, is in a highly secure environment where instances are isolated within a private network. Here’s how it can benefit businesses:
Environment Details For This Walkthrough
Architecture
Pre-requisites:
IAM Recommendations:
Define Permissions: Start by identifying the specific actions and resources you want to control. For EC2 Instance Connect, the relevant actions include?ec2-instance-connect:SendSSHPublicKey?and?ec2-instance-connect:SendSerialConsoleSSHPublicKey. Determine which instances or instance types should be accessible and limit the actions accordingly.
Specify Resource ARNs: To target specific resources, use Amazon Resource Names (ARNs) in your IAM policy. You can specify instance ARNs to allow access to particular instances or use wildcards to cover multiple instances.
Leverage Condition Keys: Condition keys in IAM policies allow for additional control based on various factors. You can set conditions such as IP address ranges, VPC IDs, instance tags, or IAM user/role attributes to further restrict access to EC2 instances.
Grant IAM Role Permissions: Grant the necessary permissions to IAM roles that users assume to connect to EC2 instances. Use the?ec2-instance-connect:SendSSHPublicKey?action to enable SSH access or the?ec2-instance-connect:SendSerialConsoleSSHPublicKey?action for Serial Console access. Ensure that the role has the appropriate policies attached for EC2 Instance Connect actions.
Restrict Actions and Resources: To restrict access, use the?Deny?effect in your IAM policy. You can explicitly deny certain actions or resources to specific IAM users, roles, or groups. This helps enforce more restrictive access controls and prevents unintended access to EC2 instances.
Test and Verify: Before applying the IAM policy, thoroughly test it to ensure that the desired access restrictions are in place. Use different IAM users or roles to verify that the policy effectively controls access to EC2 Instance Connect.
Let’s Get Started
1- Create EC2 Instance Connect Endpoint
You can refer following link for IAM policy creation.
领英推荐
Refer to the link mentioned for implementation steps.
Connectivity Test
From SSH
You can use SSH to connect to your Linux instance and use the?open-tunnel?command to establish a private tunnel. You can use?open-tunnel?in single-connection or multi-connection mode.
For information about using the AWS CLI to connect to your instance using SSH, see?Connect to your Linux instance using the AWS CLI.
The following examples use?OpenSSH. You can use any other SSH client that supports a proxy mode.
Single connection
To allow only a single connection to an instance using SSH and the?open-tunnel?command
Use?ssh?and the?open-tunnel?AWS CLI command is as follows. The?-o?proxy command encloses the?open-tunnel?the command that creates the private tunnel to the instance.
ssh -i my-key-pair.pem ec2-user@i-0123456789example \
-o ProxyCommand='aws ec2-instance-connect open-tunnel --instance-id i-0123456789example'
For:
Multi-connection
To allow multiple connections to an instance, first run the?open-tunnel?AWS CLI command to start listening for new TCP connections, and then use?ssh?to create a new TCP connection and a private tunnel to your instance.
To allow multiple connections to your instance using SSH and the?open-tunnel?command
2. In a?new terminal window, run the following?ssh?command to create a new TCP connection and a private tunnel to your instance.
ssh -i my-key-pair.pem ubuntu@localhost -p 8888
From Amazon EC2 Console
You can connect to an instance using the Amazon EC2 console by selecting the instance from the console and choosing to connect using EC2 Instance Connect, which handles the permissions and provides a successful connection.
To connect to your instance using the browser-based client from the Amazon EC2 console
Note: For?Max tunnel duration (seconds), enter the maximum allowed duration for the SSH connection.
The duration must comply with the?maxTunnelDuration?condition specified in the IAM policy. If you don't have access to the IAM policy, ask your administrator to verify it. If?maxTunnelDuration?is not specified in the IAM policy, enter the default, which is?3600?seconds (1 hour).
4. A new tab will be opened in your browser and the ssh terminal will be available for your use.
Recommendations for using EC2 Instance Connect
Here are some recommendations for using the EC2 Instance Connect service effectively:
Reference Link
Billing
There is no additional cost for using EIC endpoints. Standard data transfer charges apply
Conclusion
In conclusion, EC2 Instance Connect is a valuable service provided by Amazon Web Services (AWS) that simplifies and secures SSH connectivity to Amazon EC2 instances. By leveraging EC2 Instance Connect, you can establish encrypted SSH connections to your EC2 instances without requiring them to have a public IP address. This enables enhanced security by reducing the attack surface and minimizing exposure to the public internet. To learn more, visit the?EIC Endpoint documentation.
Learning Through Writing Evangelist | Making DevOps work for you
1 年It is worth adding that EC2 Instance Connect Endpoint allows you to connect not only to EC2 instance, but also to any resource within VPC. In order to do this, you should use the --private-ip-address and --remote-port parameters ?? https://www.dhirubhai.net/posts/roman-siewko_aws-awscommunity-awscommunitybuilders-activity-7075536906003845120-VDkv