InternetGatewayAttachment
An AWS Internet Gateway Attachment creates the connection between an Internet Gateway and a VPC, enabling internet connectivity for resources within the VPC.
Minimal Example
Section titled “Minimal Example”Attach an Internet Gateway to a VPC:
import { Vpc, InternetGateway, InternetGatewayAttachment} from "alchemy/aws/ec2";
const vpc = await Vpc("main-vpc", { cidrBlock: "10.0.0.0/16"});
const igw = await InternetGateway("main-igw", {});
const attachment = await InternetGatewayAttachment("main-igw-attachment", { internetGateway: igw, vpc: vpc});
Reference by IDs
Section titled “Reference by IDs”Attach using existing resource IDs:
import { InternetGatewayAttachment } from "alchemy/aws/ec2";
const attachment = await InternetGatewayAttachment("existing-igw-attachment", { internetGateway: "igw-1234567890abcdef0", vpc: "vpc-0987654321fedcba0"});
Complete Internet Setup
Section titled “Complete Internet Setup”Full setup with routing for internet connectivity:
import { Vpc, InternetGateway, InternetGatewayAttachment, Subnet, RouteTable, Route, RouteTableAssociation} from "alchemy/aws/ec2";
// Create VPC with DNS enabledconst vpc = await Vpc("web-vpc", { cidrBlock: "10.0.0.0/16", enableDnsHostnames: true, enableDnsSupport: true, tags: { Name: "web-vpc", Environment: "production" }});
// Create Internet Gatewayconst igw = await InternetGateway("web-igw", { tags: { Name: "web-internet-gateway" }});
// Attach Internet Gateway to VPCconst attachment = await InternetGatewayAttachment("web-igw-attachment", { internetGateway: igw, vpc: vpc});
// Create public subnetconst publicSubnet = await Subnet("public-subnet", { vpc, cidrBlock: "10.0.1.0/24", availabilityZone: "us-east-1a", mapPublicIpOnLaunch: true, tags: { Name: "public-subnet-1a", Type: "public" }});
// Create route table for public accessconst publicRouteTable = await RouteTable("public-rt", { vpc, tags: { Name: "public-route-table" }});
// Add route to Internet Gateway (depends on attachment)const internetRoute = await Route("internet-route", { routeTable: publicRouteTable, destinationCidrBlock: "0.0.0.0/0", target: { internetGateway: igw }});
// Associate subnet with route tableconst routeAssociation = await RouteTableAssociation("public-association", { routeTable: publicRouteTable, subnet: publicSubnet});
Multi-AZ Public Infrastructure
Section titled “Multi-AZ Public Infrastructure”Attach Internet Gateway for high-availability setup:
import { Vpc, InternetGateway, InternetGatewayAttachment, Subnet, RouteTable, Route, RouteTableAssociation} from "alchemy/aws/ec2";
const vpc = await Vpc("ha-vpc", { cidrBlock: "10.0.0.0/16", enableDnsHostnames: true, enableDnsSupport: true});
const igw = await InternetGateway("ha-igw", { tags: { Name: "high-availability-igw" }});
const attachment = await InternetGatewayAttachment("ha-igw-attachment", { internetGateway: igw, vpc: vpc});
// Public subnets in multiple availability zonesconst publicSubnet1a = await Subnet("public-subnet-1a", { vpc, cidrBlock: "10.0.1.0/24", availabilityZone: "us-east-1a", mapPublicIpOnLaunch: true});
const publicSubnet1b = await Subnet("public-subnet-1b", { vpc, cidrBlock: "10.0.2.0/24", availabilityZone: "us-east-1b", mapPublicIpOnLaunch: true});
const publicSubnet1c = await Subnet("public-subnet-1c", { vpc, cidrBlock: "10.0.3.0/24", availabilityZone: "us-east-1c", mapPublicIpOnLaunch: true});
// Single route table for all public subnetsconst publicRouteTable = await RouteTable("public-rt", { vpc: vpc});
const internetRoute = await Route("internet-route", { routeTable: publicRouteTable, destinationCidrBlock: "0.0.0.0/0", target: { internetGateway: igw }});
// Associate all public subnetsconst association1a = await RouteTableAssociation("public-association-1a", { routeTable: publicRouteTable, subnet: publicSubnet1a});
const association1b = await RouteTableAssociation("public-association-1b", { routeTable: publicRouteTable, subnet: publicSubnet1b});
const association1c = await RouteTableAssociation("public-association-1c", { routeTable: publicRouteTable, subnet: publicSubnet1c});
Custom Timeout Configuration
Section titled “Custom Timeout Configuration”Handle slower environments with custom timeout settings:
import { Vpc, InternetGateway, InternetGatewayAttachment} from "alchemy/aws/ec2";
const vpc = await Vpc("slow-vpc", { cidrBlock: "10.0.0.0/16"});
const igw = await InternetGateway("slow-igw", {});
const attachment = await InternetGatewayAttachment("slow-igw-attachment", { internetGateway: igw, vpc, timeout: { maxAttempts: 120, // Increase attempts for slower environments delayMs: 3000 // 3 second delay between checks }});
Web Application Architecture
Section titled “Web Application Architecture”Complete setup for a web application with public and private tiers:
import { Vpc, InternetGateway, InternetGatewayAttachment, Subnet, RouteTable, Route, RouteTableAssociation, NatGateway} from "alchemy/aws/ec2";
// VPC for web applicationconst webVpc = await Vpc("web-app-vpc", { cidrBlock: "10.0.0.0/16", enableDnsHostnames: true, enableDnsSupport: true, tags: { Name: "web-application-vpc", Project: "web-app" }});
// Internet Gateway for public accessconst webIgw = await InternetGateway("web-app-igw", { tags: { Name: "web-application-igw" }});
// Attach Internet Gateway to VPCconst webAttachment = await InternetGatewayAttachment("web-app-igw-attachment", { internetGateway: webIgw, vpc: webVpc});
// Public subnet for load balancers and NAT gatewaysconst publicSubnet = await Subnet("web-public-subnet", { vpc: webVpc, cidrBlock: "10.0.1.0/24", availabilityZone: "us-east-1a", mapPublicIpOnLaunch: true, tags: { Name: "web-public-subnet", Tier: "public" }});
// Private subnet for application serversconst privateSubnet = await Subnet("web-private-subnet", { vpc: webVpc, cidrBlock: "10.0.10.0/24", availabilityZone: "us-east-1a", tags: { Name: "web-private-subnet", Tier: "private" }});
// NAT Gateway for private subnet outbound accessconst natGateway = await NatGateway("web-nat", { subnet: publicSubnet, tags: { Name: "web-nat-gateway" }});
// Public route tableconst publicRouteTable = await RouteTable("web-public-rt", { vpc: webVpc, tags: { Name: "web-public-routes" }});
// Private route tableconst privateRouteTable = await RouteTable("web-private-rt", { vpc: webVpc, tags: { Name: "web-private-routes" }});
// Routesconst publicInternetRoute = await Route("web-public-internet", { routeTable: publicRouteTable, destinationCidrBlock: "0.0.0.0/0", target: { internetGateway: webIgw }});
const privateNatRoute = await Route("web-private-nat", { routeTable: privateRouteTable, destinationCidrBlock: "0.0.0.0/0", target: { natGateway: natGateway }});
// Route table associationsconst publicAssociation = await RouteTableAssociation("web-public-association", { routeTable: publicRouteTable, subnet: publicSubnet});
const privateAssociation = await RouteTableAssociation("web-private-association", { routeTable: privateRouteTable, subnet: privateSubnet});
Reference
Section titled “Reference”Access attachment properties after creation:
const attachment = await InternetGatewayAttachment("my-attachment", { internetGateway: igw, vpc: vpc});
console.log(`Internet Gateway ID: ${attachment.internetGatewayId}`);console.log(`VPC ID: ${attachment.vpcId}`);console.log(`Attachment State: ${attachment.state}`);