Skip to content

aws-ia/terraform-aws-ipam

Repository files navigation

Terraform Module for Amazon VPC IP Address Manager on AWS

Note: For information regarding the 2.0 upgrade see our upgrade guide.

This module helps deploy AWS IPAM including IPAM Pools, Provisioned CIDRs, and can help with sharing those pools via AWS RAM.

Built to accommodate a wide range of use cases, this Terraform module can deploy both simple and complex Amazon Virtual Private Cloud (Amazon VPC) IP Address Manager (IPAM) configurations. It supports both symmetrically nested, multi-Region deployments (most common IPAM designs) as well as asymmetically nested deployments.

Refer to the examples/ directory in this GitHub repository for examples.

The embedded example below describes a symmetrically nested pool structure, including its configuration, implementation details, requirements, and more.

Architecture Example

symmetrically nested pool deployment

_Note: The diagram above is an example of the type of Pool design you can deploy using this module._

Configuration

This module strongly relies on the var.pool_configuration variable, which is a multi-level, nested map that describes how to nest your IPAM pools. It can accept most aws_vpc_ipam_pool and aws_vpc_ipam_pool_cidr attributes (detailed below) as well as RAM share pools (at any level) to valid AWS principals. Nested pools do not inherit attributes from their source pool(s), so all configuration options are available at each level. locale is implied in sub pools after declared in a parent.

In this module, pools can be nested up to four levels, including one root pool and up to three nested pools. The root pool defines the address_family variable. If you want to deploy an IPv4 and IPv6 pool structure, you must instantiate the module for each type.

The pool_configurations variable is the structure of the other three levels. The sub_pool submodule has a var.pool_config variable that defines the structure that each pool can accept. The variable has the following structure:

pool_configurations = {
  my_pool_name = {
    description      = "my pool"
    cidr             = ["10.0.0.0/16"]
    locale           = "us-east-1"

    sub_pools = {

      sandbox = {
        cidr = ["10.0.48.0/20"]
        ram_share_principals = [local.dev_ou_arn]
        # ...any pool_config argument (below)
      }
    }
  }
}

The key of a pool_config variable is the name of the pool, followed by its attributes ram_share_principals and a sub_pools map, which is another nested pool_config variable.

variable "pool_config" {
  type = object({
    cidr                 = list(string)
    ram_share_principals = optional(list(string))

    name                              = optional(string)
    locale                            = optional(string)
    allocation_default_netmask_length = optional(string)
    allocation_max_netmask_length     = optional(string)
    allocation_min_netmask_length     = optional(string)
    auto_import                       = optional(string)
    aws_service                       = optional(string)
    description                       = optional(string)
    publicly_advertisable             = optional(bool)

    allocation_resource_tags   = optional(map(string))
    tags                       = optional(map(string))

    sub_pools = optional(any)
  })
}

RAM Sharing

This module allows you to share invidual pools to any valid RAM principal. All levels of var.pool_configurations accept an argument ram_share_principals which should be a list of valid RAM share principals (org-id, ou-id, or account id).

Using Outputs

Since resources are dynamically generated based on user configuration, we roll them into grouped outputs. For example, to get attributes off your level 2 pools:

The output pools_level_2 offers you a map of every pool where the name is the route of the tree keys example "corporate-us-west-2/dev".

To get a specific ID:

> module.basic.pools_level_2["corporate-us-west-2/dev"].id
"ipam-pool-0c816929a16f08747"

To get all IDs

> [ for pool in module.basic.pools_level_2: pool["id"]]
[
  "ipam-pool-0c816929a16f08747",
  "ipam-pool-0192c70b370384661",
  "ipam-pool-037bb0524f8b3278e",
  "ipam-pool-09400d26a6d1df4a5",
  "ipam-pool-0ee5ebe8f8d2d7187",
]

Implementation

Implied pool names and descriptions

By default, pool Name tags and pool descriptions are implied from the name-hierarchy structure of the pool. For example, a pool with two parents us-east-1 and dev has an implied name and description value of us-east-1/dev. You can override either or both name and description at any pool level by specifying a name or description value.

Locales

IPAM pools do not inherit attributes from their parent pools. Locales cannot change from parent to child. For that reason, after a pool in the var.pool_configurations variable defines a locale value, all other child pools have an implied_locale value.

Operating Regions

The IPAM operating_region variable must be set for the primary Region in your Terraform provider block and any Regions you want to set a locale. For that reason, the value of the aws_vpc_ipam.operating_regions variable is constructed by combining the pool_configurations and data.aws_region.current.name attributes.

Requirements

Name Version
terraform >= 1.3.0
aws >= 4.53.0

Providers

Name Version
aws >= 4.53.0

Modules

Name Source Version
level_one ./modules/sub_pool n/a
level_three ./modules/sub_pool n/a
level_two ./modules/sub_pool n/a
level_zero ./modules/sub_pool n/a

Resources

Name Type
aws_vpc_ipam.main resource
aws_region.current data source

Inputs

Name Description Type Default Required
address_family IPv4/6 address family. string "ipv4" no
create_ipam Determines whether to create an IPAM. If false, you must also provide a var.ipam_scope_id. bool true no
ipam_scope_id (Optional) Required if var.ipam_id is set. Determines which scope to deploy pools into. string null no
ipam_scope_type Which scope type to use. Valid inputs include public or private. You can alternatively provide your own scope ID. string "private" no
pool_configurations A multi-level, nested map describing nested IPAM pools. Can nest up to three levels with the top level being outside the pool_configurations in vars prefixed top_. If arugument descriptions are omitted, you can find them in the official documentation.

- ram_share_principals = (optional, list(string)) of valid organization principals to create ram shares to.
- name = (optional, string) name to give the pool, the key of your map in var.pool_configurations will be used if omitted.
- description = (optional, string) description to give the pool, the key of your map in var.pool_configurations will be used if omitted.
- cidr = (optional, list(string)) list of CIDRs to provision into pool. Conflicts with netmask_length.
- netmask_length = (optional, number) netmask length to request provisioned into pool. Conflicts with cidr.

- locale = (optional, string) locale to set for pool.
- auto_import = (optional, string)
- tags = (optional, map(string))
- allocation_default_netmask_length = (optional, string)
- allocation_max_netmask_length = (optional, string)
- allocation_min_netmask_length = (optional, string)
- allocation_resource_tags = (optional, map(string))

The following arguments are available but only relevant for public ips
- cidr_authorization_context = (optional, map(string)) Details found in official documentation.
- aws_service = (optional, string)
- publicly_advertisable = (optional, bool)

- sub_pools = (nested repeats of pool_configuration object above)
any {} no
tags Tags to add to the aws_vpc_ipam resource. any {} no
top_auto_import auto_import setting for top-level pool. bool null no
top_aws_service AWS service, for usage with public IPs. Valid values "ec2". string null no
top_cidr Top-level CIDR blocks. list(string) null no
top_cidr_authorization_contexts CIDR must match a CIDR defined in var.top_cidr. A list of signed documents that proves that you are authorized to bring the specified IP address range to Amazon using BYOIP. Document is not stored in the state file. For more information, refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_ipam_pool_cidr#cidr_authorization_context.
list(object({
cidr = string
message = string
signature = string
}))
[] no
top_description Description of top-level pool. string "" no
top_locale locale of the top-level pool. Do not use this value unless building an ipv6 contiguous block pool. You will have to instantiate the module for each operating region you want a pool structure in. string null no
top_name Name of top-level pool. string null no
top_netmask_length Top-level netmask length to request. Not possible to use for IPv4. Only possible to use with amazon provided ipv6. number null no
top_public_ip_source public IP source for usage with public IPs. Valid values "amazon" or "byoip". string null no
top_publicly_advertisable Whether or not the top-level pool is publicly advertisable. bool null no
top_ram_share_principals Principals to create RAM shares for top-level pool. list(string) null no

Outputs

Name Description
ipam_info If created, ouput the IPAM object information.
operating_regions List of all IPAM operating regions.
pool_level_0 Map of all pools at level 0.
pool_names List of all pool names.
pools_level_1 Map of all pools at level 1.
pools_level_2 Map of all pools at level 2.
pools_level_3 Map of all pools at level 3.