Skip to content

vpc

AWS VPC with public and private subnets spread across the availability zones you pass, one of each per AZ. Optional, all off by default and priced accordingly: NAT gateways for private-subnet egress (enable_nat_gateway), VPC flow logs to a CloudWatch log group with its own IAM role (enable_flow_logs), and S3 / DynamoDB gateway endpoints that keep that traffic off the NAT at no cost (enable_dynamodb_gateway_endpoint). Subnet CIDRs are validated against azs at plan time.

Usage

module "vpc" {
  source = "github.com/rafaelcarvajal05/terraform-stacks//modules/vpc?ref=v0.1.0"

  name                 = "my-app"
  cidr_block           = "10.0.0.0/16"
  azs                  = ["us-east-1a", "us-east-1b"]
  public_subnet_cidrs  = ["10.0.0.0/24", "10.0.1.0/24"]
  private_subnet_cidrs = ["10.0.10.0/24", "10.0.11.0/24"]

  enable_nat_gateway               = true
  enable_flow_logs                 = true
  enable_dynamodb_gateway_endpoint = true

  tags = {
    Environment = "prod"
  }
}

See examples/vpc-basic for a runnable, zero-cost configuration.

Requirements

Name Version
terraform >= 1.6.0
aws >= 5.0

Providers

Name Version
aws >= 5.0

Modules

No modules.

Resources

Name Type
aws_cloudwatch_log_group.flow_logs resource
aws_default_security_group.this resource
aws_eip.nat resource
aws_flow_log.this resource
aws_iam_role.flow_logs resource
aws_iam_role_policy.flow_logs resource
aws_internet_gateway.this resource
aws_nat_gateway.this resource
aws_route.private_nat_gateway resource
aws_route.public_internet_gateway resource
aws_route_table.private resource
aws_route_table.public resource
aws_route_table_association.private resource
aws_route_table_association.public resource
aws_subnet.private resource
aws_subnet.public resource
aws_vpc.this resource
aws_vpc_endpoint.dynamodb resource
aws_vpc_endpoint.s3 resource
aws_iam_policy_document.flow_logs_assume_role data source
aws_iam_policy_document.flow_logs_publish data source
aws_vpc_endpoint_service.dynamodb data source
aws_vpc_endpoint_service.s3 data source

Inputs

Name Description Type Default Required
azs Availability zones to spread the subnets across. One public and one private subnet is created per AZ. list(string) n/a yes
cidr_block IPv4 CIDR block for the VPC, e.g. 10.0.0.0/16. string n/a yes
name Name prefix used for every resource created by the module (also applied as the Name tag). string n/a yes
private_subnet_cidrs CIDR blocks for the private subnets, one per entry in azs (same order). The count is checked against azs at plan time. list(string) n/a yes
public_subnet_cidrs CIDR blocks for the public subnets, one per entry in azs (same order). The count is checked against azs at plan time. list(string) n/a yes
enable_dynamodb_gateway_endpoint Create a DynamoDB gateway VPC endpoint associated with every route table (free, keeps DynamoDB traffic off the NAT). bool false no
enable_flow_logs Create a CloudWatch Logs log group, an IAM role and a VPC flow log that publishes the VPC traffic to it. bool false no
enable_nat_gateway Create NAT gateway(s) so private subnets get outbound internet access. bool false no
enable_s3_gateway_endpoint Create an S3 gateway VPC endpoint associated with every route table (free, keeps S3 traffic off the NAT). bool true no
flow_logs_kms_key_arn ARN of a KMS key used to encrypt the flow logs log group. When null the log group uses the default CloudWatch Logs encryption. The key policy must allow the CloudWatch Logs service. string null no
flow_logs_retention_in_days Retention in days of the flow logs CloudWatch log group. Only used when enable_flow_logs is true. Must be a value accepted by CloudWatch Logs. number 365 no
flow_logs_traffic_type Type of traffic captured by the flow log: ACCEPT, REJECT or ALL. Only used when enable_flow_logs is true. string "ALL" no
single_nat_gateway When enable_nat_gateway is true, create a single shared NAT gateway (cheaper) instead of one per AZ (highly available). bool true no
tags Tags applied to every resource. A Name tag is added per resource. map(string) {} no

Outputs

Name Description
dynamodb_endpoint_id ID of the DynamoDB gateway VPC endpoint (null when enable_dynamodb_gateway_endpoint is false).
flow_log_id ID of the VPC flow log (null when enable_flow_logs is false).
flow_logs_log_group_arn ARN of the CloudWatch log group receiving the flow logs (null when enable_flow_logs is false).
flow_logs_log_group_name Name of the CloudWatch log group receiving the flow logs (null when enable_flow_logs is false).
nat_gateway_ids IDs of the NAT gateways (empty when enable_nat_gateway is false).
private_route_table_ids IDs of the private route tables, one per AZ in the same order as azs.
private_subnet_ids IDs of the private subnets, in the same order as azs.
public_route_table_id ID of the route table shared by the public subnets.
public_subnet_ids IDs of the public subnets, in the same order as azs.
s3_endpoint_id ID of the S3 gateway VPC endpoint (null when enable_s3_gateway_endpoint is false).
vpc_cidr_block IPv4 CIDR block of the VPC.
vpc_id ID of the VPC.