The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { |
| 3 | EgressOnlyInternetGateway, |
| 4 | EIP, |
| 5 | InternetGateway, |
| 6 | NatGateway, |
| 7 | NetworkAcl, |
| 8 | NetworkAclAssociation, |
| 9 | NetworkAclEntry, |
| 10 | Route, |
| 11 | RouteTable, |
| 12 | RouteTableAssociation, |
| 13 | SecurityGroup, |
| 14 | Subnet, |
| 15 | Vpc, |
| 16 | VpcEndpoint, |
| 17 | } from "@alchemy.run/aws/EC2"; |
| 18 | import { expect } from "@alchemy.run/test"; |
| 19 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 20 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 21 | import { Data, Schedule } from "effect"; |
| 22 | import * as Effect from "effect/Effect"; |
| 23 | import { MinimumLogLevel } from "effect/References"; |
| 24 | import * as Test from "./VpcTest.ts"; |
| 25 | |
| 26 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 27 | |
| 28 | const { test } = Test.make({ providers: AWS.providers() }); |
| 29 | |
| 30 | test.provider.skip( |
| 31 | "VPC evolution: from simple to complex", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | // Get available AZs for multi-AZ stages |
| 37 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 38 | const availableAzs = |
| 39 | azResult.AvailabilityZones?.filter((az) => az.State === "available") ?? []; |
| 40 | const az1 = availableAzs[0]?.ZoneName!; |
| 41 | const az2 = availableAzs[1]?.ZoneName!; |
| 42 | |
| 43 | // ========================================================================= |
| 44 | // STAGE 1: Bare Minimum VPC |
| 45 | // User starts with just a VPC - the most basic setup |
| 46 | // ========================================================================= |
| 47 | yield* Effect.log("=== Stage 1: Bare Minimum VPC ==="); |
| 48 | { |
| 49 | const myVpc = yield* stack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | return yield* Vpc("MyVpc", { |
| 52 | cidrBlock: "10.0.0.0/16", |
| 53 | }); |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | // Verify VPC was created |
| 58 | expect(myVpc.vpcId).toMatch(/^vpc-/); |
| 59 | expect(myVpc.cidrBlock).toEqual("10.0.0.0/16"); |
| 60 | expect(myVpc.state).toEqual("available"); |
| 61 | |
| 62 | const vpcResult = yield* EC2.describeVpcs({ |
| 63 | VpcIds: [myVpc.vpcId], |
| 64 | }); |
| 65 | expect(vpcResult.Vpcs?.[0]?.CidrBlock).toEqual("10.0.0.0/16"); |
| 66 | } |
| 67 | |
| 68 | // ========================================================================= |
| 69 | // STAGE 2: Add Internet Connectivity |
| 70 | // User needs public internet access - add IGW, public subnet, route table |
| 71 | // Tests: VPC update (DNS settings), IGW create, Subnet create, Route create |
| 72 | // ========================================================================= |
| 73 | yield* Effect.log("=== Stage 2: Add Internet Connectivity ==="); |
| 74 | { |
| 75 | const _stack = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | const myVpc = yield* Vpc("MyVpc", { |
| 78 | cidrBlock: "10.0.0.0/16", |
| 79 | enableDnsSupport: true, |
| 80 | enableDnsHostnames: true, |
| 81 | }); |
| 82 | |
| 83 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 84 | vpcId: myVpc.vpcId, |
| 85 | }); |
| 86 | |
| 87 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 88 | vpcId: myVpc.vpcId, |
| 89 | cidrBlock: "10.0.1.0/24", |
| 90 | availabilityZone: az1, |
| 91 | mapPublicIpOnLaunch: true, |
| 92 | }); |
| 93 | |
| 94 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 95 | vpcId: myVpc.vpcId, |
| 96 | }); |
| 97 | |
| 98 | const internetRoute = yield* Route("InternetRoute", { |
| 99 | routeTableId: publicRouteTable.routeTableId, |
| 100 | destinationCidrBlock: "0.0.0.0/0", |
| 101 | gatewayId: internetGateway.internetGatewayId, |
| 102 | }); |
| 103 | |
| 104 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 105 | "PublicSubnet1Association", |
| 106 | { |
| 107 | routeTableId: publicRouteTable.routeTableId, |
| 108 | subnetId: publicSubnet1.subnetId, |
| 109 | }, |
| 110 | ); |
| 111 | |
| 112 | return { |
| 113 | myVpc, |
| 114 | internetGateway, |
| 115 | publicSubnet1, |
| 116 | publicRouteTable, |
| 117 | internetRoute, |
| 118 | publicSubnet1Association, |
| 119 | }; |
| 120 | }), |
| 121 | ); |
| 122 | |
| 123 | // Verify IGW |
| 124 | expect(_stack.internetGateway.internetGatewayId).toMatch(/^igw-/); |
| 125 | expect(_stack.internetGateway.vpcId).toEqual(_stack.myVpc.vpcId); |
| 126 | |
| 127 | // Verify public subnet |
| 128 | expect(_stack.publicSubnet1.subnetId).toMatch(/^subnet-/); |
| 129 | expect(_stack.publicSubnet1.mapPublicIpOnLaunch).toEqual(true); |
| 130 | expect(_stack.publicSubnet1.availabilityZone).toEqual(az1); |
| 131 | |
| 132 | // Verify route to IGW |
| 133 | expect(_stack.internetRoute.state).toEqual("active"); |
| 134 | expect(_stack.internetRoute.gatewayId).toEqual(_stack.internetGateway.internetGatewayId); |
| 135 | |
| 136 | // Verify association |
| 137 | expect(_stack.publicSubnet1Association.associationId).toMatch(/^rtbassoc-/); |
| 138 | } |
| 139 | |
| 140 | // ========================================================================= |
| 141 | // STAGE 3: Add Private Subnet |
| 142 | // User needs private resources (databases, internal services) |
| 143 | // Tests: Adding private subnet with separate route table (no internet) |
| 144 | // ========================================================================= |
| 145 | yield* Effect.log("=== Stage 3: Add Private Subnet ==="); |
| 146 | { |
| 147 | const _stack = yield* stack.deploy( |
| 148 | Effect.gen(function* () { |
| 149 | const myVpc = yield* Vpc("MyVpc", { |
| 150 | cidrBlock: "10.0.0.0/16", |
| 151 | enableDnsSupport: true, |
| 152 | enableDnsHostnames: true, |
| 153 | }); |
| 154 | |
| 155 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 156 | vpcId: myVpc.vpcId, |
| 157 | }); |
| 158 | |
| 159 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 160 | vpcId: myVpc.vpcId, |
| 161 | cidrBlock: "10.0.1.0/24", |
| 162 | availabilityZone: az1, |
| 163 | mapPublicIpOnLaunch: true, |
| 164 | }); |
| 165 | |
| 166 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 167 | vpcId: myVpc.vpcId, |
| 168 | cidrBlock: "10.0.10.0/24", |
| 169 | availabilityZone: az1, |
| 170 | }); |
| 171 | |
| 172 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 173 | vpcId: myVpc.vpcId, |
| 174 | }); |
| 175 | |
| 176 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 177 | vpcId: myVpc.vpcId, |
| 178 | }); |
| 179 | |
| 180 | const internetRoute = yield* Route("InternetRoute", { |
| 181 | routeTableId: publicRouteTable.routeTableId, |
| 182 | destinationCidrBlock: "0.0.0.0/0", |
| 183 | gatewayId: internetGateway.internetGatewayId, |
| 184 | }); |
| 185 | |
| 186 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 187 | "PublicSubnet1Association", |
| 188 | { |
| 189 | routeTableId: publicRouteTable.routeTableId, |
| 190 | subnetId: publicSubnet1.subnetId, |
| 191 | }, |
| 192 | ); |
| 193 | |
| 194 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 195 | "PrivateSubnet1Association", |
| 196 | { |
| 197 | routeTableId: privateRouteTable.routeTableId, |
| 198 | subnetId: privateSubnet1.subnetId, |
| 199 | }, |
| 200 | ); |
| 201 | |
| 202 | return { |
| 203 | myVpc, |
| 204 | internetGateway, |
| 205 | publicSubnet1, |
| 206 | privateSubnet1, |
| 207 | publicRouteTable, |
| 208 | privateRouteTable, |
| 209 | internetRoute, |
| 210 | publicSubnet1Association, |
| 211 | privateSubnet1Association, |
| 212 | }; |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | // Verify private subnet |
| 217 | expect(_stack.privateSubnet1.subnetId).toMatch(/^subnet-/); |
| 218 | expect(_stack.privateSubnet1.mapPublicIpOnLaunch).toBeFalsy(); |
| 219 | |
| 220 | // Verify private route table has NO internet route |
| 221 | const privateRtResult = yield* EC2.describeRouteTables({ |
| 222 | RouteTableIds: [_stack.privateRouteTable.routeTableId], |
| 223 | }); |
| 224 | const privateRoutes = privateRtResult.RouteTables?.[0]?.Routes ?? []; |
| 225 | const privateInternetRoute = privateRoutes.find( |
| 226 | (r) => r.DestinationCidrBlock === "0.0.0.0/0", |
| 227 | ); |
| 228 | expect(privateInternetRoute).toBeUndefined(); |
| 229 | } |
| 230 | |
| 231 | // ========================================================================= |
| 232 | // STAGE 4: Multi-AZ Expansion |
| 233 | // User needs high availability - add subnets in second AZ |
| 234 | // Tests: Adding subnets in second AZ, sharing route tables |
| 235 | // ========================================================================= |
| 236 | yield* Effect.log("=== Stage 4: Multi-AZ Expansion ==="); |
| 237 | { |
| 238 | const _stack = yield* stack.deploy( |
| 239 | Effect.gen(function* () { |
| 240 | const myVpc = yield* Vpc("MyVpc", { |
| 241 | cidrBlock: "10.0.0.0/16", |
| 242 | enableDnsSupport: true, |
| 243 | enableDnsHostnames: true, |
| 244 | }); |
| 245 | |
| 246 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 247 | vpcId: myVpc.vpcId, |
| 248 | }); |
| 249 | |
| 250 | // AZ1 subnets |
| 251 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 252 | vpcId: myVpc.vpcId, |
| 253 | cidrBlock: "10.0.1.0/24", |
| 254 | availabilityZone: az1, |
| 255 | mapPublicIpOnLaunch: true, |
| 256 | }); |
| 257 | |
| 258 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 259 | vpcId: myVpc.vpcId, |
| 260 | cidrBlock: "10.0.10.0/24", |
| 261 | availabilityZone: az1, |
| 262 | }); |
| 263 | |
| 264 | // AZ2 subnets |
| 265 | const publicSubnet2 = yield* Subnet("PublicSubnet2", { |
| 266 | vpcId: myVpc.vpcId, |
| 267 | cidrBlock: "10.0.2.0/24", |
| 268 | availabilityZone: az2, |
| 269 | mapPublicIpOnLaunch: true, |
| 270 | }); |
| 271 | |
| 272 | const privateSubnet2 = yield* Subnet("PrivateSubnet2", { |
| 273 | vpcId: myVpc.vpcId, |
| 274 | cidrBlock: "10.0.11.0/24", |
| 275 | availabilityZone: az2, |
| 276 | }); |
| 277 | |
| 278 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 279 | vpcId: myVpc.vpcId, |
| 280 | }); |
| 281 | |
| 282 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 283 | vpcId: myVpc.vpcId, |
| 284 | }); |
| 285 | |
| 286 | const internetRoute = yield* Route("InternetRoute", { |
| 287 | routeTableId: publicRouteTable.routeTableId, |
| 288 | destinationCidrBlock: "0.0.0.0/0", |
| 289 | gatewayId: internetGateway.internetGatewayId, |
| 290 | }); |
| 291 | |
| 292 | // AZ1 associations |
| 293 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 294 | "PublicSubnet1Association", |
| 295 | { |
| 296 | routeTableId: publicRouteTable.routeTableId, |
| 297 | subnetId: publicSubnet1.subnetId, |
| 298 | }, |
| 299 | ); |
| 300 | |
| 301 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 302 | "PrivateSubnet1Association", |
| 303 | { |
| 304 | routeTableId: privateRouteTable.routeTableId, |
| 305 | subnetId: privateSubnet1.subnetId, |
| 306 | }, |
| 307 | ); |
| 308 | |
| 309 | // AZ2 associations (share route tables) |
| 310 | const publicSubnet2Association = yield* RouteTableAssociation( |
| 311 | "PublicSubnet2Association", |
| 312 | { |
| 313 | routeTableId: publicRouteTable.routeTableId, |
| 314 | subnetId: publicSubnet2.subnetId, |
| 315 | }, |
| 316 | ); |
| 317 | |
| 318 | const privateSubnet2Association = yield* RouteTableAssociation( |
| 319 | "PrivateSubnet2Association", |
| 320 | { |
| 321 | routeTableId: privateRouteTable.routeTableId, |
| 322 | subnetId: privateSubnet2.subnetId, |
| 323 | }, |
| 324 | ); |
| 325 | |
| 326 | return { |
| 327 | myVpc, |
| 328 | internetGateway, |
| 329 | publicSubnet1, |
| 330 | privateSubnet1, |
| 331 | publicSubnet2, |
| 332 | privateSubnet2, |
| 333 | publicRouteTable, |
| 334 | privateRouteTable, |
| 335 | internetRoute, |
| 336 | publicSubnet1Association, |
| 337 | privateSubnet1Association, |
| 338 | publicSubnet2Association, |
| 339 | privateSubnet2Association, |
| 340 | }; |
| 341 | }), |
| 342 | ); |
| 343 | |
| 344 | // Verify subnets are in different AZs |
| 345 | expect(_stack.publicSubnet1.availabilityZone).toEqual(az1); |
| 346 | expect(_stack.publicSubnet2.availabilityZone).toEqual(az2); |
| 347 | expect(_stack.privateSubnet1.availabilityZone).toEqual(az1); |
| 348 | expect(_stack.privateSubnet2.availabilityZone).toEqual(az2); |
| 349 | |
| 350 | // Verify all 4 associations exist |
| 351 | expect(_stack.publicSubnet1Association.associationId).toMatch(/^rtbassoc-/); |
| 352 | expect(_stack.publicSubnet2Association.associationId).toMatch(/^rtbassoc-/); |
| 353 | expect(_stack.privateSubnet1Association.associationId).toMatch(/^rtbassoc-/); |
| 354 | expect(_stack.privateSubnet2Association.associationId).toMatch(/^rtbassoc-/); |
| 355 | |
| 356 | // Verify both public subnets share the same route table |
| 357 | expect(_stack.publicSubnet1Association.routeTableId).toEqual( |
| 358 | _stack.publicSubnet2Association.routeTableId, |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | // ========================================================================= |
| 363 | // STAGE 5: Update Tags and Properties |
| 364 | // User needs better organization - add tags for production |
| 365 | // Tests: Tag updates on existing resources |
| 366 | // ========================================================================= |
| 367 | yield* Effect.log("=== Stage 5: Update Tags and Properties ==="); |
| 368 | { |
| 369 | const _stack = yield* stack.deploy( |
| 370 | Effect.gen(function* () { |
| 371 | const myVpc = yield* Vpc("MyVpc", { |
| 372 | cidrBlock: "10.0.0.0/16", |
| 373 | enableDnsSupport: true, |
| 374 | enableDnsHostnames: true, |
| 375 | tags: { |
| 376 | Name: "production-vpc", |
| 377 | Environment: "production", |
| 378 | }, |
| 379 | }); |
| 380 | |
| 381 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 382 | vpcId: myVpc.vpcId, |
| 383 | tags: { |
| 384 | Name: "production-igw", |
| 385 | }, |
| 386 | }); |
| 387 | |
| 388 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 389 | vpcId: myVpc.vpcId, |
| 390 | cidrBlock: "10.0.1.0/24", |
| 391 | availabilityZone: az1, |
| 392 | mapPublicIpOnLaunch: true, |
| 393 | tags: { Name: "public-1a", Tier: "public" }, |
| 394 | }); |
| 395 | |
| 396 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 397 | vpcId: myVpc.vpcId, |
| 398 | cidrBlock: "10.0.10.0/24", |
| 399 | availabilityZone: az1, |
| 400 | tags: { Name: "private-1a", Tier: "private" }, |
| 401 | }); |
| 402 | |
| 403 | const publicSubnet2 = yield* Subnet("PublicSubnet2", { |
| 404 | vpcId: myVpc.vpcId, |
| 405 | cidrBlock: "10.0.2.0/24", |
| 406 | availabilityZone: az2, |
| 407 | mapPublicIpOnLaunch: true, |
| 408 | tags: { Name: "public-1b", Tier: "public" }, |
| 409 | }); |
| 410 | |
| 411 | const privateSubnet2 = yield* Subnet("PrivateSubnet2", { |
| 412 | vpcId: myVpc.vpcId, |
| 413 | cidrBlock: "10.0.11.0/24", |
| 414 | availabilityZone: az2, |
| 415 | tags: { Name: "private-1b", Tier: "private" }, |
| 416 | }); |
| 417 | |
| 418 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 419 | vpcId: myVpc.vpcId, |
| 420 | tags: { Name: "public-rt" }, |
| 421 | }); |
| 422 | |
| 423 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 424 | vpcId: myVpc.vpcId, |
| 425 | tags: { Name: "private-rt" }, |
| 426 | }); |
| 427 | |
| 428 | const internetRoute = yield* Route("InternetRoute", { |
| 429 | routeTableId: publicRouteTable.routeTableId, |
| 430 | destinationCidrBlock: "0.0.0.0/0", |
| 431 | gatewayId: internetGateway.internetGatewayId, |
| 432 | }); |
| 433 | |
| 434 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 435 | "PublicSubnet1Association", |
| 436 | { |
| 437 | routeTableId: publicRouteTable.routeTableId, |
| 438 | subnetId: publicSubnet1.subnetId, |
| 439 | }, |
| 440 | ); |
| 441 | |
| 442 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 443 | "PrivateSubnet1Association", |
| 444 | { |
| 445 | routeTableId: privateRouteTable.routeTableId, |
| 446 | subnetId: privateSubnet1.subnetId, |
| 447 | }, |
| 448 | ); |
| 449 | |
| 450 | const publicSubnet2Association = yield* RouteTableAssociation( |
| 451 | "PublicSubnet2Association", |
| 452 | { |
| 453 | routeTableId: publicRouteTable.routeTableId, |
| 454 | subnetId: publicSubnet2.subnetId, |
| 455 | }, |
| 456 | ); |
| 457 | |
| 458 | const privateSubnet2Association = yield* RouteTableAssociation( |
| 459 | "PrivateSubnet2Association", |
| 460 | { |
| 461 | routeTableId: privateRouteTable.routeTableId, |
| 462 | subnetId: privateSubnet2.subnetId, |
| 463 | }, |
| 464 | ); |
| 465 | |
| 466 | return { |
| 467 | myVpc, |
| 468 | internetGateway, |
| 469 | publicSubnet1, |
| 470 | privateSubnet1, |
| 471 | publicSubnet2, |
| 472 | privateSubnet2, |
| 473 | publicRouteTable, |
| 474 | privateRouteTable, |
| 475 | internetRoute, |
| 476 | publicSubnet1Association, |
| 477 | privateSubnet1Association, |
| 478 | publicSubnet2Association, |
| 479 | privateSubnet2Association, |
| 480 | }; |
| 481 | }), |
| 482 | ); |
| 483 | |
| 484 | // Verify tags were applied by checking AWS (with retry for eventual consistency) |
| 485 | yield* assertVpcTags(_stack.myVpc.vpcId, { |
| 486 | Name: "production-vpc", |
| 487 | Environment: "production", |
| 488 | }); |
| 489 | } |
| 490 | |
| 491 | // ========================================================================= |
| 492 | // STAGE 6: Re-associate Subnet to Different Route Table |
| 493 | // User wants to move PublicSubnet2 to a dedicated route table |
| 494 | // Tests: Route table association update (replaceRouteTableAssociation) |
| 495 | // ========================================================================= |
| 496 | yield* Effect.log("=== Stage 6: Re-associate Subnet ==="); |
| 497 | { |
| 498 | const _stack = yield* stack.deploy( |
| 499 | Effect.gen(function* () { |
| 500 | const myVpc = yield* Vpc("MyVpc", { |
| 501 | cidrBlock: "10.0.0.0/16", |
| 502 | enableDnsSupport: true, |
| 503 | enableDnsHostnames: true, |
| 504 | tags: { |
| 505 | Name: "production-vpc", |
| 506 | Environment: "production", |
| 507 | }, |
| 508 | }); |
| 509 | |
| 510 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 511 | vpcId: myVpc.vpcId, |
| 512 | tags: { Name: "production-igw" }, |
| 513 | }); |
| 514 | |
| 515 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 516 | vpcId: myVpc.vpcId, |
| 517 | cidrBlock: "10.0.1.0/24", |
| 518 | availabilityZone: az1, |
| 519 | mapPublicIpOnLaunch: true, |
| 520 | tags: { Name: "public-1a", Tier: "public" }, |
| 521 | }); |
| 522 | |
| 523 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 524 | vpcId: myVpc.vpcId, |
| 525 | cidrBlock: "10.0.10.0/24", |
| 526 | availabilityZone: az1, |
| 527 | tags: { Name: "private-1a", Tier: "private" }, |
| 528 | }); |
| 529 | |
| 530 | const publicSubnet2 = yield* Subnet("PublicSubnet2", { |
| 531 | vpcId: myVpc.vpcId, |
| 532 | cidrBlock: "10.0.2.0/24", |
| 533 | availabilityZone: az2, |
| 534 | mapPublicIpOnLaunch: true, |
| 535 | tags: { Name: "public-1b", Tier: "public" }, |
| 536 | }); |
| 537 | |
| 538 | const privateSubnet2 = yield* Subnet("PrivateSubnet2", { |
| 539 | vpcId: myVpc.vpcId, |
| 540 | cidrBlock: "10.0.11.0/24", |
| 541 | availabilityZone: az2, |
| 542 | tags: { Name: "private-1b", Tier: "private" }, |
| 543 | }); |
| 544 | |
| 545 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 546 | vpcId: myVpc.vpcId, |
| 547 | tags: { Name: "public-rt" }, |
| 548 | }); |
| 549 | |
| 550 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 551 | vpcId: myVpc.vpcId, |
| 552 | tags: { Name: "private-rt" }, |
| 553 | }); |
| 554 | |
| 555 | // NEW: Dedicated route table for AZ2 public subnet |
| 556 | const publicRouteTable2 = yield* RouteTable("PublicRouteTable2", { |
| 557 | vpcId: myVpc.vpcId, |
| 558 | tags: { Name: "public-rt-az2" }, |
| 559 | }); |
| 560 | |
| 561 | const internetRoute = yield* Route("InternetRoute", { |
| 562 | routeTableId: publicRouteTable.routeTableId, |
| 563 | destinationCidrBlock: "0.0.0.0/0", |
| 564 | gatewayId: internetGateway.internetGatewayId, |
| 565 | }); |
| 566 | |
| 567 | // NEW: Internet route for AZ2 public route table |
| 568 | const internetRoute2 = yield* Route("InternetRoute2", { |
| 569 | routeTableId: publicRouteTable2.routeTableId, |
| 570 | destinationCidrBlock: "0.0.0.0/0", |
| 571 | gatewayId: internetGateway.internetGatewayId, |
| 572 | }); |
| 573 | |
| 574 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 575 | "PublicSubnet1Association", |
| 576 | { |
| 577 | routeTableId: publicRouteTable.routeTableId, |
| 578 | subnetId: publicSubnet1.subnetId, |
| 579 | }, |
| 580 | ); |
| 581 | |
| 582 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 583 | "PrivateSubnet1Association", |
| 584 | { |
| 585 | routeTableId: privateRouteTable.routeTableId, |
| 586 | subnetId: privateSubnet1.subnetId, |
| 587 | }, |
| 588 | ); |
| 589 | |
| 590 | // CHANGED: PublicSubnet2 now uses PublicRouteTable2 |
| 591 | const publicSubnet2Association = yield* RouteTableAssociation( |
| 592 | "PublicSubnet2Association", |
| 593 | { |
| 594 | routeTableId: publicRouteTable2.routeTableId, |
| 595 | subnetId: publicSubnet2.subnetId, |
| 596 | }, |
| 597 | ); |
| 598 | |
| 599 | const privateSubnet2Association = yield* RouteTableAssociation( |
| 600 | "PrivateSubnet2Association", |
| 601 | { |
| 602 | routeTableId: privateRouteTable.routeTableId, |
| 603 | subnetId: privateSubnet2.subnetId, |
| 604 | }, |
| 605 | ); |
| 606 | |
| 607 | return { |
| 608 | myVpc, |
| 609 | internetGateway, |
| 610 | publicSubnet1, |
| 611 | privateSubnet1, |
| 612 | publicSubnet2, |
| 613 | privateSubnet2, |
| 614 | publicRouteTable, |
| 615 | privateRouteTable, |
| 616 | publicRouteTable2, |
| 617 | internetRoute, |
| 618 | internetRoute2, |
| 619 | publicSubnet1Association, |
| 620 | privateSubnet1Association, |
| 621 | publicSubnet2Association, |
| 622 | privateSubnet2Association, |
| 623 | }; |
| 624 | }), |
| 625 | ); |
| 626 | |
| 627 | // Verify PublicSubnet2 is now associated with a different route table |
| 628 | expect(_stack.publicSubnet2Association.routeTableId).toEqual( |
| 629 | _stack.publicRouteTable2.routeTableId, |
| 630 | ); |
| 631 | expect(_stack.publicSubnet2Association.routeTableId).not.toEqual( |
| 632 | _stack.publicSubnet1Association.routeTableId, |
| 633 | ); |
| 634 | |
| 635 | // Verify the new route table has an internet route |
| 636 | expect(_stack.internetRoute2.state).toEqual("active"); |
| 637 | } |
| 638 | |
| 639 | // ========================================================================= |
| 640 | // STAGE 7: Scale Down |
| 641 | // User removes AZ2 resources (cost savings) |
| 642 | // Tests: Resource deletion, dependency ordering during delete |
| 643 | // ========================================================================= |
| 644 | yield* Effect.log("=== Stage 7: Scale Down ==="); |
| 645 | { |
| 646 | const _stack = yield* stack.deploy( |
| 647 | Effect.gen(function* () { |
| 648 | const myVpc = yield* Vpc("MyVpc", { |
| 649 | cidrBlock: "10.0.0.0/16", |
| 650 | enableDnsSupport: true, |
| 651 | enableDnsHostnames: true, |
| 652 | tags: { |
| 653 | Name: "production-vpc", |
| 654 | Environment: "production", |
| 655 | }, |
| 656 | }); |
| 657 | |
| 658 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 659 | vpcId: myVpc.vpcId, |
| 660 | tags: { Name: "production-igw" }, |
| 661 | }); |
| 662 | |
| 663 | // Only AZ1 subnets remain |
| 664 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 665 | vpcId: myVpc.vpcId, |
| 666 | cidrBlock: "10.0.1.0/24", |
| 667 | availabilityZone: az1, |
| 668 | mapPublicIpOnLaunch: true, |
| 669 | tags: { Name: "public-1a", Tier: "public" }, |
| 670 | }); |
| 671 | |
| 672 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 673 | vpcId: myVpc.vpcId, |
| 674 | cidrBlock: "10.0.10.0/24", |
| 675 | availabilityZone: az1, |
| 676 | tags: { Name: "private-1a", Tier: "private" }, |
| 677 | }); |
| 678 | |
| 679 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 680 | vpcId: myVpc.vpcId, |
| 681 | tags: { Name: "public-rt" }, |
| 682 | }); |
| 683 | |
| 684 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 685 | vpcId: myVpc.vpcId, |
| 686 | tags: { Name: "private-rt" }, |
| 687 | }); |
| 688 | |
| 689 | const internetRoute = yield* Route("InternetRoute", { |
| 690 | routeTableId: publicRouteTable.routeTableId, |
| 691 | destinationCidrBlock: "0.0.0.0/0", |
| 692 | gatewayId: internetGateway.internetGatewayId, |
| 693 | }); |
| 694 | |
| 695 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 696 | "PublicSubnet1Association", |
| 697 | { |
| 698 | routeTableId: publicRouteTable.routeTableId, |
| 699 | subnetId: publicSubnet1.subnetId, |
| 700 | }, |
| 701 | ); |
| 702 | |
| 703 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 704 | "PrivateSubnet1Association", |
| 705 | { |
| 706 | routeTableId: privateRouteTable.routeTableId, |
| 707 | subnetId: privateSubnet1.subnetId, |
| 708 | }, |
| 709 | ); |
| 710 | |
| 711 | // Note: PublicSubnet2, PrivateSubnet2, PublicRouteTable2, InternetRoute2, |
| 712 | // and their associations are NOT included - they will be deleted |
| 713 | |
| 714 | return { |
| 715 | myVpc, |
| 716 | internetGateway, |
| 717 | publicSubnet1, |
| 718 | privateSubnet1, |
| 719 | publicRouteTable, |
| 720 | privateRouteTable, |
| 721 | internetRoute, |
| 722 | publicSubnet1Association, |
| 723 | privateSubnet1Association, |
| 724 | }; |
| 725 | }), |
| 726 | ); |
| 727 | |
| 728 | // Verify only 2 subnets exist now |
| 729 | const subnetsResult = yield* EC2.describeSubnets({ |
| 730 | Filters: [{ Name: "vpc-id", Values: [_stack.myVpc.vpcId] }], |
| 731 | }); |
| 732 | expect(subnetsResult.Subnets).toHaveLength(2); |
| 733 | |
| 734 | // Verify remaining subnets are in AZ1 |
| 735 | for (const subnet of subnetsResult.Subnets ?? []) { |
| 736 | expect(subnet.AvailabilityZone).toEqual(az1); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // ========================================================================= |
| 741 | // STAGE 8: Add NAT Gateway for Private Subnet Internet Access |
| 742 | // User needs private instances to access internet for updates |
| 743 | // Tests: EIP create, NAT Gateway create with state waiting |
| 744 | // ========================================================================= |
| 745 | yield* Effect.log("=== Stage 8: Add NAT Gateway ==="); |
| 746 | { |
| 747 | const _stack = yield* stack.deploy( |
| 748 | Effect.gen(function* () { |
| 749 | const myVpc = yield* Vpc("MyVpc", { |
| 750 | cidrBlock: "10.0.0.0/16", |
| 751 | enableDnsSupport: true, |
| 752 | enableDnsHostnames: true, |
| 753 | tags: { |
| 754 | Name: "production-vpc", |
| 755 | Environment: "production", |
| 756 | }, |
| 757 | }); |
| 758 | |
| 759 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 760 | vpcId: myVpc.vpcId, |
| 761 | tags: { Name: "production-igw" }, |
| 762 | }); |
| 763 | |
| 764 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 765 | vpcId: myVpc.vpcId, |
| 766 | cidrBlock: "10.0.1.0/24", |
| 767 | availabilityZone: az1, |
| 768 | mapPublicIpOnLaunch: true, |
| 769 | tags: { Name: "public-1a", Tier: "public" }, |
| 770 | }); |
| 771 | |
| 772 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 773 | vpcId: myVpc.vpcId, |
| 774 | cidrBlock: "10.0.10.0/24", |
| 775 | availabilityZone: az1, |
| 776 | tags: { Name: "private-1a", Tier: "private" }, |
| 777 | }); |
| 778 | |
| 779 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 780 | vpcId: myVpc.vpcId, |
| 781 | tags: { Name: "public-rt" }, |
| 782 | }); |
| 783 | |
| 784 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 785 | vpcId: myVpc.vpcId, |
| 786 | tags: { Name: "private-rt" }, |
| 787 | }); |
| 788 | |
| 789 | const internetRoute = yield* Route("InternetRoute", { |
| 790 | routeTableId: publicRouteTable.routeTableId, |
| 791 | destinationCidrBlock: "0.0.0.0/0", |
| 792 | gatewayId: internetGateway.internetGatewayId, |
| 793 | }); |
| 794 | |
| 795 | // NEW: Elastic IP for NAT Gateway |
| 796 | const natEip = yield* EIP("NatEip", { |
| 797 | tags: { Name: "nat-eip" }, |
| 798 | }); |
| 799 | |
| 800 | // NEW: NAT Gateway in public subnet |
| 801 | const natGateway = yield* NatGateway("NatGateway", { |
| 802 | subnetId: publicSubnet1.subnetId, |
| 803 | allocationId: natEip.allocationId, |
| 804 | tags: { Name: "production-nat" }, |
| 805 | }); |
| 806 | |
| 807 | // NEW: Route from private subnet to NAT Gateway |
| 808 | const natRoute = yield* Route("NatRoute", { |
| 809 | routeTableId: privateRouteTable.routeTableId, |
| 810 | destinationCidrBlock: "0.0.0.0/0", |
| 811 | natGatewayId: natGateway.natGatewayId, |
| 812 | }); |
| 813 | |
| 814 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 815 | "PublicSubnet1Association", |
| 816 | { |
| 817 | routeTableId: publicRouteTable.routeTableId, |
| 818 | subnetId: publicSubnet1.subnetId, |
| 819 | }, |
| 820 | ); |
| 821 | |
| 822 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 823 | "PrivateSubnet1Association", |
| 824 | { |
| 825 | routeTableId: privateRouteTable.routeTableId, |
| 826 | subnetId: privateSubnet1.subnetId, |
| 827 | }, |
| 828 | ); |
| 829 | |
| 830 | return { |
| 831 | myVpc, |
| 832 | internetGateway, |
| 833 | publicSubnet1, |
| 834 | privateSubnet1, |
| 835 | publicRouteTable, |
| 836 | privateRouteTable, |
| 837 | internetRoute, |
| 838 | natEip, |
| 839 | natGateway, |
| 840 | natRoute, |
| 841 | publicSubnet1Association, |
| 842 | privateSubnet1Association, |
| 843 | }; |
| 844 | }), |
| 845 | ); |
| 846 | |
| 847 | // Verify EIP |
| 848 | expect(_stack.natEip.allocationId).toMatch(/^eipalloc-/); |
| 849 | expect(_stack.natEip.publicIp).toBeDefined(); |
| 850 | |
| 851 | // Verify NAT Gateway |
| 852 | expect(_stack.natGateway.natGatewayId).toMatch(/^nat-/); |
| 853 | expect(_stack.natGateway.state).toEqual("available"); |
| 854 | expect(_stack.natGateway.publicIp).toEqual(_stack.natEip.publicIp); |
| 855 | |
| 856 | // Verify NAT route is active |
| 857 | expect(_stack.natRoute.state).toEqual("active"); |
| 858 | expect(_stack.natRoute.natGatewayId).toEqual(_stack.natGateway.natGatewayId); |
| 859 | |
| 860 | // Verify private route table now has internet route via NAT |
| 861 | const privateRtResult = yield* EC2.describeRouteTables({ |
| 862 | RouteTableIds: [_stack.privateRouteTable.routeTableId], |
| 863 | }); |
| 864 | const privateRoutes = privateRtResult.RouteTables?.[0]?.Routes ?? []; |
| 865 | const privateInternetRoute = privateRoutes.find( |
| 866 | (r) => r.DestinationCidrBlock === "0.0.0.0/0", |
| 867 | ); |
| 868 | expect(privateInternetRoute?.NatGatewayId).toEqual(_stack.natGateway.natGatewayId); |
| 869 | } |
| 870 | |
| 871 | // ========================================================================= |
| 872 | // STAGE 9: Add Security Groups |
| 873 | // User needs to control instance access |
| 874 | // Tests: Security Group with inline ingress/egress rules |
| 875 | // ========================================================================= |
| 876 | yield* Effect.log("=== Stage 9: Add Security Groups ==="); |
| 877 | { |
| 878 | const _stack = yield* stack.deploy( |
| 879 | Effect.gen(function* () { |
| 880 | const myVpc = yield* Vpc("MyVpc", { |
| 881 | cidrBlock: "10.0.0.0/16", |
| 882 | enableDnsSupport: true, |
| 883 | enableDnsHostnames: true, |
| 884 | tags: { |
| 885 | Name: "production-vpc", |
| 886 | Environment: "production", |
| 887 | }, |
| 888 | }); |
| 889 | |
| 890 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 891 | vpcId: myVpc.vpcId, |
| 892 | tags: { Name: "production-igw" }, |
| 893 | }); |
| 894 | |
| 895 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 896 | vpcId: myVpc.vpcId, |
| 897 | cidrBlock: "10.0.1.0/24", |
| 898 | availabilityZone: az1, |
| 899 | mapPublicIpOnLaunch: true, |
| 900 | tags: { Name: "public-1a", Tier: "public" }, |
| 901 | }); |
| 902 | |
| 903 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 904 | vpcId: myVpc.vpcId, |
| 905 | cidrBlock: "10.0.10.0/24", |
| 906 | availabilityZone: az1, |
| 907 | tags: { Name: "private-1a", Tier: "private" }, |
| 908 | }); |
| 909 | |
| 910 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 911 | vpcId: myVpc.vpcId, |
| 912 | tags: { Name: "public-rt" }, |
| 913 | }); |
| 914 | |
| 915 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 916 | vpcId: myVpc.vpcId, |
| 917 | tags: { Name: "private-rt" }, |
| 918 | }); |
| 919 | |
| 920 | const internetRoute = yield* Route("InternetRoute", { |
| 921 | routeTableId: publicRouteTable.routeTableId, |
| 922 | destinationCidrBlock: "0.0.0.0/0", |
| 923 | gatewayId: internetGateway.internetGatewayId, |
| 924 | }); |
| 925 | |
| 926 | const natEip = yield* EIP("NatEip", { |
| 927 | tags: { Name: "nat-eip" }, |
| 928 | }); |
| 929 | |
| 930 | const natGateway = yield* NatGateway("NatGateway", { |
| 931 | subnetId: publicSubnet1.subnetId, |
| 932 | allocationId: natEip.allocationId, |
| 933 | tags: { Name: "production-nat" }, |
| 934 | }); |
| 935 | |
| 936 | const natRoute = yield* Route("NatRoute", { |
| 937 | routeTableId: privateRouteTable.routeTableId, |
| 938 | destinationCidrBlock: "0.0.0.0/0", |
| 939 | natGatewayId: natGateway.natGatewayId, |
| 940 | }); |
| 941 | |
| 942 | // NEW: Web security group allowing HTTP/HTTPS |
| 943 | const webSecurityGroup = yield* SecurityGroup("WebSecurityGroup", { |
| 944 | vpcId: myVpc.vpcId, |
| 945 | description: "Web tier security group", |
| 946 | ingress: [ |
| 947 | { |
| 948 | ipProtocol: "tcp", |
| 949 | fromPort: 80, |
| 950 | toPort: 80, |
| 951 | cidrIpv4: "0.0.0.0/0", |
| 952 | description: "Allow HTTP", |
| 953 | }, |
| 954 | { |
| 955 | ipProtocol: "tcp", |
| 956 | fromPort: 443, |
| 957 | toPort: 443, |
| 958 | cidrIpv4: "0.0.0.0/0", |
| 959 | description: "Allow HTTPS", |
| 960 | }, |
| 961 | ], |
| 962 | egress: [ |
| 963 | { |
| 964 | ipProtocol: "-1", |
| 965 | cidrIpv4: "0.0.0.0/0", |
| 966 | description: "Allow all outbound", |
| 967 | }, |
| 968 | ], |
| 969 | tags: { Name: "web-sg" }, |
| 970 | }); |
| 971 | |
| 972 | // NEW: Database security group allowing access from web tier |
| 973 | const dbSecurityGroup = yield* SecurityGroup("DbSecurityGroup", { |
| 974 | vpcId: myVpc.vpcId, |
| 975 | description: "Database tier security group", |
| 976 | ingress: [ |
| 977 | { |
| 978 | ipProtocol: "tcp", |
| 979 | fromPort: 5432, |
| 980 | toPort: 5432, |
| 981 | referencedGroupId: webSecurityGroup.groupId, |
| 982 | description: "Allow PostgreSQL from web tier", |
| 983 | }, |
| 984 | ], |
| 985 | egress: [ |
| 986 | { |
| 987 | ipProtocol: "-1", |
| 988 | cidrIpv4: "0.0.0.0/0", |
| 989 | description: "Allow all outbound", |
| 990 | }, |
| 991 | ], |
| 992 | tags: { Name: "db-sg" }, |
| 993 | }); |
| 994 | |
| 995 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 996 | "PublicSubnet1Association", |
| 997 | { |
| 998 | routeTableId: publicRouteTable.routeTableId, |
| 999 | subnetId: publicSubnet1.subnetId, |
| 1000 | }, |
| 1001 | ); |
| 1002 | |
| 1003 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 1004 | "PrivateSubnet1Association", |
| 1005 | { |
| 1006 | routeTableId: privateRouteTable.routeTableId, |
| 1007 | subnetId: privateSubnet1.subnetId, |
| 1008 | }, |
| 1009 | ); |
| 1010 | |
| 1011 | return { |
| 1012 | myVpc, |
| 1013 | internetGateway, |
| 1014 | publicSubnet1, |
| 1015 | privateSubnet1, |
| 1016 | publicRouteTable, |
| 1017 | privateRouteTable, |
| 1018 | internetRoute, |
| 1019 | natEip, |
| 1020 | natGateway, |
| 1021 | natRoute, |
| 1022 | webSecurityGroup, |
| 1023 | dbSecurityGroup, |
| 1024 | publicSubnet1Association, |
| 1025 | privateSubnet1Association, |
| 1026 | }; |
| 1027 | }), |
| 1028 | ); |
| 1029 | |
| 1030 | // Verify Web Security Group |
| 1031 | expect(_stack.webSecurityGroup.groupId).toMatch(/^sg-/); |
| 1032 | expect(_stack.webSecurityGroup.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1033 | expect(_stack.webSecurityGroup.ingressRules).toHaveLength(2); |
| 1034 | expect(_stack.webSecurityGroup.egressRules).toHaveLength(1); |
| 1035 | |
| 1036 | // Verify DB Security Group references Web Security Group |
| 1037 | expect(_stack.dbSecurityGroup.groupId).toMatch(/^sg-/); |
| 1038 | expect(_stack.dbSecurityGroup.ingressRules).toHaveLength(1); |
| 1039 | expect(_stack.dbSecurityGroup.ingressRules?.[0]?.referencedGroupId).toEqual( |
| 1040 | _stack.webSecurityGroup.groupId, |
| 1041 | ); |
| 1042 | } |
| 1043 | |
| 1044 | // ========================================================================= |
| 1045 | // STAGE 10: Scale Down - Remove NAT Gateway and Security Groups |
| 1046 | // User scales down to basic VPC for cost savings |
| 1047 | // Tests: NAT Gateway delete with state waiting, Security Group delete |
| 1048 | // ========================================================================= |
| 1049 | yield* Effect.log("=== Stage 10: Scale Down to Basic VPC ==="); |
| 1050 | { |
| 1051 | const _stack = yield* stack.deploy( |
| 1052 | Effect.gen(function* () { |
| 1053 | const myVpc = yield* Vpc("MyVpc", { |
| 1054 | cidrBlock: "10.0.0.0/16", |
| 1055 | enableDnsSupport: true, |
| 1056 | enableDnsHostnames: true, |
| 1057 | tags: { |
| 1058 | Name: "production-vpc", |
| 1059 | Environment: "production", |
| 1060 | }, |
| 1061 | }); |
| 1062 | |
| 1063 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 1064 | vpcId: myVpc.vpcId, |
| 1065 | tags: { Name: "production-igw" }, |
| 1066 | }); |
| 1067 | |
| 1068 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 1069 | vpcId: myVpc.vpcId, |
| 1070 | cidrBlock: "10.0.1.0/24", |
| 1071 | availabilityZone: az1, |
| 1072 | mapPublicIpOnLaunch: true, |
| 1073 | tags: { Name: "public-1a", Tier: "public" }, |
| 1074 | }); |
| 1075 | |
| 1076 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 1077 | vpcId: myVpc.vpcId, |
| 1078 | cidrBlock: "10.0.10.0/24", |
| 1079 | availabilityZone: az1, |
| 1080 | tags: { Name: "private-1a", Tier: "private" }, |
| 1081 | }); |
| 1082 | |
| 1083 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 1084 | vpcId: myVpc.vpcId, |
| 1085 | tags: { Name: "public-rt" }, |
| 1086 | }); |
| 1087 | |
| 1088 | const privateRouteTable = yield* RouteTable("PrivateRouteTable", { |
| 1089 | vpcId: myVpc.vpcId, |
| 1090 | tags: { Name: "private-rt" }, |
| 1091 | }); |
| 1092 | |
| 1093 | const internetRoute = yield* Route("InternetRoute", { |
| 1094 | routeTableId: publicRouteTable.routeTableId, |
| 1095 | destinationCidrBlock: "0.0.0.0/0", |
| 1096 | gatewayId: internetGateway.internetGatewayId, |
| 1097 | }); |
| 1098 | |
| 1099 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 1100 | "PublicSubnet1Association", |
| 1101 | { |
| 1102 | routeTableId: publicRouteTable.routeTableId, |
| 1103 | subnetId: publicSubnet1.subnetId, |
| 1104 | }, |
| 1105 | ); |
| 1106 | |
| 1107 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 1108 | "PrivateSubnet1Association", |
| 1109 | { |
| 1110 | routeTableId: privateRouteTable.routeTableId, |
| 1111 | subnetId: privateSubnet1.subnetId, |
| 1112 | }, |
| 1113 | ); |
| 1114 | |
| 1115 | // Note: NAT Gateway, EIP, NatRoute, and Security Groups are NOT included |
| 1116 | // They will be deleted |
| 1117 | |
| 1118 | return { |
| 1119 | myVpc, |
| 1120 | internetGateway, |
| 1121 | publicSubnet1, |
| 1122 | privateSubnet1, |
| 1123 | publicRouteTable, |
| 1124 | privateRouteTable, |
| 1125 | internetRoute, |
| 1126 | publicSubnet1Association, |
| 1127 | privateSubnet1Association, |
| 1128 | }; |
| 1129 | }), |
| 1130 | ); |
| 1131 | |
| 1132 | // Verify NAT Gateway is deleted |
| 1133 | const natGwResult = yield* ec2 |
| 1134 | .describeNatGateways({ |
| 1135 | Filter: [{ Name: "vpc-id", Values: [_stack.myVpc.vpcId] }], |
| 1136 | }) |
| 1137 | .pipe(Effect.map((r) => r.NatGateways?.filter((gw) => gw.State !== "deleted"))); |
| 1138 | expect(natGwResult).toHaveLength(0); |
| 1139 | |
| 1140 | // Verify Security Groups are deleted (only default should remain) |
| 1141 | const sgResult = yield* EC2.describeSecurityGroups({ |
| 1142 | Filters: [{ Name: "vpc-id", Values: [_stack.myVpc.vpcId] }], |
| 1143 | }); |
| 1144 | expect(sgResult.SecurityGroups).toHaveLength(1); // Only default SG |
| 1145 | expect(sgResult.SecurityGroups?.[0]?.GroupName).toEqual("default"); |
| 1146 | } |
| 1147 | |
| 1148 | // ========================================================================= |
| 1149 | // STAGE 11: Final Cleanup |
| 1150 | // Destroy everything and verify |
| 1151 | // ========================================================================= |
| 1152 | yield* Effect.log("=== Stage 11: Final Cleanup ==="); |
| 1153 | const vpcResult = yield* EC2.describeVpcs({ |
| 1154 | Filters: [{ Name: "tag:Name", Values: ["production-vpc"] }], |
| 1155 | }); |
| 1156 | const capturedVpcId = vpcResult.Vpcs?.[0]?.VpcId; |
| 1157 | |
| 1158 | yield* stack.destroy(); |
| 1159 | |
| 1160 | // Verify VPC is deleted |
| 1161 | if (capturedVpcId) { |
| 1162 | yield* EC2.describeVpcs({ VpcIds: [capturedVpcId] }).pipe( |
| 1163 | Effect.flatMap(() => Effect.fail(new Error("VPC still exists"))), |
| 1164 | Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void), |
| 1165 | ); |
| 1166 | } |
| 1167 | |
| 1168 | yield* Effect.log("=== All stages completed successfully! ==="); |
| 1169 | }).pipe(logLevel), |
| 1170 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 1_000_000 }, |
| 1171 | ); |
| 1172 | |
| 1173 | test.provider.skip( |
| 1174 | "Comprehensive VPC with all components", |
| 1175 | (stack) => |
| 1176 | Effect.gen(function* () { |
| 1177 | yield* stack.destroy(); |
| 1178 | |
| 1179 | // Get available AZs |
| 1180 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 1181 | const availableAzs = |
| 1182 | azResult.AvailabilityZones?.filter((az) => az.State === "available") ?? []; |
| 1183 | const az1 = availableAzs[0]?.ZoneName!; |
| 1184 | const az2 = availableAzs[1]?.ZoneName!; |
| 1185 | |
| 1186 | // ========================================================================= |
| 1187 | // Define all resources for a production-ready VPC |
| 1188 | // ========================================================================= |
| 1189 | const _stack = yield* stack.deploy( |
| 1190 | Effect.gen(function* () { |
| 1191 | // VPC with DNS enabled and IPv6 for egress-only IGW |
| 1192 | const myVpc = yield* Vpc("MyVpc", { |
| 1193 | cidrBlock: "10.0.0.0/16", |
| 1194 | enableDnsSupport: true, |
| 1195 | enableDnsHostnames: true, |
| 1196 | amazonProvidedIpv6CidrBlock: true, |
| 1197 | tags: { |
| 1198 | Name: "comprehensive-vpc", |
| 1199 | Environment: "test", |
| 1200 | }, |
| 1201 | }); |
| 1202 | |
| 1203 | // Internet Gateway for public internet access |
| 1204 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 1205 | vpcId: myVpc.vpcId, |
| 1206 | tags: { Name: "comprehensive-igw" }, |
| 1207 | }); |
| 1208 | |
| 1209 | // Egress-Only Internet Gateway for IPv6 outbound traffic from private subnets |
| 1210 | const egressOnlyIgw = yield* EgressOnlyInternetGateway("EgressOnlyIgw", { |
| 1211 | vpcId: myVpc.vpcId, |
| 1212 | tags: { Name: "comprehensive-eigw" }, |
| 1213 | }); |
| 1214 | |
| 1215 | // Public Subnets in two AZs |
| 1216 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 1217 | vpcId: myVpc.vpcId, |
| 1218 | cidrBlock: "10.0.1.0/24", |
| 1219 | availabilityZone: az1, |
| 1220 | mapPublicIpOnLaunch: true, |
| 1221 | tags: { Name: "public-1a", Tier: "public" }, |
| 1222 | }); |
| 1223 | |
| 1224 | const publicSubnet2 = yield* Subnet("PublicSubnet2", { |
| 1225 | vpcId: myVpc.vpcId, |
| 1226 | cidrBlock: "10.0.2.0/24", |
| 1227 | availabilityZone: az2, |
| 1228 | mapPublicIpOnLaunch: true, |
| 1229 | tags: { Name: "public-1b", Tier: "public" }, |
| 1230 | }); |
| 1231 | |
| 1232 | // Private Subnets in two AZs |
| 1233 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 1234 | vpcId: myVpc.vpcId, |
| 1235 | cidrBlock: "10.0.10.0/24", |
| 1236 | availabilityZone: az1, |
| 1237 | tags: { Name: "private-1a", Tier: "private" }, |
| 1238 | }); |
| 1239 | |
| 1240 | const privateSubnet2 = yield* Subnet("PrivateSubnet2", { |
| 1241 | vpcId: myVpc.vpcId, |
| 1242 | cidrBlock: "10.0.11.0/24", |
| 1243 | availabilityZone: az2, |
| 1244 | tags: { Name: "private-1b", Tier: "private" }, |
| 1245 | }); |
| 1246 | |
| 1247 | // Route Tables |
| 1248 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 1249 | vpcId: myVpc.vpcId, |
| 1250 | tags: { Name: "public-rt" }, |
| 1251 | }); |
| 1252 | |
| 1253 | const privateRouteTable1 = yield* RouteTable("PrivateRouteTable1", { |
| 1254 | vpcId: myVpc.vpcId, |
| 1255 | tags: { Name: "private-rt-1" }, |
| 1256 | }); |
| 1257 | |
| 1258 | const privateRouteTable2 = yield* RouteTable("PrivateRouteTable2", { |
| 1259 | vpcId: myVpc.vpcId, |
| 1260 | tags: { Name: "private-rt-2" }, |
| 1261 | }); |
| 1262 | |
| 1263 | // Internet route for public subnets |
| 1264 | const internetRoute = yield* Route("InternetRoute", { |
| 1265 | routeTableId: publicRouteTable.routeTableId, |
| 1266 | destinationCidrBlock: "0.0.0.0/0", |
| 1267 | gatewayId: internetGateway.internetGatewayId, |
| 1268 | }); |
| 1269 | |
| 1270 | // NAT Gateway with EIP for AZ1 |
| 1271 | const natEip1 = yield* EIP("NatEip1", { |
| 1272 | tags: { Name: "nat-eip-1" }, |
| 1273 | }); |
| 1274 | |
| 1275 | const natGateway1 = yield* NatGateway("NatGateway1", { |
| 1276 | subnetId: publicSubnet1.subnetId, |
| 1277 | allocationId: natEip1.allocationId, |
| 1278 | tags: { Name: "nat-gateway-1" }, |
| 1279 | }); |
| 1280 | |
| 1281 | // NAT Gateway with EIP for AZ2 |
| 1282 | const natEip2 = yield* EIP("NatEip2", { |
| 1283 | tags: { Name: "nat-eip-2" }, |
| 1284 | }); |
| 1285 | |
| 1286 | const natGateway2 = yield* NatGateway("NatGateway2", { |
| 1287 | subnetId: publicSubnet2.subnetId, |
| 1288 | allocationId: natEip2.allocationId, |
| 1289 | tags: { Name: "nat-gateway-2" }, |
| 1290 | }); |
| 1291 | |
| 1292 | // NAT routes for private subnets (each AZ routes to its own NAT) |
| 1293 | const natRoute1 = yield* Route("NatRoute1", { |
| 1294 | routeTableId: privateRouteTable1.routeTableId, |
| 1295 | destinationCidrBlock: "0.0.0.0/0", |
| 1296 | natGatewayId: natGateway1.natGatewayId, |
| 1297 | }); |
| 1298 | |
| 1299 | const natRoute2 = yield* Route("NatRoute2", { |
| 1300 | routeTableId: privateRouteTable2.routeTableId, |
| 1301 | destinationCidrBlock: "0.0.0.0/0", |
| 1302 | natGatewayId: natGateway2.natGatewayId, |
| 1303 | }); |
| 1304 | |
| 1305 | // Route Table Associations |
| 1306 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 1307 | "PublicSubnet1Association", |
| 1308 | { |
| 1309 | routeTableId: publicRouteTable.routeTableId, |
| 1310 | subnetId: publicSubnet1.subnetId, |
| 1311 | }, |
| 1312 | ); |
| 1313 | |
| 1314 | const publicSubnet2Association = yield* RouteTableAssociation( |
| 1315 | "PublicSubnet2Association", |
| 1316 | { |
| 1317 | routeTableId: publicRouteTable.routeTableId, |
| 1318 | subnetId: publicSubnet2.subnetId, |
| 1319 | }, |
| 1320 | ); |
| 1321 | |
| 1322 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 1323 | "PrivateSubnet1Association", |
| 1324 | { |
| 1325 | routeTableId: privateRouteTable1.routeTableId, |
| 1326 | subnetId: privateSubnet1.subnetId, |
| 1327 | }, |
| 1328 | ); |
| 1329 | |
| 1330 | const privateSubnet2Association = yield* RouteTableAssociation( |
| 1331 | "PrivateSubnet2Association", |
| 1332 | { |
| 1333 | routeTableId: privateRouteTable2.routeTableId, |
| 1334 | subnetId: privateSubnet2.subnetId, |
| 1335 | }, |
| 1336 | ); |
| 1337 | |
| 1338 | // Security Groups |
| 1339 | const webSecurityGroup = yield* SecurityGroup("WebSecurityGroup", { |
| 1340 | vpcId: myVpc.vpcId, |
| 1341 | description: "Web tier security group", |
| 1342 | ingress: [ |
| 1343 | { |
| 1344 | ipProtocol: "tcp", |
| 1345 | fromPort: 80, |
| 1346 | toPort: 80, |
| 1347 | cidrIpv4: "0.0.0.0/0", |
| 1348 | description: "Allow HTTP", |
| 1349 | }, |
| 1350 | { |
| 1351 | ipProtocol: "tcp", |
| 1352 | fromPort: 443, |
| 1353 | toPort: 443, |
| 1354 | cidrIpv4: "0.0.0.0/0", |
| 1355 | description: "Allow HTTPS", |
| 1356 | }, |
| 1357 | ], |
| 1358 | egress: [ |
| 1359 | { |
| 1360 | ipProtocol: "-1", |
| 1361 | cidrIpv4: "0.0.0.0/0", |
| 1362 | description: "Allow all outbound", |
| 1363 | }, |
| 1364 | ], |
| 1365 | tags: { Name: "web-sg" }, |
| 1366 | }); |
| 1367 | |
| 1368 | const appSecurityGroup = yield* SecurityGroup("AppSecurityGroup", { |
| 1369 | vpcId: myVpc.vpcId, |
| 1370 | description: "Application tier security group", |
| 1371 | ingress: [ |
| 1372 | { |
| 1373 | ipProtocol: "tcp", |
| 1374 | fromPort: 8080, |
| 1375 | toPort: 8080, |
| 1376 | referencedGroupId: webSecurityGroup.groupId, |
| 1377 | description: "Allow from web tier", |
| 1378 | }, |
| 1379 | ], |
| 1380 | egress: [ |
| 1381 | { |
| 1382 | ipProtocol: "-1", |
| 1383 | cidrIpv4: "0.0.0.0/0", |
| 1384 | description: "Allow all outbound", |
| 1385 | }, |
| 1386 | ], |
| 1387 | tags: { Name: "app-sg" }, |
| 1388 | }); |
| 1389 | |
| 1390 | const dbSecurityGroup = yield* SecurityGroup("DbSecurityGroup", { |
| 1391 | vpcId: myVpc.vpcId, |
| 1392 | description: "Database tier security group", |
| 1393 | ingress: [ |
| 1394 | { |
| 1395 | ipProtocol: "tcp", |
| 1396 | fromPort: 5432, |
| 1397 | toPort: 5432, |
| 1398 | referencedGroupId: appSecurityGroup.groupId, |
| 1399 | description: "Allow PostgreSQL from app tier", |
| 1400 | }, |
| 1401 | ], |
| 1402 | egress: [ |
| 1403 | { |
| 1404 | ipProtocol: "-1", |
| 1405 | cidrIpv4: "0.0.0.0/0", |
| 1406 | description: "Allow all outbound", |
| 1407 | }, |
| 1408 | ], |
| 1409 | tags: { Name: "db-sg" }, |
| 1410 | }); |
| 1411 | |
| 1412 | // Network ACL for private subnets with custom rules |
| 1413 | const privateNetworkAcl = yield* NetworkAcl("PrivateNetworkAcl", { |
| 1414 | vpcId: myVpc.vpcId, |
| 1415 | tags: { Name: "private-nacl" }, |
| 1416 | }); |
| 1417 | |
| 1418 | // Network ACL Entries (rules) |
| 1419 | // Allow inbound traffic from VPC CIDR |
| 1420 | const privateNaclIngressVpc = yield* NetworkAclEntry("PrivateNaclIngressVpc", { |
| 1421 | networkAclId: privateNetworkAcl.networkAclId, |
| 1422 | ruleNumber: 100, |
| 1423 | protocol: "-1", // All protocols |
| 1424 | ruleAction: "allow", |
| 1425 | egress: false, |
| 1426 | cidrBlock: "10.0.0.0/16", |
| 1427 | }); |
| 1428 | |
| 1429 | // Allow inbound ephemeral ports (for NAT return traffic) |
| 1430 | const privateNaclIngressEphemeral = yield* NetworkAclEntry( |
| 1431 | "PrivateNaclIngressEphemeral", |
| 1432 | { |
| 1433 | networkAclId: privateNetworkAcl.networkAclId, |
| 1434 | ruleNumber: 200, |
| 1435 | protocol: "6", // TCP |
| 1436 | ruleAction: "allow", |
| 1437 | egress: false, |
| 1438 | cidrBlock: "0.0.0.0/0", |
| 1439 | portRange: { from: 1024, to: 65535 }, |
| 1440 | }, |
| 1441 | ); |
| 1442 | |
| 1443 | // Allow all outbound traffic |
| 1444 | const privateNaclEgressAll = yield* NetworkAclEntry("PrivateNaclEgressAll", { |
| 1445 | networkAclId: privateNetworkAcl.networkAclId, |
| 1446 | ruleNumber: 100, |
| 1447 | protocol: "-1", // All protocols |
| 1448 | ruleAction: "allow", |
| 1449 | egress: true, |
| 1450 | cidrBlock: "0.0.0.0/0", |
| 1451 | }); |
| 1452 | |
| 1453 | // Network ACL Associations - associate private subnets with the custom NACL |
| 1454 | const privateSubnet1NaclAssoc = yield* NetworkAclAssociation("PrivateSubnet1NaclAssoc", { |
| 1455 | networkAclId: privateNetworkAcl.networkAclId, |
| 1456 | subnetId: privateSubnet1.subnetId, |
| 1457 | }); |
| 1458 | |
| 1459 | const privateSubnet2NaclAssoc = yield* NetworkAclAssociation("PrivateSubnet2NaclAssoc", { |
| 1460 | networkAclId: privateNetworkAcl.networkAclId, |
| 1461 | subnetId: privateSubnet2.subnetId, |
| 1462 | }); |
| 1463 | |
| 1464 | // VPC Gateway Endpoint for S3 (reduces NAT costs and improves latency) |
| 1465 | const s3Endpoint = yield* VpcEndpoint("S3Endpoint", { |
| 1466 | vpcId: myVpc.vpcId, |
| 1467 | serviceName: `com.amazonaws.${ |
| 1468 | (yield* EC2.describeAvailabilityZones({})).AvailabilityZones?.[0]?.RegionName |
| 1469 | }.s3`, |
| 1470 | vpcEndpointType: "Gateway", |
| 1471 | routeTableIds: [privateRouteTable1.routeTableId, privateRouteTable2.routeTableId], |
| 1472 | tags: { Name: "s3-endpoint" }, |
| 1473 | }); |
| 1474 | |
| 1475 | return { |
| 1476 | myVpc, |
| 1477 | internetGateway, |
| 1478 | egressOnlyIgw, |
| 1479 | publicSubnet1, |
| 1480 | publicSubnet2, |
| 1481 | privateSubnet1, |
| 1482 | privateSubnet2, |
| 1483 | publicRouteTable, |
| 1484 | privateRouteTable1, |
| 1485 | privateRouteTable2, |
| 1486 | internetRoute, |
| 1487 | natEip1, |
| 1488 | natEip2, |
| 1489 | natGateway1, |
| 1490 | natGateway2, |
| 1491 | natRoute1, |
| 1492 | natRoute2, |
| 1493 | publicSubnet1Association, |
| 1494 | publicSubnet2Association, |
| 1495 | privateSubnet1Association, |
| 1496 | privateSubnet2Association, |
| 1497 | webSecurityGroup, |
| 1498 | appSecurityGroup, |
| 1499 | dbSecurityGroup, |
| 1500 | privateNetworkAcl, |
| 1501 | privateNaclIngressVpc, |
| 1502 | privateNaclIngressEphemeral, |
| 1503 | privateNaclEgressAll, |
| 1504 | privateSubnet1NaclAssoc, |
| 1505 | privateSubnet2NaclAssoc, |
| 1506 | s3Endpoint, |
| 1507 | }; |
| 1508 | }), |
| 1509 | ); |
| 1510 | |
| 1511 | // ========================================================================= |
| 1512 | // Verify VPC |
| 1513 | // ========================================================================= |
| 1514 | expect(_stack.myVpc.vpcId).toMatch(/^vpc-/); |
| 1515 | expect(_stack.myVpc.cidrBlock).toEqual("10.0.0.0/16"); |
| 1516 | expect(_stack.myVpc.state).toEqual("available"); |
| 1517 | |
| 1518 | const vpcResult = yield* EC2.describeVpcs({ |
| 1519 | VpcIds: [_stack.myVpc.vpcId], |
| 1520 | }); |
| 1521 | expect(vpcResult.Vpcs?.[0]?.CidrBlock).toEqual("10.0.0.0/16"); |
| 1522 | // EnableDnsSupport and EnableDnsHostnames are not present in the describeVpcs output. |
| 1523 | // Instead, use describeVpcAttribute for these: |
| 1524 | const dnsSupport = yield* EC2.describeVpcAttribute({ |
| 1525 | VpcId: _stack.myVpc.vpcId, |
| 1526 | Attribute: "enableDnsSupport", |
| 1527 | }); |
| 1528 | expect(dnsSupport.EnableDnsSupport?.Value).toBeTruthy(); |
| 1529 | |
| 1530 | const dnsHostnames = yield* EC2.describeVpcAttribute({ |
| 1531 | VpcId: _stack.myVpc.vpcId, |
| 1532 | Attribute: "enableDnsHostnames", |
| 1533 | }); |
| 1534 | expect(dnsHostnames.EnableDnsHostnames?.Value).toBeTruthy(); |
| 1535 | |
| 1536 | // Verify VPC has IPv6 CIDR block |
| 1537 | expect(_stack.myVpc.ipv6CidrBlockAssociationSet).toBeDefined(); |
| 1538 | expect(_stack.myVpc.ipv6CidrBlockAssociationSet?.length).toBeGreaterThan(0); |
| 1539 | |
| 1540 | // ========================================================================= |
| 1541 | // Verify Internet Gateway |
| 1542 | // ========================================================================= |
| 1543 | expect(_stack.internetGateway.internetGatewayId).toMatch(/^igw-/); |
| 1544 | expect(_stack.internetGateway.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1545 | |
| 1546 | // ========================================================================= |
| 1547 | // Verify Egress-Only Internet Gateway |
| 1548 | // ========================================================================= |
| 1549 | expect(_stack.egressOnlyIgw.egressOnlyInternetGatewayId).toMatch(/^eigw-/); |
| 1550 | expect(_stack.egressOnlyIgw.attachments).toBeDefined(); |
| 1551 | expect(_stack.egressOnlyIgw.attachments?.[0]?.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1552 | |
| 1553 | // ========================================================================= |
| 1554 | // Verify Subnets |
| 1555 | // ========================================================================= |
| 1556 | expect(_stack.publicSubnet1.subnetId).toMatch(/^subnet-/); |
| 1557 | expect(_stack.publicSubnet1.availabilityZone).toEqual(az1); |
| 1558 | expect(_stack.publicSubnet1.mapPublicIpOnLaunch).toEqual(true); |
| 1559 | |
| 1560 | expect(_stack.publicSubnet2.subnetId).toMatch(/^subnet-/); |
| 1561 | expect(_stack.publicSubnet2.availabilityZone).toEqual(az2); |
| 1562 | expect(_stack.publicSubnet2.mapPublicIpOnLaunch).toEqual(true); |
| 1563 | |
| 1564 | expect(_stack.privateSubnet1.subnetId).toMatch(/^subnet-/); |
| 1565 | expect(_stack.privateSubnet1.availabilityZone).toEqual(az1); |
| 1566 | expect(_stack.privateSubnet1.mapPublicIpOnLaunch).toBeFalsy(); |
| 1567 | |
| 1568 | expect(_stack.privateSubnet2.subnetId).toMatch(/^subnet-/); |
| 1569 | expect(_stack.privateSubnet2.availabilityZone).toEqual(az2); |
| 1570 | expect(_stack.privateSubnet2.mapPublicIpOnLaunch).toBeFalsy(); |
| 1571 | |
| 1572 | // Verify 4 subnets total |
| 1573 | const subnetsResult = yield* EC2.describeSubnets({ |
| 1574 | Filters: [{ Name: "vpc-id", Values: [_stack.myVpc.vpcId] }], |
| 1575 | }); |
| 1576 | expect(subnetsResult.Subnets).toHaveLength(4); |
| 1577 | |
| 1578 | // ========================================================================= |
| 1579 | // Verify NAT Gateways and EIPs |
| 1580 | // ========================================================================= |
| 1581 | expect(_stack.natEip1.allocationId).toMatch(/^eipalloc-/); |
| 1582 | expect(_stack.natEip1.publicIp).toBeDefined(); |
| 1583 | |
| 1584 | expect(_stack.natEip2.allocationId).toMatch(/^eipalloc-/); |
| 1585 | expect(_stack.natEip2.publicIp).toBeDefined(); |
| 1586 | |
| 1587 | expect(_stack.natGateway1.natGatewayId).toMatch(/^nat-/); |
| 1588 | expect(_stack.natGateway1.state).toEqual("available"); |
| 1589 | expect(_stack.natGateway1.publicIp).toEqual(_stack.natEip1.publicIp); |
| 1590 | expect(_stack.natGateway1.subnetId).toEqual(_stack.publicSubnet1.subnetId); |
| 1591 | |
| 1592 | expect(_stack.natGateway2.natGatewayId).toMatch(/^nat-/); |
| 1593 | expect(_stack.natGateway2.state).toEqual("available"); |
| 1594 | expect(_stack.natGateway2.publicIp).toEqual(_stack.natEip2.publicIp); |
| 1595 | expect(_stack.natGateway2.subnetId).toEqual(_stack.publicSubnet2.subnetId); |
| 1596 | |
| 1597 | // ========================================================================= |
| 1598 | // Verify Routes |
| 1599 | // ========================================================================= |
| 1600 | // Internet route to IGW |
| 1601 | expect(_stack.internetRoute.state).toEqual("active"); |
| 1602 | expect(_stack.internetRoute.gatewayId).toEqual(_stack.internetGateway.internetGatewayId); |
| 1603 | |
| 1604 | // NAT routes |
| 1605 | expect(_stack.natRoute1.state).toEqual("active"); |
| 1606 | expect(_stack.natRoute1.natGatewayId).toEqual(_stack.natGateway1.natGatewayId); |
| 1607 | |
| 1608 | expect(_stack.natRoute2.state).toEqual("active"); |
| 1609 | expect(_stack.natRoute2.natGatewayId).toEqual(_stack.natGateway2.natGatewayId); |
| 1610 | |
| 1611 | // Verify public route table has internet route |
| 1612 | const publicRtResult = yield* EC2.describeRouteTables({ |
| 1613 | RouteTableIds: [_stack.publicRouteTable.routeTableId], |
| 1614 | }); |
| 1615 | const publicRoutes = publicRtResult.RouteTables?.[0]?.Routes ?? []; |
| 1616 | const publicInternetRoute = publicRoutes.find((r) => r.DestinationCidrBlock === "0.0.0.0/0"); |
| 1617 | expect(publicInternetRoute?.GatewayId).toEqual(_stack.internetGateway.internetGatewayId); |
| 1618 | |
| 1619 | // Verify private route tables have NAT routes |
| 1620 | const private1RtResult = yield* EC2.describeRouteTables({ |
| 1621 | RouteTableIds: [_stack.privateRouteTable1.routeTableId], |
| 1622 | }); |
| 1623 | const private1Routes = private1RtResult.RouteTables?.[0]?.Routes ?? []; |
| 1624 | const private1NatRoute = private1Routes.find((r) => r.DestinationCidrBlock === "0.0.0.0/0"); |
| 1625 | expect(private1NatRoute?.NatGatewayId).toEqual(_stack.natGateway1.natGatewayId); |
| 1626 | |
| 1627 | const private2RtResult = yield* EC2.describeRouteTables({ |
| 1628 | RouteTableIds: [_stack.privateRouteTable2.routeTableId], |
| 1629 | }); |
| 1630 | const private2Routes = private2RtResult.RouteTables?.[0]?.Routes ?? []; |
| 1631 | const private2NatRoute = private2Routes.find((r) => r.DestinationCidrBlock === "0.0.0.0/0"); |
| 1632 | expect(private2NatRoute?.NatGatewayId).toEqual(_stack.natGateway2.natGatewayId); |
| 1633 | |
| 1634 | // ========================================================================= |
| 1635 | // Verify Route Table Associations |
| 1636 | // ========================================================================= |
| 1637 | expect(_stack.publicSubnet1Association.associationId).toMatch(/^rtbassoc-/); |
| 1638 | expect(_stack.publicSubnet2Association.associationId).toMatch(/^rtbassoc-/); |
| 1639 | expect(_stack.privateSubnet1Association.associationId).toMatch(/^rtbassoc-/); |
| 1640 | expect(_stack.privateSubnet2Association.associationId).toMatch(/^rtbassoc-/); |
| 1641 | |
| 1642 | // Both public subnets share the same route table |
| 1643 | expect(_stack.publicSubnet1Association.routeTableId).toEqual( |
| 1644 | _stack.publicSubnet2Association.routeTableId, |
| 1645 | ); |
| 1646 | |
| 1647 | // Private subnets have their own route tables (for HA NAT) |
| 1648 | expect(_stack.privateSubnet1Association.routeTableId).not.toEqual( |
| 1649 | _stack.privateSubnet2Association.routeTableId, |
| 1650 | ); |
| 1651 | |
| 1652 | // ========================================================================= |
| 1653 | // Verify Security Groups |
| 1654 | // ========================================================================= |
| 1655 | expect(_stack.webSecurityGroup.groupId).toMatch(/^sg-/); |
| 1656 | expect(_stack.webSecurityGroup.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1657 | expect(_stack.webSecurityGroup.ingressRules).toHaveLength(2); |
| 1658 | // TODO(sam): why is it 2 when we only have 1? is it a default or egress only ingress? |
| 1659 | expect(_stack.webSecurityGroup.egressRules).toHaveLength(2); |
| 1660 | |
| 1661 | expect(_stack.appSecurityGroup.groupId).toMatch(/^sg-/); |
| 1662 | expect(_stack.appSecurityGroup.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1663 | expect(_stack.appSecurityGroup.ingressRules).toHaveLength(1); |
| 1664 | expect(_stack.appSecurityGroup.ingressRules?.[0]?.referencedGroupId).toEqual( |
| 1665 | _stack.webSecurityGroup.groupId, |
| 1666 | ); |
| 1667 | |
| 1668 | expect(_stack.dbSecurityGroup.groupId).toMatch(/^sg-/); |
| 1669 | expect(_stack.dbSecurityGroup.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1670 | expect(_stack.dbSecurityGroup.ingressRules).toHaveLength(1); |
| 1671 | expect(_stack.dbSecurityGroup.ingressRules?.[0]?.referencedGroupId).toEqual( |
| 1672 | _stack.appSecurityGroup.groupId, |
| 1673 | ); |
| 1674 | |
| 1675 | // Verify security groups in AWS |
| 1676 | const sgResult = yield* EC2.describeSecurityGroups({ |
| 1677 | Filters: [{ Name: "vpc-id", Values: [_stack.myVpc.vpcId] }], |
| 1678 | }); |
| 1679 | // 4 security groups: default + web + app + db |
| 1680 | expect(sgResult.SecurityGroups).toHaveLength(4); |
| 1681 | |
| 1682 | // ========================================================================= |
| 1683 | // Verify Network ACL |
| 1684 | // ========================================================================= |
| 1685 | expect(_stack.privateNetworkAcl.networkAclId).toMatch(/^acl-/); |
| 1686 | expect(_stack.privateNetworkAcl.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1687 | expect(_stack.privateNetworkAcl.isDefault).toEqual(false); |
| 1688 | |
| 1689 | // Verify Network ACL in AWS |
| 1690 | const naclResult = yield* EC2.describeNetworkAcls({ |
| 1691 | NetworkAclIds: [_stack.privateNetworkAcl.networkAclId], |
| 1692 | }); |
| 1693 | expect(naclResult.NetworkAcls).toHaveLength(1); |
| 1694 | expect(naclResult.NetworkAcls?.[0]?.VpcId).toEqual(_stack.myVpc.vpcId); |
| 1695 | |
| 1696 | // ========================================================================= |
| 1697 | // Verify Network ACL Entries |
| 1698 | // ========================================================================= |
| 1699 | expect(_stack.privateNaclIngressVpc.networkAclId).toEqual( |
| 1700 | _stack.privateNetworkAcl.networkAclId, |
| 1701 | ); |
| 1702 | expect(_stack.privateNaclIngressVpc.ruleNumber).toEqual(100); |
| 1703 | expect(_stack.privateNaclIngressVpc.egress).toEqual(false); |
| 1704 | expect(_stack.privateNaclIngressVpc.ruleAction).toEqual("allow"); |
| 1705 | |
| 1706 | expect(_stack.privateNaclIngressEphemeral.ruleNumber).toEqual(200); |
| 1707 | expect(_stack.privateNaclIngressEphemeral.portRange).toEqual({ |
| 1708 | from: 1024, |
| 1709 | to: 65535, |
| 1710 | }); |
| 1711 | |
| 1712 | expect(_stack.privateNaclEgressAll.egress).toEqual(true); |
| 1713 | expect(_stack.privateNaclEgressAll.ruleNumber).toEqual(100); |
| 1714 | |
| 1715 | // ========================================================================= |
| 1716 | // Verify Network ACL Associations |
| 1717 | // ========================================================================= |
| 1718 | expect(_stack.privateSubnet1NaclAssoc.associationId).toMatch(/^aclassoc-/); |
| 1719 | expect(_stack.privateSubnet1NaclAssoc.networkAclId).toEqual( |
| 1720 | _stack.privateNetworkAcl.networkAclId, |
| 1721 | ); |
| 1722 | expect(_stack.privateSubnet1NaclAssoc.subnetId).toEqual(_stack.privateSubnet1.subnetId); |
| 1723 | |
| 1724 | expect(_stack.privateSubnet2NaclAssoc.associationId).toMatch(/^aclassoc-/); |
| 1725 | expect(_stack.privateSubnet2NaclAssoc.subnetId).toEqual(_stack.privateSubnet2.subnetId); |
| 1726 | |
| 1727 | // ========================================================================= |
| 1728 | // Verify VPC Endpoint for S3 |
| 1729 | // ========================================================================= |
| 1730 | expect(_stack.s3Endpoint.vpcEndpointId).toMatch(/^vpce-/); |
| 1731 | expect(_stack.s3Endpoint.vpcEndpointType).toEqual("Gateway"); |
| 1732 | expect(_stack.s3Endpoint.vpcId).toEqual(_stack.myVpc.vpcId); |
| 1733 | expect(_stack.s3Endpoint.state).toEqual("available"); |
| 1734 | expect(_stack.s3Endpoint.routeTableIds).toContain(_stack.privateRouteTable1.routeTableId); |
| 1735 | expect(_stack.s3Endpoint.routeTableIds).toContain(_stack.privateRouteTable2.routeTableId); |
| 1736 | |
| 1737 | // Verify VPC Endpoint in AWS |
| 1738 | const vpceResult = yield* EC2.describeVpcEndpoints({ |
| 1739 | VpcEndpointIds: [_stack.s3Endpoint.vpcEndpointId], |
| 1740 | }); |
| 1741 | expect(vpceResult.VpcEndpoints).toHaveLength(1); |
| 1742 | expect(vpceResult.VpcEndpoints?.[0]?.VpcEndpointType).toEqual("Gateway"); |
| 1743 | |
| 1744 | // ========================================================================= |
| 1745 | // Verify Tags |
| 1746 | // ========================================================================= |
| 1747 | yield* assertVpcTags(_stack.myVpc.vpcId, { |
| 1748 | Name: "comprehensive-vpc", |
| 1749 | Environment: "test", |
| 1750 | }); |
| 1751 | |
| 1752 | // ========================================================================= |
| 1753 | // Idempotency check - apply again and verify no changes |
| 1754 | // ========================================================================= |
| 1755 | yield* Effect.log("=== Idempotency Check: Re-applying stack ==="); |
| 1756 | const stack2 = yield* stack.deploy( |
| 1757 | Effect.gen(function* () { |
| 1758 | // VPC with DNS enabled and IPv6 for egress-only IGW |
| 1759 | const myVpc = yield* Vpc("MyVpc", { |
| 1760 | cidrBlock: "10.0.0.0/16", |
| 1761 | enableDnsSupport: true, |
| 1762 | enableDnsHostnames: true, |
| 1763 | amazonProvidedIpv6CidrBlock: true, |
| 1764 | tags: { |
| 1765 | Name: "comprehensive-vpc", |
| 1766 | Environment: "test", |
| 1767 | }, |
| 1768 | }); |
| 1769 | |
| 1770 | // Internet Gateway for public internet access |
| 1771 | const internetGateway = yield* InternetGateway("InternetGateway", { |
| 1772 | vpcId: myVpc.vpcId, |
| 1773 | tags: { Name: "comprehensive-igw" }, |
| 1774 | }); |
| 1775 | |
| 1776 | // Egress-Only Internet Gateway for IPv6 outbound traffic from private subnets |
| 1777 | const egressOnlyIgw = yield* EgressOnlyInternetGateway("EgressOnlyIgw", { |
| 1778 | vpcId: myVpc.vpcId, |
| 1779 | tags: { Name: "comprehensive-eigw" }, |
| 1780 | }); |
| 1781 | |
| 1782 | // Public Subnets in two AZs |
| 1783 | const publicSubnet1 = yield* Subnet("PublicSubnet1", { |
| 1784 | vpcId: myVpc.vpcId, |
| 1785 | cidrBlock: "10.0.1.0/24", |
| 1786 | availabilityZone: az1, |
| 1787 | mapPublicIpOnLaunch: true, |
| 1788 | tags: { Name: "public-1a", Tier: "public" }, |
| 1789 | }); |
| 1790 | |
| 1791 | const publicSubnet2 = yield* Subnet("PublicSubnet2", { |
| 1792 | vpcId: myVpc.vpcId, |
| 1793 | cidrBlock: "10.0.2.0/24", |
| 1794 | availabilityZone: az2, |
| 1795 | mapPublicIpOnLaunch: true, |
| 1796 | tags: { Name: "public-1b", Tier: "public" }, |
| 1797 | }); |
| 1798 | |
| 1799 | // Private Subnets in two AZs |
| 1800 | const privateSubnet1 = yield* Subnet("PrivateSubnet1", { |
| 1801 | vpcId: myVpc.vpcId, |
| 1802 | cidrBlock: "10.0.10.0/24", |
| 1803 | availabilityZone: az1, |
| 1804 | tags: { Name: "private-1a", Tier: "private" }, |
| 1805 | }); |
| 1806 | |
| 1807 | const privateSubnet2 = yield* Subnet("PrivateSubnet2", { |
| 1808 | vpcId: myVpc.vpcId, |
| 1809 | cidrBlock: "10.0.11.0/24", |
| 1810 | availabilityZone: az2, |
| 1811 | tags: { Name: "private-1b", Tier: "private" }, |
| 1812 | }); |
| 1813 | |
| 1814 | // Route Tables |
| 1815 | const publicRouteTable = yield* RouteTable("PublicRouteTable", { |
| 1816 | vpcId: myVpc.vpcId, |
| 1817 | tags: { Name: "public-rt" }, |
| 1818 | }); |
| 1819 | |
| 1820 | const privateRouteTable1 = yield* RouteTable("PrivateRouteTable1", { |
| 1821 | vpcId: myVpc.vpcId, |
| 1822 | tags: { Name: "private-rt-1" }, |
| 1823 | }); |
| 1824 | |
| 1825 | const privateRouteTable2 = yield* RouteTable("PrivateRouteTable2", { |
| 1826 | vpcId: myVpc.vpcId, |
| 1827 | tags: { Name: "private-rt-2" }, |
| 1828 | }); |
| 1829 | |
| 1830 | // Internet route for public subnets |
| 1831 | const internetRoute = yield* Route("InternetRoute", { |
| 1832 | routeTableId: publicRouteTable.routeTableId, |
| 1833 | destinationCidrBlock: "0.0.0.0/0", |
| 1834 | gatewayId: internetGateway.internetGatewayId, |
| 1835 | }); |
| 1836 | |
| 1837 | // NAT Gateway with EIP for AZ1 |
| 1838 | const natEip1 = yield* EIP("NatEip1", { |
| 1839 | tags: { Name: "nat-eip-1" }, |
| 1840 | }); |
| 1841 | |
| 1842 | const natGateway1 = yield* NatGateway("NatGateway1", { |
| 1843 | subnetId: publicSubnet1.subnetId, |
| 1844 | allocationId: natEip1.allocationId, |
| 1845 | tags: { Name: "nat-gateway-1" }, |
| 1846 | }); |
| 1847 | |
| 1848 | // NAT Gateway with EIP for AZ2 |
| 1849 | const natEip2 = yield* EIP("NatEip2", { |
| 1850 | tags: { Name: "nat-eip-2" }, |
| 1851 | }); |
| 1852 | |
| 1853 | const natGateway2 = yield* NatGateway("NatGateway2", { |
| 1854 | subnetId: publicSubnet2.subnetId, |
| 1855 | allocationId: natEip2.allocationId, |
| 1856 | tags: { Name: "nat-gateway-2" }, |
| 1857 | }); |
| 1858 | |
| 1859 | // NAT routes for private subnets (each AZ routes to its own NAT) |
| 1860 | const natRoute1 = yield* Route("NatRoute1", { |
| 1861 | routeTableId: privateRouteTable1.routeTableId, |
| 1862 | destinationCidrBlock: "0.0.0.0/0", |
| 1863 | natGatewayId: natGateway1.natGatewayId, |
| 1864 | }); |
| 1865 | |
| 1866 | const natRoute2 = yield* Route("NatRoute2", { |
| 1867 | routeTableId: privateRouteTable2.routeTableId, |
| 1868 | destinationCidrBlock: "0.0.0.0/0", |
| 1869 | natGatewayId: natGateway2.natGatewayId, |
| 1870 | }); |
| 1871 | |
| 1872 | // Route Table Associations |
| 1873 | const publicSubnet1Association = yield* RouteTableAssociation( |
| 1874 | "PublicSubnet1Association", |
| 1875 | { |
| 1876 | routeTableId: publicRouteTable.routeTableId, |
| 1877 | subnetId: publicSubnet1.subnetId, |
| 1878 | }, |
| 1879 | ); |
| 1880 | |
| 1881 | const publicSubnet2Association = yield* RouteTableAssociation( |
| 1882 | "PublicSubnet2Association", |
| 1883 | { |
| 1884 | routeTableId: publicRouteTable.routeTableId, |
| 1885 | subnetId: publicSubnet2.subnetId, |
| 1886 | }, |
| 1887 | ); |
| 1888 | |
| 1889 | const privateSubnet1Association = yield* RouteTableAssociation( |
| 1890 | "PrivateSubnet1Association", |
| 1891 | { |
| 1892 | routeTableId: privateRouteTable1.routeTableId, |
| 1893 | subnetId: privateSubnet1.subnetId, |
| 1894 | }, |
| 1895 | ); |
| 1896 | |
| 1897 | const privateSubnet2Association = yield* RouteTableAssociation( |
| 1898 | "PrivateSubnet2Association", |
| 1899 | { |
| 1900 | routeTableId: privateRouteTable2.routeTableId, |
| 1901 | subnetId: privateSubnet2.subnetId, |
| 1902 | }, |
| 1903 | ); |
| 1904 | |
| 1905 | // Security Groups |
| 1906 | const webSecurityGroup = yield* SecurityGroup("WebSecurityGroup", { |
| 1907 | vpcId: myVpc.vpcId, |
| 1908 | description: "Web tier security group", |
| 1909 | ingress: [ |
| 1910 | { |
| 1911 | ipProtocol: "tcp", |
| 1912 | fromPort: 80, |
| 1913 | toPort: 80, |
| 1914 | cidrIpv4: "0.0.0.0/0", |
| 1915 | description: "Allow HTTP", |
| 1916 | }, |
| 1917 | { |
| 1918 | ipProtocol: "tcp", |
| 1919 | fromPort: 443, |
| 1920 | toPort: 443, |
| 1921 | cidrIpv4: "0.0.0.0/0", |
| 1922 | description: "Allow HTTPS", |
| 1923 | }, |
| 1924 | ], |
| 1925 | egress: [ |
| 1926 | { |
| 1927 | ipProtocol: "-1", |
| 1928 | cidrIpv4: "0.0.0.0/0", |
| 1929 | description: "Allow all outbound", |
| 1930 | }, |
| 1931 | ], |
| 1932 | tags: { Name: "web-sg" }, |
| 1933 | }); |
| 1934 | |
| 1935 | const appSecurityGroup = yield* SecurityGroup("AppSecurityGroup", { |
| 1936 | vpcId: myVpc.vpcId, |
| 1937 | description: "Application tier security group", |
| 1938 | ingress: [ |
| 1939 | { |
| 1940 | ipProtocol: "tcp", |
| 1941 | fromPort: 8080, |
| 1942 | toPort: 8080, |
| 1943 | referencedGroupId: webSecurityGroup.groupId, |
| 1944 | description: "Allow from web tier", |
| 1945 | }, |
| 1946 | ], |
| 1947 | egress: [ |
| 1948 | { |
| 1949 | ipProtocol: "-1", |
| 1950 | cidrIpv4: "0.0.0.0/0", |
| 1951 | description: "Allow all outbound", |
| 1952 | }, |
| 1953 | ], |
| 1954 | tags: { Name: "app-sg" }, |
| 1955 | }); |
| 1956 | |
| 1957 | const dbSecurityGroup = yield* SecurityGroup("DbSecurityGroup", { |
| 1958 | vpcId: myVpc.vpcId, |
| 1959 | description: "Database tier security group", |
| 1960 | ingress: [ |
| 1961 | { |
| 1962 | ipProtocol: "tcp", |
| 1963 | fromPort: 5432, |
| 1964 | toPort: 5432, |
| 1965 | referencedGroupId: appSecurityGroup.groupId, |
| 1966 | description: "Allow PostgreSQL from app tier", |
| 1967 | }, |
| 1968 | ], |
| 1969 | egress: [ |
| 1970 | { |
| 1971 | ipProtocol: "-1", |
| 1972 | cidrIpv4: "0.0.0.0/0", |
| 1973 | description: "Allow all outbound", |
| 1974 | }, |
| 1975 | ], |
| 1976 | tags: { Name: "db-sg" }, |
| 1977 | }); |
| 1978 | |
| 1979 | // Network ACL for private subnets with custom rules |
| 1980 | const privateNetworkAcl = yield* NetworkAcl("PrivateNetworkAcl", { |
| 1981 | vpcId: myVpc.vpcId, |
| 1982 | tags: { Name: "private-nacl" }, |
| 1983 | }); |
| 1984 | |
| 1985 | // Network ACL Entries (rules) |
| 1986 | // Allow inbound traffic from VPC CIDR |
| 1987 | const privateNaclIngressVpc = yield* NetworkAclEntry("PrivateNaclIngressVpc", { |
| 1988 | networkAclId: privateNetworkAcl.networkAclId, |
| 1989 | ruleNumber: 100, |
| 1990 | protocol: "-1", // All protocols |
| 1991 | ruleAction: "allow", |
| 1992 | egress: false, |
| 1993 | cidrBlock: "10.0.0.0/16", |
| 1994 | }); |
| 1995 | |
| 1996 | // Allow inbound ephemeral ports (for NAT return traffic) |
| 1997 | const privateNaclIngressEphemeral = yield* NetworkAclEntry( |
| 1998 | "PrivateNaclIngressEphemeral", |
| 1999 | { |
| 2000 | networkAclId: privateNetworkAcl.networkAclId, |
| 2001 | ruleNumber: 200, |
| 2002 | protocol: "6", // TCP |
| 2003 | ruleAction: "allow", |
| 2004 | egress: false, |
| 2005 | cidrBlock: "0.0.0.0/0", |
| 2006 | portRange: { from: 1024, to: 65535 }, |
| 2007 | }, |
| 2008 | ); |
| 2009 | |
| 2010 | // Allow all outbound traffic |
| 2011 | const privateNaclEgressAll = yield* NetworkAclEntry("PrivateNaclEgressAll", { |
| 2012 | networkAclId: privateNetworkAcl.networkAclId, |
| 2013 | ruleNumber: 100, |
| 2014 | protocol: "-1", // All protocols |
| 2015 | ruleAction: "allow", |
| 2016 | egress: true, |
| 2017 | cidrBlock: "0.0.0.0/0", |
| 2018 | }); |
| 2019 | |
| 2020 | // Network ACL Associations - associate private subnets with the custom NACL |
| 2021 | const privateSubnet1NaclAssoc = yield* NetworkAclAssociation("PrivateSubnet1NaclAssoc", { |
| 2022 | networkAclId: privateNetworkAcl.networkAclId, |
| 2023 | subnetId: privateSubnet1.subnetId, |
| 2024 | }); |
| 2025 | |
| 2026 | const privateSubnet2NaclAssoc = yield* NetworkAclAssociation("PrivateSubnet2NaclAssoc", { |
| 2027 | networkAclId: privateNetworkAcl.networkAclId, |
| 2028 | subnetId: privateSubnet2.subnetId, |
| 2029 | }); |
| 2030 | |
| 2031 | // VPC Gateway Endpoint for S3 (reduces NAT costs and improves latency) |
| 2032 | const s3Endpoint = yield* VpcEndpoint("S3Endpoint", { |
| 2033 | vpcId: myVpc.vpcId, |
| 2034 | serviceName: `com.amazonaws.${ |
| 2035 | (yield* EC2.describeAvailabilityZones({})).AvailabilityZones?.[0]?.RegionName |
| 2036 | }.s3`, |
| 2037 | vpcEndpointType: "Gateway", |
| 2038 | routeTableIds: [privateRouteTable1.routeTableId, privateRouteTable2.routeTableId], |
| 2039 | tags: { Name: "s3-endpoint" }, |
| 2040 | }); |
| 2041 | |
| 2042 | return { |
| 2043 | myVpc, |
| 2044 | internetGateway, |
| 2045 | egressOnlyIgw, |
| 2046 | publicSubnet1, |
| 2047 | publicSubnet2, |
| 2048 | privateSubnet1, |
| 2049 | privateSubnet2, |
| 2050 | publicRouteTable, |
| 2051 | privateRouteTable1, |
| 2052 | privateRouteTable2, |
| 2053 | internetRoute, |
| 2054 | natEip1, |
| 2055 | natEip2, |
| 2056 | natGateway1, |
| 2057 | natGateway2, |
| 2058 | natRoute1, |
| 2059 | natRoute2, |
| 2060 | publicSubnet1Association, |
| 2061 | publicSubnet2Association, |
| 2062 | privateSubnet1Association, |
| 2063 | privateSubnet2Association, |
| 2064 | webSecurityGroup, |
| 2065 | appSecurityGroup, |
| 2066 | dbSecurityGroup, |
| 2067 | privateNetworkAcl, |
| 2068 | privateNaclIngressVpc, |
| 2069 | privateNaclIngressEphemeral, |
| 2070 | privateNaclEgressAll, |
| 2071 | privateSubnet1NaclAssoc, |
| 2072 | privateSubnet2NaclAssoc, |
| 2073 | s3Endpoint, |
| 2074 | }; |
| 2075 | }), |
| 2076 | ); |
| 2077 | |
| 2078 | // All IDs should remain the same |
| 2079 | expect(stack2.myVpc.vpcId).toEqual(_stack.myVpc.vpcId); |
| 2080 | expect(stack2.internetGateway.internetGatewayId).toEqual( |
| 2081 | _stack.internetGateway.internetGatewayId, |
| 2082 | ); |
| 2083 | expect(stack2.egressOnlyIgw.egressOnlyInternetGatewayId).toEqual( |
| 2084 | _stack.egressOnlyIgw.egressOnlyInternetGatewayId, |
| 2085 | ); |
| 2086 | expect(stack2.natGateway1.natGatewayId).toEqual(_stack.natGateway1.natGatewayId); |
| 2087 | expect(stack2.natGateway2.natGatewayId).toEqual(_stack.natGateway2.natGatewayId); |
| 2088 | expect(stack2.privateNetworkAcl.networkAclId).toEqual(_stack.privateNetworkAcl.networkAclId); |
| 2089 | expect(stack2.s3Endpoint.vpcEndpointId).toEqual(_stack.s3Endpoint.vpcEndpointId); |
| 2090 | |
| 2091 | // ========================================================================= |
| 2092 | // Cleanup |
| 2093 | // ========================================================================= |
| 2094 | yield* Effect.log("=== Cleanup: Destroying all resources ==="); |
| 2095 | const capturedVpcId = _stack.myVpc.vpcId; |
| 2096 | |
| 2097 | yield* stack.destroy(); |
| 2098 | |
| 2099 | // Verify VPC is deleted |
| 2100 | yield* EC2.describeVpcs({ VpcIds: [capturedVpcId] }).pipe( |
| 2101 | Effect.flatMap(() => Effect.fail(new Error("VPC still exists"))), |
| 2102 | Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void), |
| 2103 | ); |
| 2104 | |
| 2105 | yield* Effect.log("=== Comprehensive VPC test completed successfully! ==="); |
| 2106 | }).pipe(logLevel), |
| 2107 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 1_000_000 }, |
| 2108 | ); |
| 2109 | |
| 2110 | // ============================================================================ |
| 2111 | // Eventually Consistent Check Utilities |
| 2112 | // ============================================================================ |
| 2113 | |
| 2114 | class TagsNotPropagated extends Data.TaggedError("TagsNotPropagated")<{ |
| 2115 | readonly expected: Record<string, string>; |
| 2116 | readonly actual: Record<string, string | undefined>; |
| 2117 | }> {} |
| 2118 | |
| 2119 | /** |
| 2120 | * Asserts that a VPC has the expected tags, retrying until eventually consistent. |
| 2121 | */ |
| 2122 | const assertVpcTags = Effect.fn(function* (vpcId: string, expectedTags: Record<string, string>) { |
| 2123 | yield* EC2.describeVpcs({ VpcIds: [vpcId] }).pipe( |
| 2124 | Effect.flatMap((result) => { |
| 2125 | const tags = result.Vpcs?.[0]?.Tags ?? []; |
| 2126 | const actual: Record<string, string | undefined> = {}; |
| 2127 | |
| 2128 | for (const key of Object.keys(expectedTags)) { |
| 2129 | actual[key] = tags.find((t) => t.Key === key)?.Value; |
| 2130 | } |
| 2131 | |
| 2132 | const allMatch = Object.entries(expectedTags).every(([key, value]) => actual[key] === value); |
| 2133 | |
| 2134 | return allMatch |
| 2135 | ? Effect.succeed(result) |
| 2136 | : Effect.fail(new TagsNotPropagated({ expected: expectedTags, actual })); |
| 2137 | }), |
| 2138 | Effect.tapError(Effect.log), |
| 2139 | Effect.retry({ |
| 2140 | while: (e) => e._tag === "TagsNotPropagated", |
| 2141 | schedule: Schedule.max([Schedule.fixed(1000), Schedule.recurs(10)]), |
| 2142 | }), |
| 2143 | ); |
| 2144 | }); |