cd ~/environment/terraform/modules/tf-vpc
## Initialize Terraform
terraform init
## Format code, validate syntax & check security issues
terraform fmt
terraform validate
## Terraform "plan" what it is going to do
terraform plan -out tfplan
## Build the VPC
terraform apply tfplan
## Destroy what we have created !!!
# terraform destroy -auto-approve
subnets.tf
contain the definition for two AWS resources (two subnets) - Is this a good idea, having lots of Terraform resources in one file ?
Note how vpc_id
is defined, it refers to an existing resources attribute (its id
) be referencing it’s full Terrform name aws_vpc.tf-vpc.id
.
The name aws_vpc.tf-vpc.id
came from your own definition of the VPC - look at the first line in the file tf-vpc.tf
โ Terraform has supporting IP networking functions to help you calculate and specify CIDR ranges
resource "aws_eip" "tf-eip" {
public_ipv4_pool = "amazon"
vpc = true
tags = {
"Name" = "CICD-EIP"
}
timeouts {}
}
resource "aws_subnet" "public-subnet" {
assign_ipv6_address_on_creation = false
availability_zone = "ap-southeast-1a"
cidr_block = "172.30.0.0/27"
map_public_ip_on_launch = false
tags = {
"Name" = "CICD-Public-Subnet"
}
vpc_id = aws_vpc.tf-vpc.id
timeouts {}
}
resource "aws_subnet" "private-subnet" {
assign_ipv6_address_on_creation = false
availability_zone = "ap-southeast-1a"
cidr_block = "172.30.0.128/27"
map_public_ip_on_launch = false
tags = {
"Name" = "CICD-Private-Subnet"
}
vpc_id = aws_vpc.tf-vpc.id
timeouts {}
}
terraform state list | grep vpc
terraform state show aws_vpc.tf-vpc
Now use the console to check all the resources exist:
CI/CD-VPC
CICD-EIP
CICD-Public-Subnet
& CICD-Private-Subnet