π
π§ͺ HANDS-ON 5
Secure Serverless Compute
(AWS Lambda + VPC + Security Groups + Secrets Manager)
This lab teaches you how real companies run Lambda securely.
1οΈβ£ What You Are Building (Big Picture)
You are building a Lambda function that:
Runs inside a private VPC and securely accesses a private resource (DB / service) using secrets.
No public exposure. No hardcoded credentials.
π Architecture
Client / Event
β
AWS Lambda (inside VPC)
β
Security Group
β
Private Resource (RDS / EC2 / internal API)
β
AWS Secrets Manager
2οΈβ£ Why This Lab Is VERY IMPORTANT
This lab teaches:
β Lambda inside VPC β Private networking β Security Groups with Lambda β Secrets management β Enterprise security patterns β Cold start trade-offs
π This is how real production systems work.
3οΈβ£ The Core Problem This Lab Solves
π΄ Problem 1: Lambda Is Public by Default
By default:
- Lambda runs outside your VPC
- It can access public AWS services
- It cannot access private resources
π΄ Problem 2: Credentials Must Be Secure
Hardcoding:
DB_PASSWORD=admin123
β Security risk β Compliance violation β Exam trap
β Solution
- Put Lambda inside VPC
- Use Security Groups
- Store secrets in Secrets Manager
4οΈβ£ Key Concept: Lambda Inside a VPC
What does this really mean?
When Lambda runs in a VPC:
- AWS creates ENIs (Elastic Network Interfaces)
- Lambda attaches to your subnets
- Lambda follows VPC networking rules
π Lambda behaves like an EC2 network-wise (not compute-wise).
5οΈβ£ Networking Setup (CRITICAL)
Typical Secure Setup
| Component | Placement |
|---|---|
| Lambda | Private Subnet |
| Database | Private Subnet |
| NAT Gateway | Public Subnet |
| Internet Gateway | VPC |
Why Private Subnet?
Because:
- Lambda should not be directly exposed
- DB must not be public
- Security best practice
6οΈβ£ Full Execution Flow (VERY IMPORTANT)
Letβs go step by step.
π’ Step 1: Lambda Is Triggered
Trigger could be:
- API Gateway
- EventBridge
- SQS
- S3
Trigger does NOT change networking behavior.
π’ Step 2: Lambda Gets ENI in VPC
AWS:
- Creates ENI in your subnet
- Assigns private IP
- Attaches Security Group
This is transparent to you.
π’ Step 3: Lambda Fetches Secret
Lambda:
- Calls AWS Secrets Manager
- Uses IAM role
- Retrieves credentials securely
π No password in code π No config file
π’ Step 4: Lambda Connects to Private Resource
Lambda:
- Uses private IP
- Security Group allows access
- DB / service accepts connection
π’ Step 5: Lambda Completes Execution
- Connection closes
- Execution ends
- No server remains
7οΈβ£ Security Groups with Lambda (VERY IMPORTANT)
Security Group Rules
Lambda SG:
Outbound β DB port (e.g. 3306)
Database SG:
Inbound β from Lambda SG
π Security Group to Security Group reference (best practice, no IPs).
8οΈβ£ Secrets Manager (CRITICAL SECURITY CONCEPT)
Why Secrets Manager?
It:
- Stores credentials securely
- Encrypts secrets
- Rotates secrets automatically
- Integrates with IAM
What Secrets Are Stored?
- DB username/password
- API keys
- Tokens
- Certificates
How Lambda Accesses Secrets
Lambda
β
IAM Role
β
Secrets Manager
IAM policy example:
secretsmanager:GetSecretValue
9οΈβ£ IAM Role Design (Exam Important)
Lambda execution role needs:
β Access to Secrets Manager β Access to logs (CloudWatch) β Access to other AWS services
β Never store credentials in environment variables (unless encrypted)
π Internet Access from VPC Lambda (BIG EXAM TRAP)
β Can Lambda access internet inside VPC?
β NOT by default
β How to Enable Internet Access
Lambda (Private Subnet)
β
NAT Gateway
β
Internet Gateway
π NAT Gateway is required π This increases cost
1οΈβ£1οΈβ£ Cold Start Impact (VERY IMPORTANT)
Lambda in VPC:
- Needs ENI creation
- Cold starts are slower
Mitigation:
- Provisioned concurrency
- Keep function warm
- Avoid VPC unless required
1οΈβ£2οΈβ£ Cost Implications (Real World)
Additional costs:
- NAT Gateway hourly cost
- NAT data processing cost
π Many βexpensive Lambdaβ problems are actually NAT Gateway costs.
1οΈβ£3οΈβ£ Common Enterprise Use Cases
- Lambda β RDS
- Lambda β internal microservice
- Lambda β private API
- Secure backend processing
1οΈβ£4οΈβ£ Common Mistakes (VERY IMPORTANT)
β Hardcoding secrets β Using public DB β Missing SG rules β Forgetting NAT for internet access β Overusing VPC Lambda
1οΈβ£5οΈβ£ AWS Exam Points (MUST REMEMBER)
- Lambda can run inside VPC
- Uses ENIs
- Security Groups apply to Lambda
- Secrets Manager for credentials
- NAT Gateway needed for internet
- VPC increases cold start time
1οΈβ£6οΈβ£ Mental Model (REMEMBER FOREVER)
Lambda in VPC = Secure employee inside office
Secrets Manager = Locker with keys
Security Group = Door access rules
1οΈβ£7οΈβ£ Why This Lab Completes Serverless Mastery
After all 5 labs, you now understand:
β APIs β Events β Async processing β Scheduling β Security & networking
π This is full serverless compute coverage.