|
| 1 | +# Provider Configuration |
| 2 | +provider "aws" { |
| 3 | + region = "us-east-1" # Change this to your desired region |
| 4 | +} |
| 5 | + |
| 6 | +# Variables |
| 7 | +variable "vpc_cidr" { |
| 8 | + default = "10.0.0.0/16" |
| 9 | +} |
| 10 | + |
| 11 | +variable "subnet_cidr" { |
| 12 | + default = "10.0.7.0/24" |
| 13 | +} |
| 14 | + |
| 15 | +# VPC Resources |
| 16 | +resource "aws_vpc" "vpc_lattice_vpc" { |
| 17 | + cidr_block = var.vpc_cidr |
| 18 | + enable_dns_support = true |
| 19 | + enable_dns_hostnames = true |
| 20 | + instance_tenancy = "default" |
| 21 | +} |
| 22 | + |
| 23 | +resource "aws_subnet" "vpc_subnet" { |
| 24 | + vpc_id = aws_vpc.vpc_lattice_vpc.id |
| 25 | + cidr_block = var.subnet_cidr |
| 26 | + availability_zone = "${data.aws_region.current.name}b" |
| 27 | + map_public_ip_on_launch = false |
| 28 | + |
| 29 | + tags = { |
| 30 | + Name = "Private-new-availability" |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +# Security Group |
| 35 | +resource "aws_security_group" "vpc_lattice_sg" { |
| 36 | + name = "VPCLatticeServiceNetworkSecurityGroup" |
| 37 | + description = "Security group for VPC LatticeService" |
| 38 | + vpc_id = aws_vpc.vpc_lattice_vpc.id |
| 39 | + |
| 40 | + ingress { |
| 41 | + from_port = 443 |
| 42 | + to_port = 443 |
| 43 | + protocol = "tcp" |
| 44 | + cidr_blocks = ["10.0.0.0/16"] |
| 45 | + } |
| 46 | + |
| 47 | + egress { |
| 48 | + from_port = 443 |
| 49 | + to_port = 443 |
| 50 | + protocol = "tcp" |
| 51 | + cidr_blocks = ["0.0.0.0/0"] |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +# VPC Lattice Resources |
| 56 | +resource "aws_vpclattice_service_network" "vl_service_network" { |
| 57 | + name = "vl-test-service-network" |
| 58 | + auth_type = "NONE" |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_vpclattice_service" "vl_service" { |
| 62 | + name = "vl-test-service" |
| 63 | + auth_type = "NONE" |
| 64 | +} |
| 65 | + |
| 66 | +# IAM Role for Lambda Functions |
| 67 | +resource "aws_iam_role" "lambda_role" { |
| 68 | + name = "vpc_lattice_lambda_role" |
| 69 | + |
| 70 | + assume_role_policy = jsonencode({ |
| 71 | + Version = "2012-10-17" |
| 72 | + Statement = [ |
| 73 | + { |
| 74 | + Action = "sts:AssumeRole" |
| 75 | + Effect = "Allow" |
| 76 | + Principal = { |
| 77 | + Service = "lambda.amazonaws.com" |
| 78 | + } |
| 79 | + } |
| 80 | + ] |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +# Attach basic Lambda execution policy |
| 85 | +resource "aws_iam_role_policy_attachment" "lambda_basic" { |
| 86 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 87 | + role = aws_iam_role.lambda_role.name |
| 88 | +} |
| 89 | + |
| 90 | +# Attach VPC access policy for Lambda |
| 91 | +resource "aws_iam_role_policy_attachment" "lambda_vpc" { |
| 92 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" |
| 93 | + role = aws_iam_role.lambda_role.name |
| 94 | +} |
| 95 | + |
| 96 | +# Lambda Functions |
| 97 | +resource "aws_lambda_function" "vl_function_primary" { |
| 98 | + filename = "primary.zip" |
| 99 | + function_name = "vl-function-primary" |
| 100 | + role = aws_iam_role.lambda_role.arn |
| 101 | + handler = "app.lambda_handler" |
| 102 | + runtime = "python3.13" |
| 103 | + architectures = ["x86_64"] |
| 104 | + source_code_hash = filebase64sha256("primary.zip") |
| 105 | +} |
| 106 | + |
| 107 | +resource "aws_lambda_function" "vl_function_secondary" { |
| 108 | + filename = "secondary.zip" |
| 109 | + function_name = "vl-function-secondary" |
| 110 | + role = aws_iam_role.lambda_role.arn |
| 111 | + handler = "app.lambda_handler" |
| 112 | + runtime = "python3.13" |
| 113 | + architectures = ["x86_64"] |
| 114 | + source_code_hash = filebase64sha256("secondary.zip") |
| 115 | +} |
| 116 | + |
| 117 | +resource "aws_lambda_function" "demo_lambda" { |
| 118 | + filename = "demolambda.zip" |
| 119 | + function_name = "demo-lambda" |
| 120 | + role = aws_iam_role.lambda_role.arn |
| 121 | + handler = "app.lambda_handler" |
| 122 | + runtime = "python3.13" |
| 123 | + architectures = ["x86_64"] |
| 124 | + source_code_hash = filebase64sha256("demolambda.zip") |
| 125 | + |
| 126 | + vpc_config { |
| 127 | + subnet_ids = [aws_subnet.vpc_subnet.id] |
| 128 | + security_group_ids = [aws_security_group.vpc_lattice_sg.id] |
| 129 | + } |
| 130 | + |
| 131 | + environment { |
| 132 | + variables = { |
| 133 | + URL = aws_vpclattice_service.vl_service.dns_entry[0].domain_name |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +# VPC Lattice Target Groups |
| 139 | +resource "aws_vpclattice_target_group" "primary_tg" { |
| 140 | + name = "vl-primary-target-group" |
| 141 | + type = "LAMBDA" |
| 142 | + |
| 143 | + config { |
| 144 | + lambda_event_structure_version = "V2" |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +resource "aws_vpclattice_target_group_attachment" "primary_tg_attachment" { |
| 149 | + target_group_identifier = aws_vpclattice_target_group.primary_tg.id |
| 150 | + target { |
| 151 | + id = aws_lambda_function.vl_function_primary.arn |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +resource "aws_vpclattice_target_group" "secondary_tg" { |
| 156 | + name = "vl-secondary-target-group" |
| 157 | + type = "LAMBDA" |
| 158 | + |
| 159 | + config { |
| 160 | + lambda_event_structure_version = "V2" |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +resource "aws_vpclattice_target_group_attachment" "secondary_tg_attachment" { |
| 165 | + target_group_identifier = aws_vpclattice_target_group.secondary_tg.id |
| 166 | + target { |
| 167 | + id = aws_lambda_function.vl_function_secondary.arn |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +# VPC Lattice Listener |
| 172 | +resource "aws_vpclattice_listener" "vl_listener" { |
| 173 | + name = "vl-service-listener" |
| 174 | + protocol = "HTTPS" |
| 175 | + port = 443 |
| 176 | + service_identifier = aws_vpclattice_service.vl_service.id |
| 177 | + |
| 178 | + default_action { |
| 179 | + forward { |
| 180 | + target_groups { |
| 181 | + target_group_identifier = aws_vpclattice_target_group.primary_tg.id |
| 182 | + weight = 60 |
| 183 | + } |
| 184 | + target_groups { |
| 185 | + target_group_identifier = aws_vpclattice_target_group.secondary_tg.id |
| 186 | + weight = 40 |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +# VPC Lattice Associations |
| 193 | +resource "aws_vpclattice_service_network_service_association" "service_association" { |
| 194 | + service_identifier = aws_vpclattice_service.vl_service.id |
| 195 | + service_network_identifier = aws_vpclattice_service_network.vl_service_network.id |
| 196 | +} |
| 197 | + |
| 198 | +resource "aws_vpclattice_service_network_vpc_association" "vpc_association" { |
| 199 | + vpc_identifier = aws_vpc.vpc_lattice_vpc.id |
| 200 | + service_network_identifier = aws_vpclattice_service_network.vl_service_network.id |
| 201 | + security_group_ids = [aws_security_group.vpc_lattice_sg.id] |
| 202 | +} |
| 203 | + |
| 204 | +# Outputs |
| 205 | +output "vl_function_primary_arn" { |
| 206 | + value = aws_lambda_function.vl_function_primary.arn |
| 207 | +} |
| 208 | + |
| 209 | +output "vl_function_secondary_arn" { |
| 210 | + value = aws_lambda_function.vl_function_secondary.arn |
| 211 | +} |
| 212 | + |
| 213 | +output "vl_service_network_id" { |
| 214 | + value = aws_vpclattice_service_network.vl_service_network.id |
| 215 | +} |
| 216 | + |
| 217 | +output "vl_service_id" { |
| 218 | + value = aws_vpclattice_service.vl_service.id |
| 219 | +} |
| 220 | + |
| 221 | +output "vl_service_dns" { |
| 222 | + value = aws_vpclattice_service.vl_service.dns_entry[0].domain_name |
| 223 | +} |
| 224 | + |
| 225 | +# Data source for current region |
| 226 | +data "aws_region" "current" {} |
0 commit comments