How to create a minimal VPC in AWS with Terraform.

Search for a command to run...

No comments yet. Be the first to comment.
Everything is simple when we write a playbook that does one thing. We have a powerful tool for expressing dependencies: the task order. But sometimes we want more than one target. We want to deploy something, then cleanup something. Or even build som...

I know… There is lots of articles on this rather basic topic. I’m not consider myself an expert. Maybe someone find helpful a HOWTO on this particular combination of technologies. TL;DR I’m going to create a simple workflow for deploying a Terraform ...

It may happen that you starting working on a Terraform project in a team and local state file doesn’t work for you anymore. Time to a remote backed. It’s not going to be complete documentation. Or better than others. But maybe I’ll manage to hit your...

So. You have you playbook in git and you want to keep some variables invisible to others. You probably should go for a ‘real’ password manager like Hashicorp Vault. Or pass (https://www.passwordstore.org/). Both have ready to use lookup plugins in An...

Don’t want use the default one anymore? Deleted the default one? Good. Let’s do it. Actually it is minimal excepting one detail, my fetish - IPv6 support.
Of course, to create a VDP - we need VDP:
resource "aws_vpc" "min_vpc" {
cidr_block = "10.0.0.0/16"
assign_generated_ipv6_cidr_block = true
enable_dns_support = true
enable_dns_hostnames = true
}
We need to specify:
IP range. (Do you have any idea why people hate 172.16/12?)
IPv6 range. It’s assigned automatically.
DNS support. If the instances uses Amazon DNS. Actually true is default.
DNS hostnames. If true - instances get funny hostnames in amazonaws.com.
Every VPC needs at least one subnet.
resource "aws_subnet" "pub" {
vpc_id = aws_vpc.min_vpc.id
cidr_block = cidrsubnet(aws_vpc.min_vpc.cidr_block, 8, 0)
ipv6_cidr_block = cidrsubnet(aws_vpc.min_vpc.ipv6_cidr_block, 8, 0)
assign_ipv6_address_on_creation = true
}
vpc_id - the parent VPC.
cidr_block - the address range of the segment. Similar for IPv6. We don’t specify it manually - instead we’re using cidrsubnet function. Which cuts a network segments to a smaller one.
Like every gateway - traffic goes through it.
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.min_vpc.id
}
Nothing fancy. Just VPC id.
Just routes everything to the gateway.
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.min_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
route {
ipv6_cidr_block = "::0/0"
gateway_id = aws_internet_gateway.gw.id
}
}
Connects a route to a subnet.
resource "aws_route_table_association" "pub_assoc" {
subnet_id = aws_subnet.pub.id
route_table_id = aws_route_table.rt.id
}
That’s more or less it. You can remove IPv6 related lines. You’ll probably gonna need a security group. I’ll describe it along with an instance example - soon.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet
https://developer.hashicorp.com/terraform/language/functions/cidrsubnet
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_gateway.html
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table