...
1/**
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17```hcl
18resource "google_compute_network" "net" {
19 name = "my-network"
20 auto_create_subnetworks = false
21}
22
23resource "google_compute_subnetwork" "subnet" {
24 name = "my-subnetwork"
25 network = google_compute_network.net.id
26 ip_cidr_range = "10.0.0.0/16"
27 region = "us-central1"
28}
29
30resource "google_compute_router" "router" {
31 name = "my-router"
32 region = google_compute_subnetwork.subnet.region
33 network = google_compute_network.net.id
34}
35
36resource "google_compute_address" "addr1" {
37 name = "nat-address1"
38 region = google_compute_subnetwork.subnet.region
39}
40
41resource "google_compute_address" "addr2" {
42 name = "nat-address2"
43 region = google_compute_subnetwork.subnet.region
44}
45
46resource "google_compute_address" "addr3" {
47 name = "nat-address3"
48 region = google_compute_subnetwork.subnet.region
49}
50
51resource "google_compute_router_nat" "nat_rules" {
52 name = "my-router-nat"
53 router = google_compute_router.router.name
54 region = google_compute_router.router.region
55
56 nat_ip_allocate_option = "MANUAL_ONLY"
57 nat_ips = [google_compute_address.addr1.self_link]
58
59 source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
60 subnetwork {
61 name = google_compute_subnetwork.subnet.id
62 source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
63 }
64
65 rules {
66 rule_number = 100
67 description = "nat rules example"
68 match = "inIpRange(destination.ip, '1.1.0.0/16') || inIpRange(destination.ip, '2.2.0.0/16')"
69 action {
70 source_nat_active_ips = [google_compute_address.addr2.self_link, google_compute_address.addr3.self_link]
71 }
72 }
73
74 enable_endpoint_independent_mapping = false
75}
76```
View as plain text