Monday, August 11, 2025

Launching Lambda in the VPC | A Deep Dive.

 

Here’s twtech overview of launching AWS Lambda in a VPC (Virtual Private Cloud) so you understand both how it works and the caveats.

1. The concept: Why Launch Lambda in a VPC

By default, a Lambda function runs outside twtech VPC in an AWS-managed network with:

  • Internet access by default.
  • Access to AWS services via the public endpoints.

twtech puts Lambda inside a VPC when it needs:

  • Access to private resources (databases, EC2 instances, internal APIs) inside subnets.
  • Tighter network security controls.
  • Private connectivity to AWS services via VPC endpoints.

2. How It Works

How twtech attaches Lambda to a VPC:

  1.  twtech chooses VPC, subnet(s), and security group(s).
  2. AWS provisions an Elastic Network Interface (ENI) in each selected subnet.
  3. The ENI connects Lambda’s execution environment into your VPC’s private network.
  4. twtech Lambda now obeys the routing rules of that subnet.

3. Key Networking Rules

  • Internet access changes:
    • Public subnets with an Internet Gateway → Lambda can still access the internet (if security group and NACL allow).
    • Private subnets → No internet access unless twtech adds a NAT Gateway or NAT instance.
  • Service access:
    • If twtech wants to call AWS services privately, it will use VPC endpoints (Gateway or Interface endpoints).
  • Security:
    • Lambda inherits the subnet’s NACL rules and the attached security group.
    • twtech can control inbound/outbound traffic just like an EC2 instance.

4. Steps to Launch Lambda in a VPC

  1. Create or identify a VPC
    • With the right CIDR block (e.g., 10.0.0.0/16).
  2. Create subnets
    • Private subnets for internal services.
    • Optional public subnet if Lambda needs internet.
  3. Set up routing
    • NAT Gateway for private subnets needing outbound internet.
    • Internet Gateway for public subnets.
  4. Create a security group
    • Allow necessary inbound/outbound traffic (e.g., DB port 3306 for MySQL).
  5. Attach VPC config to Lambda
    • In Lambda console → Configuration → VPC → select VPC, subnets, security groups.
  6. Deploy Lambda code.
  7. Test connectivity to internal resources (RDS, EC2, etc.).

5. Execution Flow

Example: Lambda inside VPC calling RDS MySQL

  1. Request triggers Lambda.
  2. Lambda runs in a VPC-enabled ENI in your subnet.
  3. Outbound packets follow subnet’s route table.
  4. RDS endpoint resolves to a private IP (inside VPC).
  5. Security group rules on Lambda and RDS allow the connection.

6. Benefits

  • Access to private resources.
  • Network control via SGs, NACLs, and route tables.
  • Private access to AWS services via VPC endpoints.
  • Compliance/security requirements.

7. Drawbacks

  • Cold start latency increases (ENI attachment can take time).
  • Extra cost for NAT Gateway if twtech needs internet from private subnet.
  • Complexity in managing subnets, routes, and SGs.
  • Must handle DNS resolution carefully if using private endpoints.

8. Best Practices

  • Use multiple subnets across AZs for high availability.
  • Keep Lambda in private subnets unless it must be in public.
  • Use VPC endpoints instead of NAT for AWS service calls — reduces cost and latency.
  • Monitor ENI usage — Lambda will create one per subnet per security group combination.
  • Use Provisioned Concurrency to reduce VPC cold start impact.



No comments:

Post a Comment

AWS DynamoDB | Read/Write Capacity Modes.

  In Amazon DynamoDB, Read/Write Capacity Mode s determine how twtech pays for throughput and how DynamoDB allocates resources to serve ...