How to Configure Static Outbound IPs for AWS Auto Scaling Groups with NAT Gateway or Proxy


1 views

When integrating with third-party services that require IP whitelisting, AWS Auto Scaling presents a unique challenge. Traditional Elastic IPs (EIPs) aren't directly compatible with dynamic scaling because:

  • EIPs can only be assigned to individual EC2 instances
  • Auto Scaling groups dynamically create/destroy instances
  • Manual EIP assignment defeats the purpose of auto scaling

The most robust solution is using a NAT Gateway in a public subnet:


# Terraform example for NAT Gateway setup
resource "aws_eip" "nat" {
  vpc = true
}

resource "aws_nat_gateway" "gw" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.public.id
}

resource "aws_route_table" "private" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block     = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.gw.id
  }
}

This configuration ensures:

  1. All outbound traffic from private subnets routes through the NAT
  2. The NAT Gateway maintains a static Elastic IP
  3. Auto Scaling works independently in private subnets

For legacy environments or specific protocols:


# Python proxy server example using Squid
import boto3

ec2 = boto3.client('ec2')
response = ec2.allocate_address(Domain='vpc')
allocation_id = response['AllocationId']

# Attach to proxy instance
ec2.associate_address(
    InstanceId='i-1234567890abcdef0',
    AllocationId=allocation_id
)

Configuration steps:

  • Deploy a dedicated proxy instance with EIP
  • Configure security groups to allow traffic from app instances
  • Set HTTP_PROXY environment variables on app servers
Solution Cost Factor Throughput
NAT Gateway $0.045/hour + data processing Up to 45 Gbps
Proxy Server EC2 instance cost only Instance-type dependent

When implementing these solutions, watch for:

  • Security group rules blocking proxy/NAT traffic
  • Route table misconfigurations in VPC
  • DNS resolution problems in private subnets

When dealing with third-party services that require IP whitelisting, AWS Auto Scaling Groups present a unique challenge. Each EC2 instance launched by the Auto Scaling group gets assigned a dynamic public IP by default, making it impossible to maintain a consistent allowlist.

There are three primary approaches to solve this:

1. Assign Elastic IPs to each instance (not recommended for Auto Scaling)
2. Route traffic through a NAT Gateway
3. Use a proxy instance with Elastic IP

For production environments, NAT Gateway is the most reliable solution. Here's how to implement it:

# Create NAT Gateway in public subnet
aws ec2 create-nat-gateway \
    --subnet-id subnet-123456 \
    --allocation-id eipalloc-123456

Then update your route table:

# Update private subnet route table
aws ec2 create-route \
    --route-table-id rtb-123456 \
    --destination-cidr-block 0.0.0.0/0 \
    --nat-gateway-id nat-123456

For smaller deployments, you can configure a proxy instance:

# iptables configuration for proxy
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1

Then configure your Auto Scaling instances to route through it:

# Route all traffic through proxy
ip route add default via 10.0.1.5 dev eth0

For Elastic Beanstalk environments, add this to your .ebextensions:

Resources:
  AWSEBAutoScalingGroup:
    Type: "AWS::AutoScaling::AutoScalingGroup"
    Properties:
      VPCZoneIdentifier:
        - "subnet-123456"  # Your private subnet

Remember to:

  • Place NAT Gateway in public subnet
  • Configure proper security group rules
  • Monitor NAT Gateway metrics
  • Consider VPC endpoints for AWS services