Skip to content

Restrict AWS SSM connection to ec2 instance from OpenVPN server

1
Scenario: I need to access an ec2 instance using aws ssm but when connected to the openvpn.

1. I have attached the AmazonSSMManagedInstanceCore policy to the EC2.
2. Created a policy for ssm restrict access.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "ssm:StartSession",
                "ssm:ResumeSession"
            ],
            "Resource": [
                "arn:aws:ec2:REGION:ACCOUNT_ID:instance/INSTANCE_ID"
            ],
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": [
                        "VPN_IP_1/32",
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": "ssm:TerminateSession",
            "Resource": [
                "arn:aws:ssm:::session/${aws:userid}-*"
            ]
        }
    ]
}

Issue: The traffic originating from your my local IP is not my OpenVPN IP address which suggests that the split tunneling is implemented. Routing need to be setup for OpenVPN for the particular traffic.

As the issue is that traffic to AWS SSM is not routing through the OpenVPN 
Is this good approach to add in server.conf of openvpn?

# Add routes for AWS SSM endpoints
push "route ssm.us-east-1.amazonaws.com 255.255.255.255"
push "route ssmmessages.us-east-1.amazonaws.com 255.255.255.255"
push "route ec2messages.us-east-1.amazonaws.com 255.255.255.255"
( or )
# For VPC Endpoints (these will be resolved through DNS)
push "route vpce-id.ec2messages.region.vpce.amazonaws.com 255.255.255.255

-------------------------------------------------------------------------------------------------------------------
For more information:
Current Architecture:
EC2 instance is in a new VPC (VPC-A with CIDR 10.x.0.0/16)
OpenVPN server is in a different VPC (same region)
EC2 is intentionally isolated in new VPC for security (no test/prod env access)

What I've Done:
EC2 Configuration:

Added local route for EIP: ip route add local EIP-address dev lo [There was a connection issue from the EC2 to itself (curl timeouts) was resolved by adding a local route]

VPC Endpoint Setup:
Created all three endpoints (ssm, ssmmessages, ec2messages) in EC2's VPC
All endpoints show "Available" status
Security group allows port 443 from VPC CIDR (10.x.0.0/16)
Enabled DNS hostnames and DNS resolution in VPC
My OpenVPN server allocates private IPs to users in the 10.x.1.0/24 range, rather than using the VPN server's public IP. My public ip doesn't change even after connecting to the vpn.
OpenVPN Configuration:
Added push "dhcp-option DNS 10.x.0.2" for VPC DNS resolver
Added push "route 10.x.0.0 255.255.0.0" for VPC routing
Already had push "route EIP-address 255.255.255.255" which enables access to server URL [The connection to this ec2 should be via its EIP]
IAM Setup:
Created new IAM group
Attached the restrictive policy to user instead of EC2
User is member of this

asked 2 years ago337 views

1 Answer
0

Hi Siva,

Thank you for reaching out! Let's dive into your scenario and work toward a clear, actionable solution. 😊


Clarifying the Issue

The goal is to access an EC2 instance using AWS SSM, but only when connected through an OpenVPN server. Currently, traffic to AWS SSM is not routing correctly through the OpenVPN server due to split tunneling. Additionally, the EC2 instance resides in a separate, isolated VPC, and you've implemented restrictive IAM policies to limit access. You’re also considering whether modifications to the OpenVPN server configuration can address the routing issue effectively.


Key Terms

  • AWS SSM (Systems Manager): A service that allows secure instance management without requiring direct SSH access.
  • OpenVPN: A VPN solution that provides secure point-to-point connections.
  • VPN (Virtual Private Network): A private network that encrypts and securely routes traffic over the internet, often used to connect remote users or devices to a private network.
  • VPC (Virtual Private Cloud): A logically isolated section of AWS where you can launch and manage AWS resources securely in a virtual network.
  • VPC Endpoint: Enables private connections to AWS services from within a VPC.
  • Split Tunneling: A VPN configuration where only specific traffic is routed through the VPN, while other traffic bypasses it and goes directly to the internet. This is useful for reducing VPN bandwidth usage but can lead to routing issues if not configured correctly.

The Solution (Our Recipe)

Steps at a Glance:

  1. Update the OpenVPN server configuration to push AWS SSM endpoint routes.
  2. Test the DNS resolution for VPC endpoints from the OpenVPN server.
  3. Verify IAM policy effectiveness and ensure proper IP-based restrictions.
  4. Validate routing and connectivity between OpenVPN and EC2 via SSM.

Step-by-Step Guide:

  1. Update the OpenVPN server configuration to push AWS SSM endpoint routes:
    • Modify the OpenVPN server's server.conf to include the necessary routes for AWS SSM. For example:

      push "route ssm.us-east-1.amazonaws.com 255.255.255.255"
      push "route ssmmessages.us-east-1.amazonaws.com 255.255.255.255"
      push "route ec2messages.us-east-1.amazonaws.com 255.255.255.255"
      

      Alternatively, if using VPC endpoints, push the private endpoint routes:

      push "route vpce-id.ec2messages.region.vpce.amazonaws.com 255.255.255.255"
      
    • Restart the OpenVPN service to apply changes:

      sudo systemctl restart openvpn
      

  1. Test the DNS resolution for VPC endpoints from the OpenVPN server:

    • Verify that your OpenVPN server can resolve private DNS names for the VPC endpoints. Use the following command:

      nslookup ssmmessages.region.amazonaws.com
      
    • Ensure the DNS resolver points to the VPC’s DNS server (e.g., 10.x.0.2).

  2. Verify IAM policy effectiveness and ensure proper IP-based restrictions:

    • Confirm that the policy applied to your IAM user or group includes the following conditions:

      "Condition": {
          "NotIpAddress": {
              "aws:SourceIp": ["VPN_IP_1/32"]
          }
      }
    • Test the policy using AWS CLI:

      aws ssm start-session --target <instance-id>
      

      The session should succeed only when connected through the VPN.


  1. Validate routing and connectivity between OpenVPN and EC2 via SSM:
    • Ensure the OpenVPN private IP range (e.g., 10.x.1.0/24) is allowed in the EC2 security group.

    • Test SSM connectivity using the following command:

      aws ssm start-session --target <instance-id>
      
    • Verify traffic routes through the OpenVPN tunnel by checking the traceroute output:

      traceroute ssm.us-east-1.amazonaws.com
      

Closing Thoughts

With these steps, you can ensure secure and reliable SSM access to your EC2 instance via OpenVPN. By leveraging VPC endpoints, restrictive IAM policies, and proper routing configurations, you maintain strong security while enabling the required access. Let us know how it goes, and feel free to reach out for further guidance! 🌟


Good luck, Siva! You're doing great in creating a secure and well-structured setup. Keep up the fantastic work! 🚀✨


Cheers, Aaron 😊

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.