From 8d19cba5f8b65e1ed1053e5e741af1cc1a65e857 Mon Sep 17 00:00:00 2001 From: jbonnier Date: Fri, 3 Nov 2023 14:01:37 -0400 Subject: [PATCH] Initial commit --- .docs/header.md | 34 +++ .docs/terraform-docs.yaml | 53 ++++ .editorconfig | 11 + .gitattributes | 23 ++ .githooks/pre-commit | 15 ++ .github/ISSUE_TEMPLATE.md | 18 ++ .github/PULL_REQUEST_TEMPLATE.md | 40 +++ .github/workflows/conventional-commits.yml | 21 ++ .github/workflows/terraform-actions.yml | 35 +++ .gitignore | 10 + Makefile | 11 + README.md | 117 +++++++++ context.tf | 279 +++++++++++++++++++++ main.tf | 47 ++++ outputs.tf | 19 ++ variables.tf | 29 +++ versions.tf | 18 ++ 17 files changed, 780 insertions(+) create mode 100644 .docs/header.md create mode 100644 .docs/terraform-docs.yaml create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .githooks/pre-commit create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/conventional-commits.yml create mode 100644 .github/workflows/terraform-actions.yml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 context.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.docs/header.md b/.docs/header.md new file mode 100644 index 0000000..a52fc2d --- /dev/null +++ b/.docs/header.md @@ -0,0 +1,34 @@ +![nventive](https://nventive-public-assets.s3.amazonaws.com/nventive_logo_github.svg?v=2) + +# terraform-aws-network + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](LICENSE) [![Latest Release](https://img.shields.io/github/release/nventive/terraform-aws-network.svg?style=flat-square)](https://github.com/nventive/terraform-aws-network/releases/latest) + +Terraform module to create a VPC with public and private subnets. + +--- + +## Examples + +**IMPORTANT:** We do not pin modules to versions in our examples because of the difficulty of keeping the versions in +the documentation in sync with the latest released versions. We highly recommend that in your code you pin the version +to the exact version you are using so that your infrastructure remains stable, and update versions in a systematic way +so that they do not catch you by surprise. + +```hcl +module "network" { + source = "nventive/network/aws" + # We recommend pinning every module to a specific version + # version = "x.x.x" + + namespace = "eg" + stage = "test" + name = "app" + + vpc_cidr_block = "10.0.0.0/16" + subnets_ipv4_cidr_block = ["10.0.0.0/16"] + subnets_availability_zones = ["us-east-1a", "us-east-1b"] + subnets_nat_gateway_enabled = true + s3_endpoint_enabled = true +} +``` diff --git a/.docs/terraform-docs.yaml b/.docs/terraform-docs.yaml new file mode 100644 index 0000000..a07ebac --- /dev/null +++ b/.docs/terraform-docs.yaml @@ -0,0 +1,53 @@ +formatter: "markdown table" + +header-from: .docs/header.md +footer-from: .docs/footer.md + +recursive: + enabled: false + path: modules + +sections: + hide: [] + show: [] + +content: |- + {{ .Header }} + {{ .Requirements }} + {{ .Providers }} + {{ .Modules }} + {{ .Resources }} + {{ .Inputs }} + {{ .Outputs }} + {{ .Footer }} + +output: + file: "README.md" + mode: replace + template: |- + + {{ .Content }} + + +output-values: + enabled: false + from: "" + +sort: + enabled: true + by: name + +settings: + anchor: true + color: true + default: true + description: false + escape: true + hide-empty: false + html: true + indent: 2 + lockfile: true + read-comments: true + required: true + sensitive: true + type: true diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..01a0da4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +max_line_length = 120 +tab_width = 4 + +[Makefile] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..77c4e75 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,23 @@ +# Define text file attributes. +# - Treat them as text. +# - Ensure no CRLF line-endings, neither on checkout nor on checkin. +# - Detect whitespace errors. +# - Exposed by default in `git diff --color` on the CLI. +# - Validate with `git diff --check`. +# - Deny applying with `git apply --whitespace=error-all`. +# - Fix automatically with `git apply --whitespace=fix`. +*.md text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 +*.tf text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 +*.yml text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 +*.yaml text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 +Makefile text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 +.githooks/* text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 + +# Define binary file attributes. +# - Do not treat them as text. +# - Include binary diff in patches instead of "binary files differ." +*.gif -text diff +*.jpeg -text diff +*.jpg -text diff +*.png -text diff +*.svgz -text diff diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000..8788b9a --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,15 @@ +#!/bin/bash +# +# This hook will block a commit if Terraform isn't formatted properly or if README isn't up to date. +# + +[[ -f "$(which terraform)" ]] || (echo 1>&2 'Git Hooks (pre-commit): Terraform not in $PATH, can''t reformat.' && exit 1) +terraform fmt -check 2>/dev/null || (echo 1>&2 'Git Hooks (pre-commit): Please run `terraform fmt` before committing.' && exit 1) + +[[ -f "$(which terraform-docs)" ]] || (echo 1>&2 'Git Hooks (pre-commit): Terraform-docs not in $PATH, can''t update README file.' && exit 1) +curl -sLo .docs/footer.md https://gist.githubusercontent.com/nventive-devops/7892a2ac9a2cc2ea219dd81796b6ce8b/raw/readme-footer.md +terraform-docs markdown --config .docs/terraform-docs.yaml --output-check . 1>/dev/null 2>/dev/null || ( + echo 1>&2 'Git Hooks (pre-commit): Terraform-docs README file is out of date.' + echo 1>&2 'Please run `make docs` before committing ' + exit 1 +) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..b63c67e --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,18 @@ +## Expected Behavior + + +## Actual Behavior + + +## Steps to Reproduce the Problem + +1. +2. +3. + +## Specifications + +- OS [e.g. Linux, OSX, WSL, etc]: +- Version [e.g. 11.5]: +- Module version: +- Terraform version: diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..43c5c04 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,40 @@ +GitHub Issue: # + + +## Proposed Changes + + + - [ ] Bug fix + - [ ] Feature + - [ ] Code style update (formatting) + - [ ] Refactoring (no functional changes, no api changes) + - [ ] Build or CI related changes + - [ ] Documentation content changes + - [ ] Other, please describe: + + +## What is the current behavior? + + + +## What is the new behavior? + + + +## Checklist + +Please check that your PR fulfills the following requirements: + +- [ ] Documentation has been added/updated. +- [ ] Automated tests for the changes have been added/updated. +- [ ] Commits have been squashed (if applicable). +- [ ] Updated [BREAKING_CHANGES.md](../BREAKING_CHANGES.md) (if you introduced a breaking change). + + + +## Other information + + diff --git a/.github/workflows/conventional-commits.yml b/.github/workflows/conventional-commits.yml new file mode 100644 index 0000000..543dde8 --- /dev/null +++ b/.github/workflows/conventional-commits.yml @@ -0,0 +1,21 @@ +name: Conventional Commits + +on: + pull_request: + types: + - opened + - synchronize + +#env: +# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + validate-commits: + runs-on: ubuntu-latest + + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Commitsar check + uses: docker://aevea/commitsar diff --git a/.github/workflows/terraform-actions.yml b/.github/workflows/terraform-actions.yml new file mode 100644 index 0000000..a74365a --- /dev/null +++ b/.github/workflows/terraform-actions.yml @@ -0,0 +1,35 @@ +name: Terraform actions + +on: + pull_request: + types: + - opened + - synchronize + +jobs: + terraform-format-check: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.event.pull_request.head.ref }} + + - name: Set up Terraform + uses: hashicorp/setup-terraform@v1 + + - name: Check Terraform Code Formatting + run: terraform fmt -check + + - name: Download footer.md + run: curl -sLo .docs/footer.md https://gist.githubusercontent.com/nventive-devops/7892a2ac9a2cc2ea219dd81796b6ce8b/raw/readme-footer.md + + - name: Render terraform docs and push changes back to PR + uses: terraform-docs/gh-actions@main + with: + working-dir: . + git-commit-message: "docs: Update README.md (automated action)" + config-file: .docs/terraform-docs.yaml + output-file: README.md + output-method: replace + git-push: "true" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..368e354 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# IDE +.idea +.vscode + +# Terraform specific +.terraform.lock.hcl +.terraform/ + +# Temporary files +.docs/footer.md diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fbfdedc --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +SHELL:=/bin/sh +.SILENT: + +.PHONY: docs hooks +docs: + curl -sLo .docs/footer.md https://gist.githubusercontent.com/nventive-devops/7892a2ac9a2cc2ea219dd81796b6ce8b/raw/readme-footer.md + terraform-docs markdown --config .docs/terraform-docs.yaml . + +hooks: + cp .githooks/pre-commit .git/hooks/pre-commit + chmod +x .git/hooks/pre-commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..775d161 --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ + +![nventive](https://nventive-public-assets.s3.amazonaws.com/nventive_logo_github.svg?v=2) + +# terraform-aws-network + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](LICENSE) [![Latest Release](https://img.shields.io/github/release/nventive/terraform-aws-network.svg?style=flat-square)](https://github.com/nventive/terraform-aws-network/releases/latest) + +Terraform module to create a VPC with public and private subnets. + +--- + +## Examples + +**IMPORTANT:** We do not pin modules to versions in our examples because of the difficulty of keeping the versions in +the documentation in sync with the latest released versions. We highly recommend that in your code you pin the version +to the exact version you are using so that your infrastructure remains stable, and update versions in a systematic way +so that they do not catch you by surprise. + +```hcl +module "network" { + source = "nventive/network/aws" + # We recommend pinning every module to a specific version + # version = "x.x.x" + + namespace = "eg" + stage = "test" + name = "app" + + vpc_cidr_block = "10.0.0.0/16" + subnets_ipv4_cidr_block = ["10.0.0.0/16"] + subnets_availability_zones = ["us-east-1a", "us-east-1b"] + subnets_nat_gateway_enabled = true + s3_endpoint_enabled = true +} +``` +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 0.13.0 | +| [aws](#requirement\_aws) | >= 3.71 | +| [null](#requirement\_null) | >= 2.0 | +| [template](#requirement\_template) | >= 2.0 | +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | >= 3.71 | +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [subnets](#module\_subnets) | cloudposse/dynamic-subnets/aws | 2.1.0 | +| [this](#module\_this) | cloudposse/label/null | 0.25.0 | +| [vpc](#module\_vpc) | cloudposse/vpc/aws | 2.0.0 | +| [vpce\_s3\_label](#module\_vpce\_s3\_label) | cloudposse/label/null | 0.25.0 | +## Resources + +| Name | Type | +|------|------| +| [aws_vpc_endpoint.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_endpoint) | resource | +| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [additional\_tag\_map](#input\_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 | +| [attributes](#input\_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 | +| [context](#input\_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": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | +| [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | +| [descriptor\_formats](#input\_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
`{
format = string
labels = list(string)
}`
(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](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | +| [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | +| [id\_length\_limit](#input\_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 | +| [label\_key\_case](#input\_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](#input\_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](#input\_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](#input\_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 | +| [name](#input\_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](#input\_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 | +| [regex\_replace\_chars](#input\_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 | +| [s3\_endpoint\_enabled](#input\_s3\_endpoint\_enabled) | Set to `true` to create a VPC gateway endpoint for S3. | `bool` | `false` | no | +| [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | +| [subnets\_availability\_zones](#input\_subnets\_availability\_zones) | Availability zones for the subnets. | `list(string)` |
[
"us-east-1a",
"us-east-1b"
]
| no | +| [subnets\_ipv4\_cidr\_block](#input\_subnets\_ipv4\_cidr\_block) | IPv4 CIDR Block for the subnets. | `list(string)` |
[
"10.0.0.0/16"
]
| no | +| [subnets\_nat\_gateway\_enabled](#input\_subnets\_nat\_gateway\_enabled) | Set to `true` to create a nat gateway in each subnet. | `bool` | `false` | no | +| [tags](#input\_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](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | +| [vpc\_cidr\_block](#input\_vpc\_cidr\_block) | CIDR Block for the VPC. | `string` | `"10.0.0.0/16"` | no | +## Outputs + +| Name | Description | +|------|-------------| +| [private\_subnet\_ids](#output\_private\_subnet\_ids) | IDs of the created private subnets. | +| [public\_subnet\_ids](#output\_public\_subnet\_ids) | IDs of the created public subnets. | +| [s3\_endpoint\_arn](#output\_s3\_endpoint\_arn) | ARN of the S3 endpoint. | +| [vpc\_id](#output\_vpc\_id) | The ID of the VPC. | +## Breaking Changes + +Please consult [BREAKING\_CHANGES.md](BREAKING\_CHANGES.md) for more information about version +history and compatibility. + +## Contributing + +Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for +contributing to this project. + +Be mindful of our [Code of Conduct](CODE\_OF\_CONDUCT.md). + +## We're hiring + +Look for current openings on BambooHR https://nventive.bamboohr.com/careers/ + +## Stay in touch + +[nventive.com](https://nventive.com/) | [Linkedin](https://www.linkedin.com/company/nventive/) | [Instagram](https://www.instagram.com/hellonventive/) | [YouTube](https://www.youtube.com/channel/UCFQyvGEKMO10hEyvCqprp5w) | [Spotify](https://open.spotify.com/show/0lsxfIb6Ttm76jB4wgutob) + \ No newline at end of file diff --git a/context.tf b/context.tf new file mode 100644 index 0000000..5e0ef88 --- /dev/null +++ b/context.tf @@ -0,0 +1,279 @@ +# +# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label +# All other instances of this file should be a copy of that one +# +# +# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf +# and then place it in your Terraform module to automatically get +# Cloud Posse's standard configuration inputs suitable for passing +# to Cloud Posse modules. +# +# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf +# +# Modules should access the whole context as `module.this.context` +# to get the input variables with nulls for defaults, +# for example `context = module.this.context`, +# and access individual variables as `module.this.`, +# with final values filled in. +# +# For example, when using defaults, `module.this.context.delimiter` +# will be null, and `module.this.delimiter` will be `-` (hyphen). +# + +module "this" { + source = "cloudposse/label/null" + version = "0.25.0" # requires Terraform >= 0.13.0 + + enabled = var.enabled + namespace = var.namespace + tenant = var.tenant + environment = var.environment + stage = var.stage + name = var.name + delimiter = var.delimiter + attributes = var.attributes + tags = var.tags + additional_tag_map = var.additional_tag_map + label_order = var.label_order + regex_replace_chars = var.regex_replace_chars + id_length_limit = var.id_length_limit + label_key_case = var.label_key_case + label_value_case = var.label_value_case + descriptor_formats = var.descriptor_formats + labels_as_tags = var.labels_as_tags + + context = var.context +} + +# Copy contents of cloudposse/terraform-null-label/variables.tf here + +variable "context" { + type = any + default = { + enabled = true + namespace = null + tenant = null + environment = null + stage = null + name = null + delimiter = null + attributes = [] + tags = {} + additional_tag_map = {} + regex_replace_chars = null + label_order = [] + id_length_limit = null + label_key_case = null + label_value_case = null + descriptor_formats = {} + # Note: we have to use [] instead of null for unset lists due to + # https://github.com/hashicorp/terraform/issues/28137 + # which was not fixed until Terraform 1.0.0, + # but we want the default to be all the labels in `label_order` + # and we want users to be able to prevent all tag generation + # by setting `labels_as_tags` to `[]`, so we need + # a different sentinel to indicate "default" + labels_as_tags = ["unset"] + } + description = <<-EOT + 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. + EOT + + validation { + condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) + error_message = "Allowed values: `lower`, `title`, `upper`." + } + + validation { + condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) + error_message = "Allowed values: `lower`, `title`, `upper`, `none`." + } +} + +variable "enabled" { + type = bool + default = null + description = "Set to false to prevent the module from creating any resources" +} + +variable "namespace" { + type = string + default = null + description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" +} + +variable "tenant" { + type = string + default = null + description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" +} + +variable "environment" { + type = string + default = null + description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" +} + +variable "stage" { + type = string + default = null + description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" +} + +variable "name" { + type = string + default = null + description = <<-EOT + 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. + EOT +} + +variable "delimiter" { + type = string + default = null + description = <<-EOT + Delimiter to be used between ID elements. + Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. + EOT +} + +variable "attributes" { + type = list(string) + default = [] + description = <<-EOT + 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. + EOT +} + +variable "labels_as_tags" { + type = set(string) + default = ["default"] + description = <<-EOT + 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. + EOT +} + +variable "tags" { + type = map(string) + default = {} + description = <<-EOT + Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). + Neither the tag keys nor the tag values will be modified by this module. + EOT +} + +variable "additional_tag_map" { + type = map(string) + default = {} + description = <<-EOT + 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. + EOT +} + +variable "label_order" { + type = list(string) + default = null + description = <<-EOT + 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. + EOT +} + +variable "regex_replace_chars" { + type = string + default = null + description = <<-EOT + 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. + EOT +} + +variable "id_length_limit" { + type = number + default = null + description = <<-EOT + 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`. + EOT + validation { + condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 + error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." + } +} + +variable "label_key_case" { + type = string + default = null + description = <<-EOT + 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`. + EOT + + validation { + condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) + error_message = "Allowed values: `lower`, `title`, `upper`." + } +} + +variable "label_value_case" { + type = string + default = null + description = <<-EOT + 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`. + EOT + + validation { + condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) + error_message = "Allowed values: `lower`, `title`, `upper`, `none`." + } +} + +variable "descriptor_formats" { + type = any + default = {} + description = <<-EOT + 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 + `{ + format = string + labels = list(string) + }` + (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). + EOT +} + +#### End of copy of cloudposse/terraform-null-label/variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..77850b1 --- /dev/null +++ b/main.tf @@ -0,0 +1,47 @@ +locals { + enabled = module.this.enabled +} + +data "aws_region" "current" {} + +module "vpc" { + source = "cloudposse/vpc/aws" + version = "2.0.0" + + ipv4_primary_cidr_block = var.vpc_cidr_block + + context = module.this.context +} + +module "subnets" { + source = "cloudposse/dynamic-subnets/aws" + version = "2.1.0" + + vpc_id = module.vpc.vpc_id + igw_id = [module.vpc.igw_id] + ipv4_cidr_block = var.subnets_ipv4_cidr_block + availability_zones = var.subnets_availability_zones + nat_gateway_enabled = var.subnets_nat_gateway_enabled + + context = module.this.context +} + +module "vpce_s3_label" { + source = "cloudposse/label/null" + version = "0.25.0" + + context = module.this.context + attributes = ["vpce-s3"] +} + +resource "aws_vpc_endpoint" "s3" { + count = var.s3_endpoint_enabled && local.enabled ? 1 : 0 + + vpc_id = module.vpc.vpc_id + + service_name = "com.amazonaws.${data.aws_region.current.name}.s3" + vpc_endpoint_type = "Gateway" + route_table_ids = module.subnets.private_route_table_ids + + tags = module.vpce_s3_label.tags +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..d0df7ca --- /dev/null +++ b/outputs.tf @@ -0,0 +1,19 @@ +output "vpc_id" { + description = "The ID of the VPC." + value = module.vpc.vpc_id +} + +output "public_subnet_ids" { + description = "IDs of the created public subnets." + value = module.subnets.public_subnet_ids +} + +output "private_subnet_ids" { + description = "IDs of the created private subnets." + value = module.subnets.private_subnet_ids +} + +output "s3_endpoint_arn" { + description = "ARN of the S3 endpoint." + value = join("", aws_vpc_endpoint.s3.*.arn) +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..158290c --- /dev/null +++ b/variables.tf @@ -0,0 +1,29 @@ +variable "vpc_cidr_block" { + type = string + default = "10.0.0.0/16" + description = "CIDR Block for the VPC." +} + +variable "subnets_ipv4_cidr_block" { + type = list(string) + default = ["10.0.0.0/16"] + description = "IPv4 CIDR Block for the subnets." +} + +variable "subnets_availability_zones" { + type = list(string) + default = ["us-east-1a", "us-east-1b"] + description = "Availability zones for the subnets." +} + +variable "subnets_nat_gateway_enabled" { + type = bool + default = false + description = "Set to `true` to create a nat gateway in each subnet." +} + +variable "s3_endpoint_enabled" { + type = bool + default = false + description = "Set to `true` to create a VPC gateway endpoint for S3." +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..230c8af --- /dev/null +++ b/versions.tf @@ -0,0 +1,18 @@ +terraform { + required_version = ">= 0.13.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 3.71" + } + null = { + source = "hashicorp/null" + version = ">= 2.0" + } + template = { + source = "hashicorp/template" + version = ">= 2.0" + } + } +}