Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions lambda-managed-instances-tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ provider "aws" {

# Local variables
locals {
function_name = "hello-world-managed-instances-tf"
function_name = "hello-world-managed-instances-tf"
log_group_name = "/demo/lambda/${local.function_name}"
}

Expand All @@ -43,7 +43,7 @@ data "archive_file" "lambda_zip" {
resource "aws_cloudwatch_log_group" "demo_log_group" {
name = local.log_group_name
retention_in_days = 14

tags = {
Name = "DemoLogGroup"
Environment = "demo"
Expand Down Expand Up @@ -101,18 +101,16 @@ resource "aws_internet_gateway" "igw" {
}
}

# Public subnets (matching CDK CIDR blocks)
# Public subnets
resource "aws_subnet" "public_subnet_1" {
vpc_id = aws_vpc.lambda_managed_instances_vpc.id
cidr_block = "10.0.0.0/19"
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true

tags = {
Name = "LambdaManagedInstancesPublicSubnet1"
Environment = "demo"
"aws-cdk:subnet-name" = "Public"
"aws-cdk:subnet-type" = "Public"
Name = "LambdaManagedInstancesPublicSubnet1"
Environment = "demo"
}
}

Expand All @@ -123,10 +121,8 @@ resource "aws_subnet" "public_subnet_2" {
map_public_ip_on_launch = true

tags = {
Name = "LambdaManagedInstancesPublicSubnet2"
Environment = "demo"
"aws-cdk:subnet-name" = "Public"
"aws-cdk:subnet-type" = "Public"
Name = "LambdaManagedInstancesPublicSubnet2"
Environment = "demo"
}
}

Expand All @@ -137,24 +133,20 @@ resource "aws_subnet" "public_subnet_3" {
map_public_ip_on_launch = true

tags = {
Name = "LambdaManagedInstancesPublicSubnet3"
Environment = "demo"
"aws-cdk:subnet-name" = "Public"
"aws-cdk:subnet-type" = "Public"
Name = "LambdaManagedInstancesPublicSubnet3"
Environment = "demo"
}
}

# Private subnets (matching CDK CIDR blocks)
# Private subnets
resource "aws_subnet" "private_subnet_1" {
vpc_id = aws_vpc.lambda_managed_instances_vpc.id
cidr_block = "10.0.96.0/19"
availability_zone = data.aws_availability_zones.available.names[0]

tags = {
Name = "LambdaManagedInstancesPrivateSubnet1"
Environment = "demo"
"aws-cdk:subnet-name" = "Private"
"aws-cdk:subnet-type" = "Private"
Name = "LambdaManagedInstancesPrivateSubnet1"
Environment = "demo"
}
}

Expand All @@ -164,10 +156,8 @@ resource "aws_subnet" "private_subnet_2" {
availability_zone = data.aws_availability_zones.available.names[1]

tags = {
Name = "LambdaManagedInstancesPrivateSubnet2"
Environment = "demo"
"aws-cdk:subnet-name" = "Private"
"aws-cdk:subnet-type" = "Private"
Name = "LambdaManagedInstancesPrivateSubnet2"
Environment = "demo"
}
}

Expand All @@ -177,10 +167,8 @@ resource "aws_subnet" "private_subnet_3" {
availability_zone = data.aws_availability_zones.available.names[2]

tags = {
Name = "LambdaManagedInstancesPrivateSubnet3"
Environment = "demo"
"aws-cdk:subnet-name" = "Private"
"aws-cdk:subnet-type" = "Private"
Name = "LambdaManagedInstancesPrivateSubnet3"
Environment = "demo"
}
}

Expand All @@ -191,7 +179,7 @@ data "aws_availability_zones" "available" {

# Elastic IPs for NAT Gateways
resource "aws_eip" "nat_eip_1" {
domain = "vpc"
domain = "vpc"
depends_on = [aws_internet_gateway.igw]

tags = {
Expand All @@ -201,7 +189,7 @@ resource "aws_eip" "nat_eip_1" {
}

resource "aws_eip" "nat_eip_2" {
domain = "vpc"
domain = "vpc"
depends_on = [aws_internet_gateway.igw]

tags = {
Expand All @@ -211,7 +199,7 @@ resource "aws_eip" "nat_eip_2" {
}

resource "aws_eip" "nat_eip_3" {
domain = "vpc"
domain = "vpc"
depends_on = [aws_internet_gateway.igw]

tags = {
Expand Down Expand Up @@ -394,7 +382,7 @@ resource "aws_security_group" "lambda_security_group" {
}
}

# Restrict default security group (matching CDK behavior)
# Restrict default security group
resource "aws_default_security_group" "default" {
vpc_id = aws_vpc.lambda_managed_instances_vpc.id

Expand All @@ -412,14 +400,14 @@ resource "aws_default_security_group" "default" {
resource "aws_lambda_function" "hello_world_function" {
filename = data.archive_file.lambda_zip.output_path
function_name = local.function_name
role = aws_iam_role.lambda_role.arn
handler = "hello-world.lambda_handler"
role = aws_iam_role.lambda_role.arn
handler = "hello-world.lambda_handler"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
runtime = "python3.13"
architectures = ["arm64"]
description = "Simple Hello World Lambda function on Managed Instances"
memory_size = 2048
publish = true
runtime = "python3.13"
architectures = ["arm64"]
description = "Simple Hello World Lambda function on Managed Instances"
memory_size = 2048
publish = true

logging_config {
log_format = "JSON"
Expand Down Expand Up @@ -492,7 +480,7 @@ resource "aws_iam_role_policy_attachment" "capacity_provider_managed_policy" {
# Lambda Capacity Provider for Managed Instances
resource "aws_lambda_capacity_provider" "lambda_capacity_provider" {
name = "lambda-capacity-provider-tf"

vpc_config {
subnet_ids = [aws_subnet.private_subnet_1.id, aws_subnet.private_subnet_2.id, aws_subnet.private_subnet_3.id]
security_group_ids = [aws_security_group.lambda_security_group.id]
Expand All @@ -512,4 +500,4 @@ resource "aws_lambda_capacity_provider" "lambda_capacity_provider" {
}
}

# Function association with capacity provider is configured in the Lambda function resource above
# Function association with capacity provider is configured in the Lambda function resource above
2 changes: 1 addition & 1 deletion lambda-managed-instances-tf/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ output "function_alias" {

output "manual_association_command" {
description = "Manual command to associate Lambda function with capacity provider"
value = "aws lambda put-capacity-provider-function --capacity-provider-arn ${aws_lambda_capacity_provider.lambda_capacity_provider.arn} --function-name ${aws_lambda_function.hello_world_function.function_name}"
value = "aws lambda put-capacity-provider-function --capacity-provider-arn ${aws_lambda_capacity_provider.lambda_capacity_provider.arn} --function-name ${aws_lambda_function.hello_world_function.function_name}"
}

output "nat_gateway_ids" {
Expand Down