Skip to content

Commit 20911ef

Browse files
committed
updated VPC with IPAM config
1 parent 306f029 commit 20911ef

File tree

9 files changed

+211
-4
lines changed

9 files changed

+211
-4
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ In this module, we have implemented the following CIS Compliance checks for VPC:
205205

206206
| Name | Type |
207207
|------|------|
208+
| [aws_vpc_ipam.ipam](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipam) | resource |
209+
| [aws_vpc_ipam_pool.ipam_pool](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipam_pool) | resource |
210+
| [aws_vpc_ipam_pool_cidr.ipam_pool_cidr](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipam_pool_cidr) | resource |
208211
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
209212
| [aws_ec2_instance_type.arch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type) | data source |
210213

@@ -214,19 +217,24 @@ In this module, we have implemented the following CIS Compliance checks for VPC:
214217
|------|-------------|------|---------|:--------:|
215218
| <a name="input_auto_assign_public_ip"></a> [auto\_assign\_public\_ip](#input\_auto\_assign\_public\_ip) | Specify true to indicate that instances launched into the subnet should be assigned a public IP address. | `bool` | `false` | no |
216219
| <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | Number of Availability Zone to be used by VPC Subnets | `list(any)` | `[]` | no |
220+
| <a name="input_create_ipam_pool"></a> [create\_ipam\_pool](#input\_create\_ipam\_pool) | Whether create new IPAM pool | `bool` | `true` | no |
217221
| <a name="input_database_subnet_assign_ipv6_address_on_creation"></a> [database\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_database\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on database subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
218222
| <a name="input_database_subnet_cidrs"></a> [database\_subnet\_cidrs](#input\_database\_subnet\_cidrs) | Database Tier subnet CIDRs to be created | `list(any)` | `[]` | no |
219223
| <a name="input_database_subnet_enabled"></a> [database\_subnet\_enabled](#input\_database\_subnet\_enabled) | Set true to enable database subnets | `bool` | `false` | no |
220224
| <a name="input_default_network_acl_ingress"></a> [default\_network\_acl\_ingress](#input\_default\_network\_acl\_ingress) | List of maps of ingress rules to set on the Default Network ACL | `list(map(string))` | <pre>[<br> {<br> "action": "deny",<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 22,<br> "protocol": "tcp",<br> "rule_no": 98,<br> "to_port": 22<br> },<br> {<br> "action": "deny",<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 3389,<br> "protocol": "tcp",<br> "rule_no": 99,<br> "to_port": 3389<br> },<br> {<br> "action": "allow",<br> "cidr_block": "0.0.0.0/0",<br> "from_port": 0,<br> "protocol": "-1",<br> "rule_no": 100,<br> "to_port": 0<br> },<br> {<br> "action": "allow",<br> "from_port": 0,<br> "ipv6_cidr_block": "::/0",<br> "protocol": "-1",<br> "rule_no": 101,<br> "to_port": 0<br> }<br>]</pre> | no |
221225
| <a name="input_enable_database_subnet_group"></a> [enable\_database\_subnet\_group](#input\_enable\_database\_subnet\_group) | Whether create database subnet groups | `bool` | `false` | no |
222226
| <a name="input_environment"></a> [environment](#input\_environment) | Specify the environment indentifier for the VPC | `string` | `""` | no |
227+
| <a name="input_existing_ipam_managed_cidr"></a> [existing\_ipam\_managed\_cidr](#input\_existing\_ipam\_managed\_cidr) | The existing IPAM pool CIDR | `string` | `""` | no |
223228
| <a name="input_flow_log_cloudwatch_log_group_kms_key_arn"></a> [flow\_log\_cloudwatch\_log\_group\_kms\_key\_arn](#input\_flow\_log\_cloudwatch\_log\_group\_kms\_key\_arn) | The ARN of the KMS Key to use when encrypting log data for VPC flow logs | `string` | `null` | no |
224229
| <a name="input_flow_log_cloudwatch_log_group_retention_in_days"></a> [flow\_log\_cloudwatch\_log\_group\_retention\_in\_days](#input\_flow\_log\_cloudwatch\_log\_group\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group for VPC flow logs. | `number` | `null` | no |
225230
| <a name="input_flow_log_enabled"></a> [flow\_log\_enabled](#input\_flow\_log\_enabled) | Whether or not to enable VPC Flow Logs | `bool` | `false` | no |
226231
| <a name="input_flow_log_max_aggregation_interval"></a> [flow\_log\_max\_aggregation\_interval](#input\_flow\_log\_max\_aggregation\_interval) | The maximum interval of time during which a flow of packets is captured and aggregated into a flow log record. Valid Values: `60` seconds or `600` seconds. | `number` | `60` | no |
227232
| <a name="input_intra_subnet_assign_ipv6_address_on_creation"></a> [intra\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_intra\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on intra subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
228233
| <a name="input_intra_subnet_cidrs"></a> [intra\_subnet\_cidrs](#input\_intra\_subnet\_cidrs) | A list of intra subnets CIDR to be created | `list(any)` | `[]` | no |
229234
| <a name="input_intra_subnet_enabled"></a> [intra\_subnet\_enabled](#input\_intra\_subnet\_enabled) | Set true to enable intra subnets | `bool` | `false` | no |
235+
| <a name="input_ipam_enabled"></a> [ipam\_enabled](#input\_ipam\_enabled) | Whether enable IPAM managed VPC or not | `bool` | `false` | no |
236+
| <a name="input_ipam_pool_id"></a> [ipam\_pool\_id](#input\_ipam\_pool\_id) | The existing IPAM pool id if any | `string` | `null` | no |
237+
| <a name="input_ipv4_netmask_length"></a> [ipv4\_netmask\_length](#input\_ipv4\_netmask\_length) | The netmask length for IPAM managed VPC | `number` | `16` | no |
230238
| <a name="input_ipv6_enabled"></a> [ipv6\_enabled](#input\_ipv6\_enabled) | Requests an Amazon-provided IPv6 CIDR block with a /56 prefix length for the VPC. You cannot specify the range of IP addresses, or the size of the CIDR block. | `bool` | `false` | no |
231239
| <a name="input_ipv6_only"></a> [ipv6\_only](#input\_ipv6\_only) | Enable it for deploying native IPv6 network | `bool` | `false` | no |
232240
| <a name="input_name"></a> [name](#input\_name) | Specify the name of the VPC | `string` | `""` | no |
@@ -237,6 +245,7 @@ In this module, we have implemented the following CIS Compliance checks for VPC:
237245
| <a name="input_public_subnet_assign_ipv6_address_on_creation"></a> [public\_subnet\_assign\_ipv6\_address\_on\_creation](#input\_public\_subnet\_assign\_ipv6\_address\_on\_creation) | Assign IPv6 address on public subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map\_public\_ip\_on\_launch | `bool` | `null` | no |
238246
| <a name="input_public_subnet_cidrs"></a> [public\_subnet\_cidrs](#input\_public\_subnet\_cidrs) | A list of public subnets CIDR to be created inside the VPC | `list(any)` | `[]` | no |
239247
| <a name="input_public_subnet_enabled"></a> [public\_subnet\_enabled](#input\_public\_subnet\_enabled) | Set true to enable public subnets | `bool` | `false` | no |
248+
| <a name="input_region"></a> [region](#input\_region) | The AWS region name | `string` | n/a | yes |
240249
| <a name="input_secondary_cidr_blocks"></a> [secondary\_cidr\_blocks](#input\_secondary\_cidr\_blocks) | List of the secondary CIDR blocks which can be at most 5 | `list(string)` | `[]` | no |
241250
| <a name="input_secondry_cidr_enabled"></a> [secondry\_cidr\_enabled](#input\_secondry\_cidr\_enabled) | Whether enable secondary CIDR with VPC | `bool` | `false` | no |
242251
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | The CIDR block of the VPC | `string` | `"10.0.0.0/16"` | no |

examples/ipam-managed-vpc/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# IPAM VPC
2+
3+
Configuration in this directory creates set of VPC resources with IPAM managed CIDRs
4+
5+
IPAM pool with desired CIDR and its allocation which restricts the overlapping of CIDRs
6+
7+
[Read more about AWS regions, availability zones and local zones](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-regions-availability-zones).
8+
9+
## Usage
10+
11+
To run this example you need to execute:
12+
13+
```bash
14+
$ terraform init
15+
$ terraform plan
16+
$ terraform apply
17+
```
18+
19+
Note that this example may create resources which can cost money (AWS Elastic IP, for example). Run `terraform destroy` when you don't need these resources.
20+
21+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
22+
## Requirements
23+
24+
No requirements.
25+
26+
## Providers
27+
28+
No providers.
29+
30+
## Modules
31+
32+
| Name | Source | Version |
33+
|------|--------|---------|
34+
| <a name="module_vpc_ipam"></a> [vpc\_ipam](#module\_vpc\_ipam) | ../.. | n/a |
35+
36+
## Resources
37+
38+
No resources.
39+
40+
## Inputs
41+
42+
No inputs.
43+
44+
## Outputs
45+
46+
| Name | Description |
47+
|------|-------------|
48+
| <a name="output_public_subnets"></a> [public\_subnets](#output\_public\_subnets) | List of IDs of public subnets |
49+
| <a name="output_region"></a> [region](#output\_region) | AWS Region |
50+
| <a name="output_vpc_cidr_block"></a> [vpc\_cidr\_block](#output\_vpc\_cidr\_block) | AWS Region |
51+
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | The ID of the VPC |
52+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/ipam-managed-vpc/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
locals {
2+
region = "us-east-1"
3+
environment = "stage"
4+
name = "skaf"
5+
additional_aws_tags = {
6+
Owner = "SquareOps"
7+
Expires = "Never"
8+
Department = "Engineering"
9+
}
10+
vpc_cidr = "10.10.0.0/16"
11+
ipam_enabled = true
12+
}
13+
14+
module "vpc_ipam" {
15+
source = "../.."
16+
17+
name = local.name
18+
19+
ipam_enabled = local.ipam_enabled
20+
region = local.region
21+
create_ipam_pool = true
22+
vpc_cidr = local.vpc_cidr
23+
availability_zones = ["ap-south-1a", "ap-south-1b"]
24+
25+
private_subnet_enabled = true
26+
public_subnet_enabled = true
27+
}

examples/ipam-managed-vpc/output.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "region" {
2+
description = "AWS Region"
3+
value = local.region
4+
}
5+
6+
output "vpc_id" {
7+
description = "The ID of the VPC"
8+
value = module.vpc_ipam.vpc_id
9+
}
10+
11+
output "vpc_cidr_block" {
12+
description = "AWS Region"
13+
value = module.vpc_ipam.vpc_cidr_block
14+
}
15+
16+
output "public_subnets" {
17+
description = "List of IDs of public subnets"
18+
value = module.vpc_ipam.public_subnets
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
provider "aws" {
2+
region = local.region
3+
default_tags {
4+
tags = local.additional_aws_tags
5+
}
6+
}

examples/vpc-native-ipv6/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# VPC with IPv6 support
1+
# VPC with Native IPv6 support
22

33

4-
VPC with dual stack IP mode enabled IPv6 and IPv4 includes public and private subnet will be created per availability zone in addition to single NAT Gateway shared between all availability zones.
4+
VPC with native IPv6 which includes public and private subnet will be created per availability zone in addition to single NAT Gateway shared between all availability zones.
55

66
## Usage
77

examples/vpc-with-secondary-cidr/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# VPC with Private Subnets
1+
# VPC with Multiple CIDR Supports
22

33

4-
A public and private subnet will be created per availability zone in addition to single NAT Gateway shared between all availability zones.
4+
MUltiple CIDRs can be attached with a VPC in addition of increasing numbers of IPs. A public and private subnet will be created per availability zone in addition to single NAT Gateway shared between all availability zones.
55

66
## Usage
77

main.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
locals {
22
azs = length(var.availability_zones)
3+
region_name = var.region
34
public_subnets_native = var.public_subnet_enabled ? length(var.public_subnet_cidrs) > 0 ? var.public_subnet_cidrs : [for netnum in range(0, local.azs) : cidrsubnet(var.vpc_cidr, 8, netnum)] : []
45
secondary_public_subnets = var.public_subnet_enabled && var.secondry_cidr_enabled ? [
56
for cidr_block in var.secondary_cidr_blocks : [
@@ -69,6 +70,9 @@ module "vpc" {
6970
name = format("%s-%s-vpc", var.environment, var.name)
7071
cidr = var.vpc_cidr # CIDR FOR VPC
7172
azs = [for n in range(0, local.azs) : data.aws_availability_zones.available.names[n]]
73+
use_ipam_pool = var.ipam_enabled ? true : false
74+
ipv4_ipam_pool_id = var.create_ipam_pool ? aws_vpc_ipam_pool.ipam_pool[0].id : var.ipam_pool_id
75+
ipv4_netmask_length = var.ipv4_netmask_length
7276
create_database_subnet_group = length(local.database_subnets) > 1 && var.enable_database_subnet_group ? true : false
7377
intra_subnets = local.intra_subnets
7478
public_subnets = local.public_subnets
@@ -186,3 +190,52 @@ module "vpn_server" {
186190
public_subnet = module.vpc.public_subnets[0]
187191
vpn_server_instance_type = var.vpn_server_instance_type
188192
}
193+
194+
resource "aws_vpc_ipam" "ipam" {
195+
count = var.ipam_enabled && var.create_ipam_pool ? 1 : 0
196+
operating_regions {
197+
region_name = local.region_name
198+
}
199+
200+
#tags = var.tags
201+
}
202+
203+
# IPv4
204+
resource "aws_vpc_ipam_pool" "ipam_pool" {
205+
count = var.ipam_enabled && var.create_ipam_pool ? 1 : 0
206+
description = "IPv4 pool"
207+
address_family = "ipv4"
208+
ipam_scope_id = aws_vpc_ipam.ipam[0].private_default_scope_id
209+
locale = local.region_name
210+
allocation_default_netmask_length = 16
211+
212+
#tags = var.tags
213+
}
214+
215+
resource "aws_vpc_ipam_pool_cidr" "ipam_pool_cidr" {
216+
count = var.ipam_enabled ? 1 : 0
217+
ipam_pool_id = var.create_ipam_pool ? aws_vpc_ipam_pool.ipam_pool[0].id : var.ipam_pool_id
218+
cidr = var.create_ipam_pool ? var.vpc_cidr : var.existing_ipam_managed_cidr
219+
}
220+
221+
# resource "aws_vpc_ipam_preview_next_cidr" "this" {
222+
# ipam_pool_id = aws_vpc_ipam_pool.this.id
223+
224+
# depends_on = [
225+
# aws_vpc_ipam_pool_cidr.this
226+
# ]
227+
# }
228+
229+
# IPv6
230+
# resource "aws_vpc_ipam_pool" "ipv6" {
231+
# count =
232+
# description = "IPv6 pool"
233+
# address_family = "ipv6"
234+
# ipam_scope_id = aws_vpc_ipam.this.public_default_scope_id
235+
# locale = var.region
236+
# allocation_default_netmask_length = 56
237+
# publicly_advertisable = false
238+
# aws_service = "ec2"
239+
240+
# #tags = var.tags
241+
# }

variables.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,44 @@ variable "enable_database_subnet_group" {
221221
default = false
222222
type = bool
223223
}
224+
225+
# variable "tags" {
226+
# description = "The Tags attached with the resources"
227+
# default = {}
228+
# type = any
229+
# }
230+
231+
variable "ipam_pool_id" {
232+
description = "The existing IPAM pool id if any"
233+
default = null
234+
type = string
235+
}
236+
237+
variable "ipam_enabled" {
238+
description = "Whether enable IPAM managed VPC or not"
239+
default = false
240+
type = bool
241+
}
242+
243+
variable "create_ipam_pool" {
244+
description = "Whether create new IPAM pool"
245+
default = true
246+
type = bool
247+
}
248+
249+
variable "ipv4_netmask_length" {
250+
description = "The netmask length for IPAM managed VPC"
251+
default = 16
252+
type = number
253+
}
254+
255+
variable "region" {
256+
description = "The AWS region name"
257+
type = string
258+
}
259+
260+
variable "existing_ipam_managed_cidr" {
261+
description = "The existing IPAM pool CIDR"
262+
default = ""
263+
type = string
264+
}

0 commit comments

Comments
 (0)