Subnet
The Subnet resource lets you manage AWS EC2 Subnets within a Virtual Private Cloud (VPC). Subnets are a range of IP addresses in your VPC that help segment your network.
Minimal Example
Section titled “Minimal Example”Create a basic subnet within a specified VPC with a CIDR block.
import AWS from "alchemy/aws/control";
const basicSubnet = await AWS.EC2.Subnet("basicSubnet", { VpcId: "vpc-0abcd1234efgh5678", CidrBlock: "10.0.1.0/24", MapPublicIpOnLaunch: true});
Advanced Configuration
Section titled “Advanced Configuration”Configure a subnet with additional options such as IPv6 support and DNS settings.
const advancedSubnet = await AWS.EC2.Subnet("advancedSubnet", { VpcId: "vpc-0abcd1234efgh5678", CidrBlock: "10.0.2.0/24", EnableDns64: true, AssignIpv6AddressOnCreation: true, Ipv6CidrBlock: "2001:db8:abcd:0012::/64"});
Subnet with Tags
Section titled “Subnet with Tags”Create a subnet and apply tags for better resource management.
const taggedSubnet = await AWS.EC2.Subnet("taggedSubnet", { VpcId: "vpc-0abcd1234efgh5678", CidrBlock: "10.0.3.0/24", Tags: [ { Key: "Name", Value: "MyTaggedSubnet" }, { Key: "Environment", Value: "Production" } ]});
Subnet in a Specific Availability Zone
Section titled “Subnet in a Specific Availability Zone”Create a subnet in a specific availability zone for redundancy.
const azSubnet = await AWS.EC2.Subnet("azSubnet", { VpcId: "vpc-0abcd1234efgh5678", CidrBlock: "10.0.4.0/24", AvailabilityZone: "us-west-2a", MapPublicIpOnLaunch: false});