Skip to content
GitHubXDiscord

SubnetRouteTableAssociation

The SubnetRouteTableAssociation resource allows you to associate a subnet with a specific route table in AWS EC2, enabling routing for the subnet’s network traffic. For more details, refer to the AWS EC2 SubnetRouteTableAssociations documentation.

Create a simple subnet route table association with the required properties.

import AWS from "alchemy/aws/control";
const subnetRouteTableAssociation = await AWS.EC2.SubnetRouteTableAssociation("mySubnetAssociation", {
RouteTableId: "rtb-0123456789abcdef0",
SubnetId: "subnet-0123456789abcdef0",
adopt: true // Adopt existing resource if it already exists
});

Configure a subnet route table association with additional properties if necessary.

const advancedSubnetRouteTableAssociation = await AWS.EC2.SubnetRouteTableAssociation("advancedSubnetAssociation", {
RouteTableId: "rtb-abcdef0123456789",
SubnetId: "subnet-abcdef0123456789",
adopt: false // Do not adopt existing resource
});

Create multiple subnet route table associations to manage various subnets within the same route table.

const firstSubnetAssociation = await AWS.EC2.SubnetRouteTableAssociation("firstSubnetAssociation", {
RouteTableId: "rtb-abcdef0123456789",
SubnetId: "subnet-abcdef0123456781"
});
const secondSubnetAssociation = await AWS.EC2.SubnetRouteTableAssociation("secondSubnetAssociation", {
RouteTableId: "rtb-abcdef0123456789",
SubnetId: "subnet-abcdef0123456782"
});

Create a subnet route table association with error handling for existing resources.

try {
const subnetRouteTableAssociation = await AWS.EC2.SubnetRouteTableAssociation("errorHandledAssociation", {
RouteTableId: "rtb-abcdef0123456789",
SubnetId: "subnet-abcdef0123456783",
adopt: true // Attempt to adopt existing resource
});
} catch (error) {
console.error("Error occurred while creating SubnetRouteTableAssociation:", error);
}