DEV Community

Cover image for 35.VPC Variable Setup Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

35.VPC Variable Setup Using Terraform

Lab Information

The Nautilus DevOps team is automating VPC creation using Terraform to manage networking efficiently. As part of this task, they need to create a VPC with specific requirements.

For this task, create an AWS VPC using Terraform with the following requirements:

The VPC name xfusion-vpc should be stored in a variable named KKE_vpc.

The VPC should have a CIDR block of 10.0.0.0/16.
Enter fullscreen mode Exit fullscreen mode

Note:

The configuration values should be stored in a variables.tf file.

The Terraform script should be structured with a main.tf file referencing variables.tf.

The Terraform working directory is /home/bob/terraform.

Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

Step 1: Check the VPC details

aws ec2 describe-vpcs --region us-east-1 --filters "Name=tag:Name,Values=xfusion-vpc" --query "Vpcs[].{VpcId:VpcId,CidrBlock:CidrBlock}"
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  aws ec2 describe-vpcs --region us-east-1 --filters "Name=tag:Name,Values=xfusion-vpc" --query "Vpcs[].{VpcId:VpcId,CidrBlock:CidrBlock}"
[]
Enter fullscreen mode Exit fullscreen mode

Step 2: Create variables.tf

# variables.tf

variable "KKE_vpc" {
  description = "The name of the VPC"
  type        = string
  default     = "xfusion-vpc"
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create main.tf

# main.tf

# Create VPC
resource "aws_vpc" "this" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = var.KKE_vpc
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: To deploy this configuration

Navigate to the Terraform directory:

cd /home/bob/terraform
Enter fullscreen mode Exit fullscreen mode

Initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Enter fullscreen mode Exit fullscreen mode

Plan the configuration:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform plan

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.this will be created
  + resource "aws_vpc" "this" {
      + arn                                  = (known after apply)
      + cidr_block                           = "10.0.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_dns_hostnames                 = (known after apply)
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + 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" = "xfusion-vpc"
        }
      + tags_all                             = {
          + "Name" = "xfusion-vpc"
        }
    }

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

─────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to
take exactly these actions if you run "terraform apply" now.
Enter fullscreen mode Exit fullscreen mode

Apply the configuration:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Then type yes when prompted to confirm the creation of the snapshot.

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform apply

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.this will be created
  + resource "aws_vpc" "this" {
      + arn                                  = (known after apply)
      + cidr_block                           = "10.0.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_dns_hostnames                 = (known after apply)
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + 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" = "xfusion-vpc"
        }
      + tags_all                             = {
          + "Name" = "xfusion-vpc"
        }
    }

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

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_vpc.this: Creating...
aws_vpc.this: Creation complete after 0s [id=vpc-076ffaba14dccbd8b]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)