Skip to content

PimpMyNines/Terraform-AWS-IPAM-Configuration

 
 

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.7
aws ~> 5.34
random 3.6.0
time 0.10.0

Providers

Name Version
aws ~> 5.34
time 0.10.0

Modules

Name Source Version
label github.com/PimpMyNines/Terraform-AWS-Labels-Tags.git v1.0.2
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
time_static.creation resource
aws_caller_identity.current data source
aws_partition.current data source
aws_region.current data source

Inputs

Name Description Type Default Required
access_role Role used by IAM boundaries to permit access withing defined boundaries string null no
additional_tag_map Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string) {} no
address_family IPv4/6 address family. string "ipv4" no
aft_managed AFT managed string "true" no
attributes ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string) [] no
aws_deployment_profile Name of AWS Profile to use for Deployment string null no
aws_deployment_role Name of AWS Role to use for Deployment string null no
aws_prefix_external Prefix for external resources string "global/platform/external" no
aws_prefix_internal Prefix for internal resources string "global/platform/internal" no
aws_prefix_service Prefix for service resources string "global/platform/services" no
aws_principal_org_id AWS Organizations ID string for the remote state trust policy string null no
context Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"default"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
no
create_ipam Determines whether to create an IPAM. If false, you must also provide a var.ipam_scope_id. bool true no
delimiter Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
string null no
descriptor_formats Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br> format = string<br> labels = list(string)<br>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any {} no
enabled Set to false to prevent the module from creating any resources bool null no
environment ID element. Used to define either 'live' or 'sdlc' environment type. (Live = Production and Staging, SDLC = QA, Development, Sandbox, and all other types string null no
git_repo Git Repository for making changes to this configuration string null no
id_length_limit Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
number null 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
label_key_case Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
string null no
label_order The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string) null no
label_value_case Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
string null no
labels_as_tags Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string)
[
"default"
]
no
monitored_by MonitoredBy tag string "cloudwatch" no
name ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
string null no
namespace ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique string null 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
regex_replace_chars Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
string null no
stage ID element. Usually used to indicate role, e.g. 'Production', 'Staging', 'QA', 'Development', 'Sandbox' string null no
tags Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string) {} no
tenant ID element. A team identifier, indicating which team owns this resource string null no
terraform_managed Terraform managed string "true" 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.