Skip to content

Commit 668f92a

Browse files
committed
add ipam ipv4 support
1 parent 57ba0ef commit 668f92a

File tree

8 files changed

+94
-7
lines changed

8 files changed

+94
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ Full contributing [guidelines are covered here](.github/contributing.md).
203203
| Name | Version |
204204
|------|---------|
205205
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
206-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
206+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.68 |
207207

208208
## Providers
209209

210210
| Name | Version |
211211
|------|---------|
212-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
212+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.68 |
213213

214214
## Modules
215215

@@ -304,7 +304,7 @@ No modules.
304304
| <a name="input_amazon_side_asn"></a> [amazon\_side\_asn](#input\_amazon\_side\_asn) | The Autonomous System Number (ASN) for the Amazon side of the gateway. By default the virtual private gateway is created with the current default Amazon ASN. | `string` | `"64512"` | no |
305305
| <a name="input_assign_ipv6_address_on_creation"></a> [assign\_ipv6\_address\_on\_creation](#input\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `false` | no |
306306
| <a name="input_azs"></a> [azs](#input\_azs) | A list of availability zones names or ids in the region | `list(string)` | `[]` | no |
307-
| <a name="input_cidr"></a> [cidr](#input\_cidr) | The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden | `string` | `"0.0.0.0/0"` | no |
307+
| <a name="input_cidr"></a> [cidr](#input\_cidr) | (Optional) The IPv4 CIDR block for the VPC. CIDR can be explicitly set or it can be derived from IPAM using `ipv4_netmask_length` & `ipv4_ipam_pool_id` | `string` | `null` | no |
308308
| <a name="input_create_database_internet_gateway_route"></a> [create\_database\_internet\_gateway\_route](#input\_create\_database\_internet\_gateway\_route) | Controls if an internet gateway route for public database access should be created | `bool` | `false` | no |
309309
| <a name="input_create_database_nat_gateway_route"></a> [create\_database\_nat\_gateway\_route](#input\_create\_database\_nat\_gateway\_route) | Controls if a nat gateway route should be created to give internet access to the database subnets | `bool` | `false` | no |
310310
| <a name="input_create_database_subnet_group"></a> [create\_database\_subnet\_group](#input\_create\_database\_subnet\_group) | Controls if database subnet group should be created (n.b. database\_subnets must also be set) | `bool` | `true` | no |
@@ -402,6 +402,8 @@ No modules.
402402
| <a name="input_intra_subnet_suffix"></a> [intra\_subnet\_suffix](#input\_intra\_subnet\_suffix) | Suffix to append to intra subnets name | `string` | `"intra"` | no |
403403
| <a name="input_intra_subnet_tags"></a> [intra\_subnet\_tags](#input\_intra\_subnet\_tags) | Additional tags for the intra subnets | `map(string)` | `{}` | no |
404404
| <a name="input_intra_subnets"></a> [intra\_subnets](#input\_intra\_subnets) | A list of intra subnets | `list(string)` | `[]` | no |
405+
| <a name="input_ipv4_ipam_pool_id"></a> [ipv4\_ipam\_pool\_id](#input\_ipv4\_ipam\_pool\_id) | (Optional) The ID of an IPv4 IPAM pool you want to use for allocating this VPC's CIDR. | `string` | `null` | no |
406+
| <a name="input_ipv4_netmask_length"></a> [ipv4\_netmask\_length](#input\_ipv4\_netmask\_length) | (Optional) The netmask length of the IPv4 CIDR you want to allocate to this VPC. Requires specifying a ipv4\_ipam\_pool\_id. | `number` | `null` | no |
405407
| <a name="input_manage_default_network_acl"></a> [manage\_default\_network\_acl](#input\_manage\_default\_network\_acl) | Should be true to adopt and manage Default Network ACL | `bool` | `false` | no |
406408
| <a name="input_manage_default_route_table"></a> [manage\_default\_route\_table](#input\_manage\_default\_route\_table) | Should be true to manage default route table | `bool` | `false` | no |
407409
| <a name="input_manage_default_security_group"></a> [manage\_default\_security\_group](#input\_manage\_default\_security\_group) | Should be true to adopt and manage default security group | `bool` | `false` | no |

examples/ipam-vpc/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
locals {
6+
name = "ipam-vpc-example"
7+
}
8+
9+
# IPAM Setup
10+
data "aws_region" "current" {}
11+
12+
resource "aws_vpc_ipam" "example" {
13+
operating_regions {
14+
region_name = data.aws_region.current.name
15+
}
16+
}
17+
18+
resource "aws_vpc_ipam_pool" "ipv4_example" {
19+
address_family = "ipv4"
20+
ipam_scope_id = aws_vpc_ipam.example.private_default_scope_id
21+
locale = data.aws_region.current.name
22+
allocation_default_netmask_length = 28
23+
}
24+
25+
resource "aws_vpc_ipam_pool_cidr" "ipv4_example" {
26+
ipam_pool_id = aws_vpc_ipam_pool.ipv4_example.id
27+
cidr = "172.2.0.0/16"
28+
}
29+
30+
# Usage Patterns
31+
32+
module "no_ipam_vpc_example" {
33+
source = "../.."
34+
name = "no-ipam-${local.name}"
35+
cidr = "172.2.0.32/28"
36+
}
37+
38+
module "ipv4_ipam_explicit_cidr_vpc" {
39+
source = "../.."
40+
name = "ipv4-explicit-cidr-${local.name}"
41+
ipv4_ipam_pool_id = aws_vpc_ipam_pool.ipv4_example.id
42+
cidr = "172.2.0.32/28"
43+
}
44+
45+
module "ipv4_ipam_explicit_netmask_vpc" {
46+
source = "../.."
47+
name = "ipv4-explicit-netmask-${local.name}"
48+
ipv4_ipam_pool_id = aws_vpc_ipam_pool.ipv4_example.id
49+
ipv4_netmask_length = 28
50+
}
51+
52+
module "ipv4_ipam_default_netmask_vpc" {
53+
source = "../.."
54+
name = "ipv4-default-netmask-${local.name}"
55+
ipv4_ipam_pool_id = aws_vpc_ipam_pool.ipv4_example.id
56+
}

examples/ipam-vpc/outputs.tf

Whitespace-only changes.

examples/ipam-vpc/variables.tf

Whitespace-only changes.

examples/ipam-vpc/versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 0.13.1"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 3.68"
8+
}
9+
}
10+
}

main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ locals {
1616
),
1717
0,
1818
)
19+
ipv4_using_ipam = var.ipv4_ipam_pool_id == null ? false : true
20+
ipv4_ipam_with_explicit_cidr = var.ipv4_ipam_pool_id != null && var.cidr != null ? true : false
21+
ipv4_ipam_with_explicit_netmask = local.ipv4_using_ipam && var.ipv4_netmask_length != null ? var.ipv4_netmask_length : null
22+
cidr = !local.ipv4_using_ipam || local.ipv4_ipam_with_explicit_cidr == true ? var.cidr : null
1923
}
2024

2125
################################################################################
@@ -25,7 +29,10 @@ locals {
2529
resource "aws_vpc" "this" {
2630
count = var.create_vpc ? 1 : 0
2731

28-
cidr_block = var.cidr
32+
cidr_block = local.cidr
33+
ipv4_ipam_pool_id = local.ipv4_using_ipam ? var.ipv4_ipam_pool_id : null
34+
ipv4_netmask_length = local.ipv4_ipam_with_explicit_netmask
35+
2936
instance_tenancy = var.instance_tenancy
3037
enable_dns_hostnames = var.enable_dns_hostnames
3138
enable_dns_support = var.enable_dns_support

variables.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ variable "name" {
1111
}
1212

1313
variable "cidr" {
14-
description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden"
14+
description = "(Optional) The IPv4 CIDR block for the VPC. CIDR can be explicitly set or it can be derived from IPAM using `ipv4_netmask_length` & `ipv4_ipam_pool_id`"
1515
type = string
16-
default = "0.0.0.0/0"
16+
default = null
1717
}
1818

1919
variable "enable_ipv6" {
@@ -1174,3 +1174,15 @@ variable "flow_log_per_hour_partition" {
11741174
type = bool
11751175
default = false
11761176
}
1177+
1178+
variable "ipv4_ipam_pool_id" {
1179+
description = "(Optional) The ID of an IPv4 IPAM pool you want to use for allocating this VPC's CIDR."
1180+
type = string
1181+
default = null
1182+
}
1183+
1184+
variable "ipv4_netmask_length" {
1185+
description = "(Optional) The netmask length of the IPv4 CIDR you want to allocate to this VPC. Requires specifying a ipv4_ipam_pool_id."
1186+
type = number
1187+
default = null
1188+
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.63"
7+
version = ">= 3.68"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)