This module handles opinionated Google Cloud Platform Cloud NAT creation and configuration.
There are multiple examples included in the examples folder but simple usage is as follows:
module "cloud-nat" {
source = "terraform-google-modules/cloud-nat/google"
project_id = "${var.project_id}"
region = "${var.region}"
router = "${google_compute_router.router.name}"
}
Then perform the following commands on the root folder:
terraform init
to get the pluginsterraform plan
to see the infrastructure planterraform apply
to apply the infrastructure buildterraform destroy
to destroy the built infrastructure
Name | Description | Type | Default | Required |
---|---|---|---|---|
icmp_idle_timeout_sec | (Optional) Timeout (in seconds) for ICMP connections. Defaults to 30s if not set. Changing this forces a new NAT to be created. | string | 30 |
no |
min_ports_per_vm | (Optional) Minimum number of ports allocated to a VM from this NAT config. Defaults to 64 if not set. Changing this forces a new NAT to be created. | string | 64 |
no |
name | (Optional) Defaults to 'cloud-nat-RANDOM_SUFFIX'. Changing this forces a new NAT to be created. | string | `` | no |
nat_ip_allocate_option | (Optional) Value inferred based on nat_ips. If present set to MANUAL_ONLY, otherwise AUTO_ONLY. | string | false |
no |
nat_ips | (Optional) List of self_links of external IPs. Changing this forces a new NAT to be created. | list | <list> |
no |
project_id | The project ID to deploy to | string | - | yes |
region | The region to deploy to | string | - | yes |
router | (Required) The name of the router in which this NAT will be configured. Changing this forces a new NAT to be created. | string | - | yes |
source_subnetwork_ip_ranges_to_nat | (Optional) Defaults to ALL_SUBNETWORKS_ALL_IP_RANGES. How NAT should be configured per Subnetwork. Valid values include: ALL_SUBNETWORKS_ALL_IP_RANGES, ALL_SUBNETWORKS_ALL_PRIMARY_IP_RANGES, LIST_OF_SUBNETWORKS. Changing this forces a new NAT to be created. | string | ALL_SUBNETWORKS_ALL_IP_RANGES |
no |
tcp_established_idle_timeout_sec | (Optional) Timeout (in seconds) for TCP established connections. Defaults to 1200s if not set. Changing this forces a new NAT to be created. | string | 1200 |
no |
tcp_transitory_idle_timeout_sec | (Optional) Timeout (in seconds) for TCP transitory connections. Defaults to 30s if not set. Changing this forces a new NAT to be created. | string | 30 |
no |
udp_idle_timeout_sec | (Optional) Timeout (in seconds) for UDP connections. Defaults to 30s if not set. Changing this forces a new NAT to be created. | string | 30 |
no |
Name | Description |
---|---|
name | Name of the Cloud NAT |
nat_ip_allocate_option | NAT IP allocation mode |
region | Cloud NAT region |
router_name | Cloud NAT router name |
Before this module can be used on a project, you must ensure that the following pre-requisites are fulfilled:
- Terraform and kubectl are installed on the machine where Terraform is executed.
- The Service Account you execute the module with has the right permissions.
- The APIs are active on the project you will launch the cluster in.
- If you are using a Shared VPC, the APIs must also be activated on the Shared VPC host project and your service account needs the proper permissions there.
- Terraform 0.10.x
- terraform-provider-google plugin v1.8.0
In order to execute this module you must have a Service Account with the following project roles:
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
- Compute Engine API - compute.googleapis.com
Be sure you have the correct Terraform version (0.10.x), you can choose the binary here:
The project has the following folders and files:
- /: root folder
- /examples: examples for using this module
- /scripts: Scripts for specific tasks on module (see Infrastructure section on this file)
- /test: Folders with files for testing the module (see Testing section on this file)
- /main.tf: main file for this module, contains all the resources to create
- /variables.tf: all the variables for the module
- /output.tf: the outputs of the module
- /readme.MD: this file
- bundler
- gcloud
- terraform-docs 0.3.0
Integration tests are run though test-kitchen, kitchen-terraform, and InSpec.
Two test-kitchen instances are defined:
basic
advanced
The test-kitchen instances in test/fixtures/
wrap identically-named examples in the examples/
directory.
- Configure the test fixtures
- Download a Service Account key with the necessary permissions and put it in the module's root directory with the name
credentials.json
. - Build the Docker containers for testing:
make docker_build_terraform
make docker_build_kitchen_terraform
- Run the testing container in interactive mode:
make docker_run
The module root directory will be loaded into the Docker container at /cftk/workdir/
.
5. Run kitchen-terraform to test the infrastructure:
kitchen create
creates Terraform state and downloads modules, if applicable.kitchen converge
creates the underlying resources. Runkitchen converge <INSTANCE_NAME>
to create resources for a specific test case.kitchen verify
tests the created infrastructure. Runkitchen verify <INSTANCE_NAME>
to run a specific test case.kitchen destroy
tears down the underlying resources created bykitchen converge
. Runkitchen destroy <INSTANCE_NAME>
to tear down resources for a specific test case.
Alternatively, you can simply run make test_integration_docker
to run all the test steps non-interactively.
Each test-kitchen instance is configured with a variables.tfvars
file in the test fixture directory, e.g. test/fixtures/basic/terraform.tfvars
.
For convenience, since all of the variables are project-specific, these files have been symlinked to test/fixtures/shared/terraform.tfvars
.
Similarly, each test fixture has a variables.tf
to define these variables, and an outputs.tf
to facilitate providing necessary information for inspec
to locate and query against created resources.
Each test-kitchen instance creates a GCP Network and Subnetwork fixture to house resources, and may create any other necessary fixture data as needed.
Run
make generate_docs
The makefile in this project will lint or sometimes just format any shell, Python, golang, Terraform, or Dockerfiles. The linters will only be run if the makefile finds files with the appropriate file extension.
All of the linter checks are in the default make target, so you just have to run
make -s
The -s is for 'silent'. Successful output looks like this
Running shellcheck
Running flake8
Running go fmt and go vet
Running terraform validate
Running hadolint on Dockerfiles
Checking for required files
Testing the validity of the header check
..
----------------------------------------------------------------------
Ran 2 tests in 0.026s
OK
Checking file headers
The following lines have trailing whitespace
The linters are as follows:
- Shell - shellcheck. Can be found in homebrew
- Python - flake8. Can be installed with 'pip install flake8'
- Golang - gofmt. gofmt comes with the standard golang installation. golang is a compiled language so there is no standard linter.
- Terraform - terraform has a built-in linter in the 'terraform validate' command.
- Dockerfiles - hadolint. Can be found in homebrew