Terraform Curriculum
On this page
- Vpc
- 1. Architectural Anatomy of a VPC
- 1.1 CIDR Block Planning (IPv4)
- 1.2 IPv6 Integration
- 2. Advanced DNS Configuration
- 2.1 enablednssupport
- 2.2 enablednshostnames
- 3. Tenancy Models and Isolation
- 4. The Default Network Guardrails
- 4.1 The Default Security Group (awsdefaultsecuritygroup)
- 4.2 The Default Network ACL (awsdefaultnetworkacl)
- 5. DHCP Options Sets and Custom DNS
- 6. VPC Flow Logs: The Auditor's Best Friend
- 7. Operational Lifecycle Management
- 8. Addressing and Outputs in Modular Architecture
- 9. Summary and Strategic Vision
Vpc
The Virtual Private Cloud (VPC) is the fundamental logical boundary within the AWS ecosystem. It provides a private, isolated network where you can launch AWS resources. Designing a VPC correctly is the most critical step in building a secure and scalable cloud infrastructure. In a professional DevOps environment, a VPC is not just a container; it is a meticulously planned network with specific IP addressing, DNS behaviors, and tenancy models. This chapter provides an exhaustive exploration of the aws_vpc resource, covering advanced configurations, architectural best practices, and the underlying mechanics of VPC networking.
1. Architectural Anatomy of a VPC
A VPC is defined by its primary CIDR block. This block determines the total quantity of internal IP addresses available to your resources.
1.1 CIDR Block Planning (IPv4)
Selecting the right CIDR block is a permanent decision for a VPC.
- Range Selection: Common ranges follow RFC 1918 standards:
10.0.0.0/8,172.16.0.0/12, and192.168.0.0/16. - Sizing: A
/16mask provides 65,536 addresses. Even if you only plan to launch 100 servers, large ranges are preferred to allow for high-scale services like Lambda (which consumes an IP per execution in a VPC) or Amazon EKS (which consumes an IP per Pod in certain CNI configurations). - Overlap Prevention: In any enterprise, you must coordinate with networking teams to ensure your VPC CIDR does not overlap with other VPCs (which would prevent VPC Peering) or the corporate on-premises data center (which would prevent VPN/Direct Connect integration).
1.2 IPv6 Integration
By setting assign_generated_ipv6_cidr_block = true, AWS provides a /56 IPv6 CIDR block. IPv6 is critical for overcoming IPv4 exhaustion and is increasingly required for modern global applications. Terraform allows you to manage this block across all child subnets using the cidrsubnet function.
2. Advanced DNS Configuration
Terraform's aws_vpc resource exposes two critical DNS toggles that control the resolution behavior within your network.
2.1 enable_dns_support
This setting enables the Amazon Route 53 Resolver (also known as the "VPC+2" address).
- Technical Detail: If your VPC range is
10.0.0.0/16, the DNS server lives at10.0.0.2. - Purpose: Without this, your instances cannot resolve AWS service endpoints, public domain names, or internal Route 53 private zones.
2.2 enable_dns_hostnames
This setting determines if instances with public IP addresses receive a public DNS hostname.
- Dependency: This requires
enable_dns_supportto betrue. - DevOps Context: This is essential for developers who need to SSH into bastion hosts or access web dashboards using a human-readable name rather than a potentially changing public IP.
3. Tenancy Models and Isolation
The instance_tenancy attribute defines the physical isolation of your workloads on the AWS hardware.
default(Shared Tenancy): Your instances shared physical hardware with other AWS accounts. This is the standard for 99% of workloads.dedicated(Dedicated Tenancy): Ensures that all instances launched in the VPC run on hardware dedicated to your account.host(Dedicated Host): For extreme compliance or "Bring Your Own License" (BYOL) scenarios where you need control over specific physical sockets or cores.
4. The Default Network Guardrails
When a VPC is created through the AWS API (and thus through Terraform), AWS automatically provisions several default components. Professional Terraform engineering involves taking explicit control over these defaults.
4.1 The Default Security Group (aws_default_security_group)
Every VPC has a "Default" group. By default, it allows all traffic from resources within the same group (Ingress) and all outbound traffic (Egress).
- Security Best Practice: Use the
aws_default_security_groupresource to "empty" the rules, effectively disabling the group, and then create explicit Security Groups for your specific tiers.
4.2 The Default Network ACL (aws_default_network_acl)
Similar to the security group, the default NACL allows all traffic. In high-security environments, you should use Terraform to enforce a "Deny All" default NACL and use custom NACLs for your subnets.
5. DHCP Options Sets and Custom DNS
In enterprise hybrid-cloud scenarios, you often need VPC resources to use on-premises DNS servers (like Microsoft Active Directory) for resolving internal domains.
6. VPC Flow Logs: The Auditor's Best Friend
A professional VPC configuration is incomplete without monitoring. VPC Flow Logs capture information about the IP traffic simplified into/out of network interfaces in your VPC.
Setting this up via Terraform ensures that from the moment the network is live, every packet's metadata is being logged for security auditing and network troubleshooting.
7. Operational Lifecycle Management
The aws_vpc resource participates in the Terraform dependency graph in a very rigid way.
- Creation Phase: It is the "Root" of the graph. Nothing else can be created until the VPC API call returns successfully.
- Deletion Phase: AWS will refuse to delete a VPC if any resources (ELBs, Instances, Subnets, Endpoints) still exist. Terraform's graph engine handles this by deleting resources in the exact reverse order of creation.
- Importing Existing VPCs: If you have a VPC created manually, use
terraform import aws_vpc.main vpc-xxxxxxto bring it under management. Terraform will verify that your HCL configuration matches the existing cloud configuration.
8. Addressing and Outputs in Modular Architecture
In a tiered architecture, the VPC ID is the primary "Glue" variable passed to downstream modules.
9. Summary and Strategic Vision
The VPC is the foundation of your digital real estate in the cloud. By moving beyond default settings and explicitly defining CIDR ranges, DNS resolution paths, tenancy models, and monitoring logs, you create an infrastructure that is secure, observable, and ready for enterprise-scale workloads. Terraform provides the declarative precision needed to ensure that this foundation is identical across every region and every account in your organization, eliminating the risks associated with manual network configuration.