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:
- twtech chooses VPC,
subnet(s), and security group(s).
- AWS provisions an Elastic Network Interface (ENI)
in each selected subnet.
- The ENI connects Lambda’s execution environment into
your VPC’s private network.
- 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
- Create or identify a VPC
- With the right CIDR block (e.g., 10.0.0.0/16).
- Create subnets
- Private subnets for internal services.
- Optional public subnet if Lambda needs internet.
- Set up routing
- NAT Gateway for private subnets needing outbound
internet.
- Internet Gateway for public subnets.
- Create a security group
- Allow necessary inbound/outbound traffic (e.g., DB
port 3306 for MySQL).
- Attach VPC config to Lambda
- In Lambda console → Configuration → VPC →
select VPC, subnets, security groups.
- Deploy Lambda code.
- Test connectivity to
internal resources (RDS, EC2, etc.).
5. Execution Flow
Example: Lambda inside VPC
calling RDS MySQL
- Request triggers Lambda.
- Lambda runs in a VPC-enabled ENI in your subnet.
- Outbound packets follow subnet’s route table.
- RDS endpoint resolves to a private IP (inside VPC).
- 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