Skip to content

Commit 6eddcad

Browse files
authored
feat: Add IPAM IPv4 support (#716)
1 parent a06dd17 commit 6eddcad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+996
-105
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.72.1
3+
rev: v1.75.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,54 @@ Sometimes it is handy to have public access to Redshift clusters (for example if
181181

182182
It is possible to integrate this VPC module with [terraform-aws-transit-gateway module](https://github.com/terraform-aws-modules/terraform-aws-transit-gateway) which handles the creation of TGW resources and VPC attachments. See [complete example there](https://github.com/terraform-aws-modules/terraform-aws-transit-gateway/tree/master/examples/complete).
183183

184+
## VPC CIDR from AWS IP Address Manager (IPAM)
185+
186+
It is possible to have your VPC CIDR assigned from an [AWS IPAM Pool](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipam_pool). However, In order to build subnets within this module Terraform must know subnet CIDRs to properly plan the amount of resources to build. Since CIDR is derived by IPAM by calling CreateVpc this is not possible within a module unless cidr is known ahead of time. You can get around this by "previewing" the CIDR and then using that as the subnet values.
187+
188+
_Note: Due to race conditions with `terraform plan`, it is not possible to use `ipv4_netmask_length` or a pools `allocation_default_netmask_length` within this module. You must explicitly set the CIDRs for a pool to use._
189+
190+
```hcl
191+
# Find the pool RAM shared to your account
192+
# Info on RAM sharing pools: https://docs.aws.amazon.com/vpc/latest/ipam/share-pool-ipam.html
193+
data "aws_vpc_ipam_pool" "ipv4_example" {
194+
filter {
195+
name = "description"
196+
values = ["*mypool*"]
197+
}
198+
199+
filter {
200+
name = "address-family"
201+
values = ["ipv4"]
202+
}
203+
}
204+
205+
# Preview next CIDR from pool
206+
data "aws_vpc_ipam_preview_next_cidr" "previewed_cidr" {
207+
ipam_pool_id = data.aws_vpc_ipam_pool.ipv4_example.id
208+
netmask_length = 24
209+
}
210+
211+
data "aws_region" "current" {}
212+
213+
# Calculate subnet cidrs from previewed IPAM CIDR
214+
locals {
215+
partition = cidrsubnets(data.aws_vpc_ipam_preview_next_cidr.previewed_cidr.cidr, 2, 2)
216+
private_subnets = cidrsubnets(local.partition[0], 2, 2)
217+
public_subnets = cidrsubnets(local.partition[1], 2, 2)
218+
azs = formatlist("${data.aws_region.current.name}%s", ["a", "b"])
219+
}
220+
221+
module "vpc_cidr_from_ipam" {
222+
source = "terraform-aws-modules/vpc/aws"
223+
name = "vpc-cidr-from-ipam"
224+
ipv4_ipam_pool_id = data.aws_vpc_ipam_pool.ipv4_example.id
225+
azs = local.azs
226+
cidr = data.aws_vpc_ipam_preview_next_cidr.previewed_cidr.cidr
227+
private_subnets = local.private_subnets
228+
public_subnets = local.public_subnets
229+
}
230+
```
231+
184232
## Examples
185233

186234
- [Simple VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/simple-vpc)
@@ -190,6 +238,7 @@ It is possible to integrate this VPC module with [terraform-aws-transit-gateway
190238
- [Network ACL](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/network-acls)
191239
- [VPC Flow Logs](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/vpc-flow-logs)
192240
- [VPC with Outpost](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/outpost)
241+
- [VPC CIDR from IPAM](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/ipam-vpc)
193242
- [Manage Default VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/manage-default-vpc)
194243
- [Few tests and edge case examples](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/issues)
195244

@@ -205,13 +254,13 @@ Full contributing [guidelines are covered here](.github/contributing.md).
205254
| Name | Version |
206255
|------|---------|
207256
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
208-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
257+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
209258

210259
## Providers
211260

212261
| Name | Version |
213262
|------|---------|
214-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
263+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.73 |
215264

216265
## Modules
217266

@@ -306,7 +355,7 @@ No modules.
306355
| <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 |
307356
| <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 |
308357
| <a name="input_azs"></a> [azs](#input\_azs) | A list of availability zones names or ids in the region | `list(string)` | `[]` | no |
309-
| <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 |
358+
| <a name="input_cidr"></a> [cidr](#input\_cidr) | (Optional) The IPv4 CIDR block for the VPC. | `string` | `"0.0.0.0/0"` | no |
310359
| <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 |
311360
| <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 |
312361
| <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 |
@@ -405,6 +454,7 @@ No modules.
405454
| <a name="input_intra_subnet_suffix"></a> [intra\_subnet\_suffix](#input\_intra\_subnet\_suffix) | Suffix to append to intra subnets name | `string` | `"intra"` | no |
406455
| <a name="input_intra_subnet_tags"></a> [intra\_subnet\_tags](#input\_intra\_subnet\_tags) | Additional tags for the intra subnets | `map(string)` | `{}` | no |
407456
| <a name="input_intra_subnets"></a> [intra\_subnets](#input\_intra\_subnets) | A list of intra subnets | `list(string)` | `[]` | no |
457+
| <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 |
408458
| <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 |
409459
| <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 |
410460
| <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 |
@@ -560,7 +610,7 @@ No modules.
560610
| <a name="output_public_subnets_ipv6_cidr_blocks"></a> [public\_subnets\_ipv6\_cidr\_blocks](#output\_public\_subnets\_ipv6\_cidr\_blocks) | List of IPv6 cidr\_blocks of public subnets in an IPv6 enabled VPC |
561611
| <a name="output_redshift_network_acl_arn"></a> [redshift\_network\_acl\_arn](#output\_redshift\_network\_acl\_arn) | ARN of the redshift network ACL |
562612
| <a name="output_redshift_network_acl_id"></a> [redshift\_network\_acl\_id](#output\_redshift\_network\_acl\_id) | ID of the redshift network ACL |
563-
| <a name="output_redshift_public_route_table_association_ids"></a> [redshift\_public\_route\_table\_association\_ids](#output\_redshift\_public\_route\_table\_association\_ids) | List of IDs of the public redshidt route table association |
613+
| <a name="output_redshift_public_route_table_association_ids"></a> [redshift\_public\_route\_table\_association\_ids](#output\_redshift\_public\_route\_table\_association\_ids) | List of IDs of the public redshift route table association |
564614
| <a name="output_redshift_route_table_association_ids"></a> [redshift\_route\_table\_association\_ids](#output\_redshift\_route\_table\_association\_ids) | List of IDs of the redshift route table association |
565615
| <a name="output_redshift_route_table_ids"></a> [redshift\_route\_table\_ids](#output\_redshift\_route\_table\_ids) | List of IDs of redshift route tables |
566616
| <a name="output_redshift_subnet_arns"></a> [redshift\_subnet\_arns](#output\_redshift\_subnet\_arns) | List of ARNs of redshift subnets |

examples/complete-vpc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ Note that this example may create resources which can cost money (AWS Elastic IP
2222
| Name | Version |
2323
|------|---------|
2424
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
25-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.63 |
25+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.73 |
2626

2727
## Providers
2828

2929
| Name | Version |
3030
|------|---------|
31-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.63 |
31+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.73 |
3232

3333
## Modules
3434

@@ -136,7 +136,7 @@ No inputs.
136136
| <a name="output_public_subnets_ipv6_cidr_blocks"></a> [public\_subnets\_ipv6\_cidr\_blocks](#output\_public\_subnets\_ipv6\_cidr\_blocks) | List of IPv6 cidr\_blocks of public subnets in an IPv6 enabled VPC |
137137
| <a name="output_redshift_network_acl_arn"></a> [redshift\_network\_acl\_arn](#output\_redshift\_network\_acl\_arn) | ARN of the redshift network ACL |
138138
| <a name="output_redshift_network_acl_id"></a> [redshift\_network\_acl\_id](#output\_redshift\_network\_acl\_id) | ID of the redshift network ACL |
139-
| <a name="output_redshift_public_route_table_association_ids"></a> [redshift\_public\_route\_table\_association\_ids](#output\_redshift\_public\_route\_table\_association\_ids) | List of IDs of the public redshidt route table association |
139+
| <a name="output_redshift_public_route_table_association_ids"></a> [redshift\_public\_route\_table\_association\_ids](#output\_redshift\_public\_route\_table\_association\_ids) | List of IDs of the public redshift route table association |
140140
| <a name="output_redshift_route_table_association_ids"></a> [redshift\_route\_table\_association\_ids](#output\_redshift\_route\_table\_association\_ids) | List of IDs of the redshift route table association |
141141
| <a name="output_redshift_route_table_ids"></a> [redshift\_route\_table\_ids](#output\_redshift\_route\_table\_ids) | List of IDs of redshift route tables |
142142
| <a name="output_redshift_subnet_arns"></a> [redshift\_subnet\_arns](#output\_redshift\_subnet\_arns) | List of ARNs of redshift subnets |

examples/complete-vpc/main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
provider "aws" {
2-
region = "eu-west-1"
2+
region = local.region
33
}
44

55
locals {
6-
name = "complete-example"
6+
name = "ex-${replace(basename(path.cwd), "_", "-")}"
77
region = "eu-west-1"
8+
89
tags = {
9-
Owner = "user"
10-
Environment = "staging"
11-
Name = "complete"
10+
Example = local.name
11+
GithubRepo = "terraform-aws-vpc"
12+
GithubOrg = "terraform-aws-modules"
1213
}
1314
}
1415

examples/complete-vpc/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ output "redshift_route_table_association_ids" {
314314
}
315315

316316
output "redshift_public_route_table_association_ids" {
317-
description = "List of IDs of the public redshidt route table association"
317+
description = "List of IDs of the public redshift route table association"
318318
value = module.vpc.redshift_public_route_table_association_ids
319319
}
320320

examples/complete-vpc/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.73"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)