Terraform tutorial part 3

Infrastructure is not only virtual machines or load balancers but also the virtual private cloud that allows everything to communicate with each other. What is also quite convenient is that it can be defined in Terraform and AWS with only a few lines.

main.tf
resource “aws_vpc” “cgd-default-vpc” {
cidr_block = var.vpc_cidr
enable_dns_hostnames = “true”
tags = { Name = "${var.environment}-webproject" }
}
Configuration for a VPC

I have created a variable vpc_cidr which will hold the CIDR for this VPC. It is defaulted 172.16.0.0/16 which is a tiny network but it can be set to a different value if a larger network is required. This default value is being overridden with 10.10.0.0/16 which is a huge network.

The VPC is defined using two different variables, vpc_cidr and environment. It is rather nifty that you can create more complex values with constants and variables like is being used in the tags definition.

One more important note for the tags is the tag Name is special and is display in all lists as a user friendly name for the AWS element. The output from the terraform apply, just like the plan command will show what changes need to be made but then will make the changes.

terraform apply -var-file=development.tfvars –auto-approve
Acquiring state lock. This may take a few moments…
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# aws_vpc.cgd-default-vpc will be created
+ resource “aws_vpc” “cgd-default-vpc” { arn = (known after apply)
+ cidr_block = “10.10.0.0/16”
+ default_network_acl_id = (known after apply)
+ default_route_table_id = (known after apply)
+ default_security_group_id = (known after apply)
+ dhcp_options_id = (known after apply)
+ enable_classiclink = (known after apply)
+ enable_classiclink_dns_support = (known after apply)
+ enable_dns_hostnames = true
+ enable_dns_support = true
+ id = (known after apply)
+ instance_tenancy = “default”
+ ipv6_association_id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_block_network_border_group = (known after apply)
+ main_route_table_id = (known after apply)
+ owner_id = (known after apply)
+ tags = {
+ “Name” = “development-webproject”
}
+ tags_all = {
+ “Name” = “development-webproject”
}
}

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
+ _environment = “development”
+ awskey = “don’t display this”
+ awssecret = “don’t display this”
+ region = “us-east-1”
+ vpc_cidr = “10.10.0.0/16”
+ vpc_id = (known after apply)
+ vpc_tags = {
+ “Name” = “development-webproject”
}

aws_vpc.cgd-default-vpc: Creating…
aws_vpc.cgd-default-vpc: Still creating… [10s elapsed]
aws_vpc.cgd-default-vpc: Creation complete after 13s [id=vpc-091d490dc7a1d6407]
Releasing state lock. This may take a few moments…

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:
_environment = “development”
region = “us-east-1”
vpc_cidr = “10.10.0.0/16”
vpc_id = “vpc-091d490dc7a1d6407”
vpc_tags = tomap({
“Name” = “development-webproject”
})

For this tutorial we are expecting one item to be created, the first time it is run, and we can see the 1 resource will be added. The last few lines under Outputs shows the values that were setup in the output.tf file.

development.tfvars
region = “us-east-1”
environment = “development”
vpc_cidr = “10.10.0.0/16”
Variables with new vpc_cidr variable

inputs.tf
variable “vpc_cidr” {
type = string
description = “virtual private network”
default = “172.16.0.0/16”
}
Variable to hold the VPC CIDR

output.tf
output “vpc_id” {
value = aws_vpc.cgd-default-vpc.id
}
output “vpc_tags” {
value = aws_vpc.cgd-default-vpc.tags
}
output “vpc_cidr” {
value = aws_vpc.cgd-default-vpc.cidr_block
}
Additions to output to display info about VPC

Creating a VPC really does not show off too much of what you can do with Terraform. However, it is the overarching network that will be filled in and where we can launch our own EC2’s.

This will be demonstrated in the next blog.

This entry was posted in programming. Bookmark the permalink.