Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(2)Terraform for AWS-(6)Networking Part 1 : VPC, IGW, and Route Tables

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
2. Terraform for AWS
6. Networking Part 1 : VPC, IGW, and Route Tables

close tabs for editing files under AWS/storage

Edit the AWS/networking/main.tf file.

#---networking/main.tf

data "aws_availability_zones" "available" {}

resource "aws_vpc" "tf_vpc" {
    cidr_block = var.vpc_cidr
    enable_dns_hostnames = true
    enable_dns_support = true
    
    tags = {
        Name = "tf_vpc"
    }
}

resource "aws_internet_gateway" "tf_internet_gateway" {
    vpc_id = aws_vpc.tf_vpc.id
    
    tags = {
        Name = "tf_igw"
    }
}

resource "aws_route_table" "tf_public_rt" {
    vpc_id = aws_vpc.tf_vpc.id
    
    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.tf_internet_gateway.id
    }
    
    tags = {
        Name = "tf_public"
    }
}

resource "aws_default_route_table" "tf_private_rt" {
    default_route_table_id = aws_vpc.tf_vpc.default_route_table_id
    
    tags = {
        Name = "tf_private"
    }
}
NB
In terraform version 0.11 the default route table ID is specified as "{aws_vpc.tf_vpc.default_route_table.id}". In Terraform version 0.13, it should be specified as aws_vpc.tf_vpc.default_route_table_id.

Edit the AWS/networking/variables.tf file.
#---networking/variables.tf

variable "vpc_cidr" {
    default = "10.123.0.0/16"
}

ec2-user:~/environment/AWS $ cd networking

ec2-user:~/environment/AWS/networking $ terraform init
Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 1.39"

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.

ec2-user:~/environment/AWS/networking $ terraform plan
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: us-west-2

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.aws_availability_zones.available: Refreshing state...

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_default_route_table.tf_private_rt will be created
  + resource "aws_default_route_table" "tf_private_rt" {
      + default_route_table_id = "{aws_vpc.tf_vpc.default_route_table.id}"
      + id                     = (known after apply)
      + owner_id               = (known after apply)
      + route                  = (known after apply)
      + tags                   = {
          + "Name" = "tf_private"
        }
      + vpc_id                 = (known after apply)
    }

  # aws_internet_gateway.tf_internet_gateway will be created
  + resource "aws_internet_gateway" "tf_internet_gateway" {
      + arn      = (known after apply)
      + id       = (known after apply)
      + owner_id = (known after apply)
      + tags     = {
          + "Name" = "tf_igw"
        }
      + vpc_id   = (known after apply)
    }

  # aws_route_table.tf_public_rt will be created
  + resource "aws_route_table" "tf_public_rt" {
      + id               = (known after apply)
      + owner_id         = (known after apply)
      + propagating_vgws = (known after apply)
      + route            = [
          + {
              + cidr_block                = "0.0.0.0/0"
              + egress_only_gateway_id    = ""
              + gateway_id                = (known after apply)
              + instance_id               = ""
              + ipv6_cidr_block           = ""
              + local_gateway_id          = ""
              + nat_gateway_id            = ""
              + network_interface_id      = ""
              + transit_gateway_id        = ""
              + vpc_peering_connection_id = ""
            },
        ]
      + tags             = {
          + "Name" = "tf_public"
        }
      + vpc_id           = (known after apply)
    }

  # aws_vpc.tf_vpc will be created
  + resource "aws_vpc" "tf_vpc" {
      + arn                              = (known after apply)
      + assign_generated_ipv6_cidr_block = false
      + cidr_block                       = "10.123.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)
      + main_route_table_id              = (known after apply)
      + owner_id                         = (known after apply)
      + tags                             = {
          + "Name" = "tf_vpc"
        }
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

ec2-user:~/environment/AWS/networking $ terraform apply
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Enter a value: us-west-2

data.aws_availability_zones.available: Refreshing state... [id=2020-10-09 07:42:17.248322116 +0000 UTC]
aws_vpc.tf_vpc: Refreshing state... [id=vpc-0c8aabd8c4d127866]
aws_internet_gateway.tf_internet_gateway: Refreshing state... [id=igw-01845da5ec1f7a223]
aws_default_route_table.tf_private_rt: Refreshing state... [id={aws_vpc.tf_vpc.default_route_table.id}]
aws_route_table.tf_public_rt: Refreshing state... [id=rtb-031128320e5c6035c]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_default_route_table.tf_private_rt will be created
  + resource "aws_default_route_table" "tf_private_rt" {
      + default_route_table_id = "rtb-0d18cf1858d8834f8"
      + id                     = (known after apply)
      + owner_id               = (known after apply)
      + route                  = (known after apply)
      + tags                   = {
          + "Name" = "tf_private"
        }
      + vpc_id                 = (known after apply)
    }

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_default_route_table.tf_private_rt: Creating...
aws_default_route_table.tf_private_rt: Creation complete after 0s [id=rtb-0d18cf1858d8834f8]

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



Category: orchestration Tags: public

Upvote


Downvote