|
1 | | -# terraform-google-network |
2 | | -A Cloud Foundation Toolkit Module: Opinionated Google Cloud Platform project creation and configuration with Shared VPC, IAM, APIs, etc. |
| 1 | +# Terraform Network Module |
| 2 | + |
| 3 | +This modules makes it easy to set up a new VPC Network in GCP by defining your network and subnet ranges in a concise syntax. |
| 4 | + |
| 5 | +It supports creating: |
| 6 | + |
| 7 | +- A Google Virtual Private Network (VPC) |
| 8 | +- Subnets within the VPC |
| 9 | +- Secondary ranges for the subnets (if applicable) |
| 10 | + |
| 11 | +## Requirements |
| 12 | +### Terraform plugins |
| 13 | +- [Terraform](https://www.terraform.io/downloads.html) 0.10.x |
| 14 | +- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v1.8.0 |
| 15 | + |
| 16 | +### Configure a Service Account |
| 17 | +In order to execute this module you must have a Service Account with the following roles: |
| 18 | + |
| 19 | +- roles/compute.networkAdmin on the organization |
| 20 | + |
| 21 | +### Enable API's |
| 22 | +In order to operate with the Service Account you must activate the following API on the project where the Service Account was created: |
| 23 | + |
| 24 | +- Compute Engine API - compute.googleapis.com |
| 25 | + |
| 26 | +## Install |
| 27 | + |
| 28 | +### Terraform |
| 29 | +Be sure you have the correct Terraform version (0.10.x), you can choose the binary here: |
| 30 | +- https://releases.hashicorp.com/terraform/ |
| 31 | + |
| 32 | +## Usage |
| 33 | +You can go to the examples folder, however the usage of the module could be like this in your own main.tf file: |
| 34 | + |
| 35 | +```hcl |
| 36 | +module "vpc" { |
| 37 | + source = "github.com/terraform-google-modules/terraform-google-network" |
| 38 | + project_id = "<PROJECT ID>" |
| 39 | + vpc_name = "example-vpc" |
| 40 | +
|
| 41 | + subnets = [ |
| 42 | + { |
| 43 | + subnet_name = "subnet-01" |
| 44 | + subnet_ip = "10.10.10.0/24" |
| 45 | + subnet_region = "us-west1" |
| 46 | + subnet_private_access = false |
| 47 | + }, |
| 48 | + { |
| 49 | + subnet_name = "subnet-02" |
| 50 | + subnet_ip = "10.10.20.0/24" |
| 51 | + subnet_region = "us-west1" |
| 52 | + subnet_private_access = false |
| 53 | + }, |
| 54 | + ] |
| 55 | +
|
| 56 | + secondary_ranges = { |
| 57 | + subnet-01 = [ |
| 58 | + { |
| 59 | + range_name = "subnet-01-secondary-01" |
| 60 | + ip_cidr_range = "192.168.64.0/24" |
| 61 | + }, |
| 62 | + ] |
| 63 | +
|
| 64 | + subnet-02 = [] |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Then perform the following commands on the root folder: |
| 70 | + |
| 71 | +- `terraform init` to get the plugins |
| 72 | +- `terraform plan` to see the infrastructure plan |
| 73 | +- `terraform apply` to apply the infrastructure build |
| 74 | +- `terraform destroy` to destroy the built infrastructure |
| 75 | + |
| 76 | +#### Variables |
| 77 | +Please refer the /variables.tf file for the required and optional variables. |
| 78 | + |
| 79 | +#### Outputs |
| 80 | +Please refer the /outputs.tf file for the outputs that you can get with the `terraform output` command |
| 81 | + |
| 82 | +## File structure |
| 83 | +The project has the following folders and files: |
| 84 | + |
| 85 | +- /: root folder |
| 86 | +- /examples: examples for using this module |
| 87 | +- /test: Folders with files for testing the module (see Testing section on this file) |
| 88 | +- /main.tf: main file for this module, contains all the resources to create |
| 89 | +- /variables.tf: all the variables for the module |
| 90 | +- /output.tf: the outputs of the module |
| 91 | +- /README.md: this file |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +### Requirements |
| 96 | +- [bats](https://github.com/sstephenson/bats) 0.4.0 |
| 97 | +- [jq](https://stedolan.github.io/jq/) 1.5 |
| 98 | + |
| 99 | +### Integration test |
| 100 | +##### Terraform integration tests |
| 101 | +The integration tests for this module are built with bats, basically the test checks the following: |
| 102 | +- Perform `terraform init` command |
| 103 | +- Perform `terraform get` command |
| 104 | +- Perform `terraform plan` command and check that it'll create *n* resources, modify 0 resources and delete 0 resources |
| 105 | +- Perform `terraform apply -auto-approve` command and check that it has created the *n* resources, modified 0 resources and deleted 0 resources |
| 106 | +- Perform several `gcloud` commands and check the infrastructure is in the desired state |
| 107 | +- Perform `terraform destroy -force` command and check that it has destroyed the *n* resources |
| 108 | + |
| 109 | +You can use the following command to run the integration test in the folder */test/integration/gcloud-test* |
| 110 | + |
| 111 | + `. launch.sh` |
| 112 | + |
| 113 | +### Linting |
| 114 | +The makefile in this project will lint or sometimes just format any shell, |
| 115 | +Python, golang, Terraform, or Dockerfiles. The linters will only be run if |
| 116 | +the makefile finds files with the appropriate file extension. |
| 117 | + |
| 118 | +All of the linter checks are in the default make target, so you just have to |
| 119 | +run |
| 120 | + |
| 121 | +``` |
| 122 | +make -s |
| 123 | +``` |
| 124 | + |
| 125 | +The -s is for 'silent'. Successful output looks like this |
| 126 | + |
| 127 | +``` |
| 128 | +Running shellcheck |
| 129 | +Running flake8 |
| 130 | +Running gofmt |
| 131 | +Running terraform validate |
| 132 | +Running hadolint on Dockerfiles |
| 133 | +Test passed - Verified all file Apache 2 headers |
| 134 | +``` |
| 135 | + |
| 136 | +The linters |
| 137 | +are as follows: |
| 138 | +* Shell - shellcheck. Can be found in homebrew |
| 139 | +* Python - flake8. Can be installed with 'pip install flake8' |
| 140 | +* Golang - gofmt. gofmt comes with the standard golang installation. golang |
| 141 | +is a compiled language so there is no standard linter. |
| 142 | +* Terraform - terraform has a built-in linter in the 'terraform validate' |
| 143 | +command. |
| 144 | +* Dockerfiles - hadolint. Can be found in homebrew |
0 commit comments