Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-ordering tag merging #148

Merged
merged 2 commits into from
Sep 18, 2018
Merged

Re-ordering tag merging #148

merged 2 commits into from
Sep 18, 2018

Conversation

sc250024
Copy link
Contributor

@sc250024 sc250024 commented Jun 30, 2018

Description

Re-orders the tag merging to be consistent with Terraform's map merging strategy, and provides the user greater control over specific resource tagging when tags are inherited from upstream.

Motivation and Context

Short version

The merging strategy of merging maps for a given object should always follow the pattern:

tags = "${merge(map(default_map, least_specific_map, ..., ..., most_specific_map)}"

This is also documented on the Terraform Interpolation page:

Returns the union of 2 or more maps. The maps are consumed in the order provided, and duplicate keys overwrite previous entries.

This PR changes the tag merging strategy to honor that.

Long version

I use CloudPosse's terraform-null-label module to enforce standard naming within my entire Terraform infrastructure. Using a module block like the following:

module "vpc_label" {
  delimiter = "-"
  name      = "vpc"
  namespace = "eu-west-1"
  source    = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
  stage     = "prod"
  tags      = "${map("LastModified", "${timestamp()}")}"
}

I can ensure that not only are tags created in a standard way, but that more complicated tagging schemes (like for auto-scaling groups) are already created for me. However, I discovered a conflict with the way that this VPC module handles merging of tags for resources.

The problem is that the terraform-null-label module above automatically creates a "Name" tag that I cannot drop. For that labeling module, the output ${module.vpc_label.tags} will return a map of tags it creates. The problem is with the way that this VPC module is currently written, if I set ...

module "vpc" {
  ...
  source  = "terraform-aws-modules/vpc/aws"
  tags    = "${module.vpc_label.tags}"
  version = "1.37.0"
}

...and then run a terraform apply, all of my Name tags look like the following:

  ~ module.vpc.aws_subnet.private[2]
      tags.LastModified:     "2018-06-30T20:50:31Z" => "2018-06-30T21:26:01Z"
      tags.Name:             "pb-euw1-prod-vpc-private-eu-west-1c" => "pb-euw1-prod-vpc"

  ~ module.vpc.aws_subnet.public[0]
      tags.LastModified:     "2018-06-30T20:50:31Z" => "2018-06-30T21:26:01Z"
      tags.Name:             "pb-euw1-prod-vpc-public-eu-west-1a" => "pb-euw1-prod-vpc"

  ~ module.vpc.aws_subnet.redshift[0]
      tags.LastModified:     "2018-06-30T20:50:31Z" => "2018-06-30T21:26:01Z"
      tags.Name:             "pb-euw1-prod-vpc-redshift-eu-west-1a" => "pb-euw1-prod-vpc"

Even if I specifically try and reset the Name tag using the <resource>_tags inputs available like so:

private_subnet_tags {
  "Name" = "some-random-name"
}

The tag still is not honored for the private_subnet resources, in this case. In general, the merging strategy of merging maps for a given object should always follow the pattern:

tags = "${merge(map(default_map, least_specific_map, ..., ..., most_specific_map)}"

How Has This Been Tested?

Ran terraform apply on the following modification of the Simple VPC example code:

provider "aws" {
  region = "eu-west-1"
}

module "vpc" {
  source = "../../"

  name = "simple-example"

  cidr = "10.0.0.0/16"

  azs             = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
  single_nat_gateway = true

  public_subnet_tags = {
    Name = "overriden-name-public"
  }

  tags = {
    Owner       = "user"
    Environment = "dev"
    Name        = "default-name-public"
  }

  vpc_tags = {
    Name = "vpc-name"
  }
}

The output shows that the Name tag specified in the public_subnet_tags block is honored even though there is already a Name tag in the tags block:

  + module.vpc.aws_subnet.public[0]
      id:                               <computed>
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "eu-west-1a"
      cidr_block:                       "10.0.101.0/24"
      ipv6_cidr_block:                  <computed>
      ipv6_cidr_block_association_id:   <computed>
      map_public_ip_on_launch:          "true"
      tags.%:                           "3"
      tags.Environment:                 "dev"
      tags.Name:                        "overriden-name-public"
      tags.Owner:                       "user"
      vpc_id:                           "${aws_vpc.this.id}"

  + module.vpc.aws_subnet.public[1]
      id:                               <computed>
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "eu-west-1b"
      cidr_block:                       "10.0.102.0/24"
      ipv6_cidr_block:                  <computed>
      ipv6_cidr_block_association_id:   <computed>
      map_public_ip_on_launch:          "true"
      tags.%:                           "3"
      tags.Environment:                 "dev"
      tags.Name:                        "overriden-name-public"
      tags.Owner:                       "user"
      vpc_id:                           "${aws_vpc.this.id}"

  + module.vpc.aws_subnet.public[2]
      id:                               <computed>
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "eu-west-1c"
      cidr_block:                       "10.0.103.0/24"
      ipv6_cidr_block:                  <computed>
      ipv6_cidr_block_association_id:   <computed>
      map_public_ip_on_launch:          "true"
      tags.%:                           "3"
      tags.Environment:                 "dev"
      tags.Name:                        "overriden-name-public"
      tags.Owner:                       "user"
      vpc_id:                           "${aws_vpc.this.id}"

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@sc250024
Copy link
Contributor Author

sc250024 commented Jul 5, 2018

@antonbabenko Thoughts? This one's fairly straightforward. Mostly looking to hear the reasons for keeping it the way it is.

Thank you!

@sc250024
Copy link
Contributor Author

@antonbabenko I just fixed the merge conflicts. Seems like this one should be pretty straightforward?

@antonbabenko
Copy link
Member

Yes, this seems rather straightforward, but still required some verification. Thank you for fixing this (and a nudge) :)

@antonbabenko antonbabenko merged commit 68e84ef into terraform-aws-modules:master Sep 18, 2018
@antonbabenko
Copy link
Member

v1.44.0 has been released

@sc250024
Copy link
Contributor Author

Thank you @antonbabenko !

guneriu added a commit to Yilu-Archive/terraform-aws-vpc that referenced this pull request May 14, 2021
* Fixing typo overriden -> overridden (terraform-aws-modules#150)

just a typo in the docs and in the
public_subnet_tags in the simple example

* Provide separate route tables for db/elasticache/redshift (terraform-aws-modules#155)

* Provide separate route tables for db/elasticache/redshift

* Added example for saperate routes

* Updated PR with suggestions

* Make redshift to use separate subnet route table also

* More cleanup and updates

* Fixed one more spelling mistake

* Add minimum support for IPv6 to VPC (terraform-aws-modules#156)

* Added support for IPv6 to VPC

* Removed IPv6 from outputs (fixed terraform-aws-modules#157) (terraform-aws-modules#158)

* Add secondary CIDR block support (terraform-aws-modules#163)

* Add secondary CIDR block support using a local variable to derive the vpc id to ensure the CIDR block operations are applied before the CIDR operations

* Add secondary cidr block outputs to module output

* Add the wonderful examples from matthiasr's PR located at terraform-aws-modules#162 all credit goes to them for this wonderful example

* From copy and paste accidentally used variable name that differed from these variables

* Resolve typo in secondary_cidr_blocks

* Fixed README formatting

* Followups for terraform-aws-modules#161

* Added local.vpc_id with description

* add vars for custom subnet and route table names (terraform-aws-modules#168)

* add vars for custom subnet and route table names

* revert db suffix to "db"

* Added cloudcraft.co as a sponsor for this module

* Added cloudcraft.co as a sponsor for this module

* Removed comments starting from # to fix README

* Updated link to cloudcraft

* Updated link to cloudcraft

* Reordering tag merging (terraform-aws-modules#148)

* Added amazon_side_asn to vpn_gateway (terraform-aws-modules#159)

* Added amazon_side_asn to vpn_gateway

* change to Amazon default ASN (as per API) (terraform-aws-modules#176)

https://docs.aws.amazon.com/cli/latest/reference/ec2/create-vpn-gateway.html

* Updated README.md after merge

* Fixed terraform-aws-modules#177 - public_subnets should not always be validated

* Fix for the error: module.vpc.aws_redshift_subnet_group.redshift: only lowercase alphanumeric characters and hyphens allowed in name

Read more: terraform-aws-modules#180

* Updated pre-commit version with new terraform-docs script

* Added IGW route for DB subnets (based on terraform-aws-modules#179)

* Reverted complete-example

* Added azs to outputs which is an argument

* Added possibility to control creation of elasticache and redshift subnet groups

* Added SSM and EC2 VPC endpoints (fixes terraform-aws-modules#195, terraform-aws-modules#194)

* adding option to create a route to nat gateway in database subnets

* Reordered vars in count for database_nat_gateway route

* add endpoints ec2messages, ssmmessages as those are required by Systems Manager in addition to ec2 and ssm.

* fix typo

* add additional endpoints to examples

* add files updated by pre-commit

* switch to terraform-docs v0.6.0

* Added option to create ECR api and dkr endpoints

* Added subnet ids to ecr endpoints

* Fixed formatting after terraform-aws-modules#205

* Fixed formatting after terraform-aws-modules#213

* Added intra subnet suffix. (terraform-aws-modules#220)

* Added intra subnet suffix.

* Fixed duplicate intra

* Fixed tag

* Added CHANGELOG.md (terraform-aws-modules#221)

* Bump version

* API gateway Endpoint (terraform-aws-modules#225)

* Updated changelog

* docs: Update comment in docs (terraform-aws-modules#226)

* Redshift public subnets (terraform-aws-modules#222)

* add public subnet for redshift to enable access for kinesis

* fix redshift subnet group name

* fix redshift public association

* add public redshift to documentation

* fix doc typo

* update code after review

* Redshift public subnets (terraform-aws-modules#222)

* Resolved conflicts after merge

* Updated changelog

* Network ACLs (terraform-aws-modules#238)

* Add variables for network ACLs

Add variables for specifying network ACLs for public, private, and
intra subnets. The ACLs are defined in a list, with sets of seven
elements for the rule number, rule action, from port, to port,
protocol, and cidr block.

* Add variables for network ACL tags

Add variables to specify additional tags for public, private, and intra
network ACL resources.

* Add resources for network ACLs

Add aws_network_acl and aws_network_acl_rule resources to specify
inbound and outbound network ACL rules for public, private, and intra
subnets.

* Add resource for default network ACL

Add a aws_default_network_acl resource to adopt the default network ACL
in the VPC.

* Adjust spacing to match code style

Remove the empty lines after comment blocks for network ACLs to match
the style of the rest of this module.

* Copy simple-vpc example as network-acls

Copy the simple-vpc example and adapt it to demonstrate the
configuration of network ACLs. A set of inbound and outbound ACLs are
specified in main.tf.

* Rename variables from _acls to _acl_rules

Clarify the variables for specifying ACL rules by renaming them from
*_acls to *_acl_rules. The values are used to create rules, not create
ACLs.

* Add nacl resources and variables for other subnets

Add aws_network_acl and aws_network_acl_rule resources for database,
redshift, and elasticache subnets, along with corresponding variables.
This provides network ACL coverage to all subnet types produced by this
module.

* Create ACLs only if there are subnets

For each subnet type, only create ACL resources if there are subnets
defined. For example, if database_subnets is empty, then don't create
ACL resources for database subnets.

* Add missing variables for ACL tags

Add the missing variable declarations for database_acl_tags,
redshift_acl_tags, and elasticache_acl_tags.

* Make ACL singular in description for _acl_tags

A single ACL is created for each of the subnet types. Update the
variable descriptions to reflect this.

* Convert rules to nested list of maps

Convert the NACL rule specifications from a list of lists to a list of
maps, as suggested by @jczerniak. This improves the readability of
rules.

* Restructure example config to use locals

Restructure the network ACL rules in the network-acls example to use
local variables to specify the rules, split between default and custom
rules.

* Follow-up for terraform-aws-modules#174

* Updated CHANGELOG

* Added missing VPC endpoints outputs (resolves terraform-aws-modules#246) (terraform-aws-modules#247)

* Updated CHANGELOG

* Add support for KMS VPC endpoint creation (terraform-aws-modules#243)

* Updated CHANGELOG

* Added ARN of VPC in module output (terraform-aws-modules#245)

I need in my policy generator the arn of vpc so I would like to include this

* Fixed formatting

* Updated CHANGELOG

* Add Output Of Subnet ARNs (terraform-aws-modules#242)

* Add Output Of Subnet ARNs

Facilitates resource access manager, subnet sharing across accounts

* Update Readme For Subnet ARN Output

* Fixed formatting

* Updated CHANGELOG

* Improving DHCP options docs (terraform-aws-modules#260)

* Improving DHCP options docs

* generating README from variables description

* Updated CHANGELOG

* ECS endpoint (terraform-aws-modules#261)

* add ecs vpc endpoints

* add ecs vpcendpoints outputs

* add ecs vpc endpoints to readme inputs/outputs table

* add ecs vpc endpoints to readme endpoint list

* Added VPC endpoints for SQS (closes terraform-aws-modules#248)

* Updated CHANGELOG

* Finally, Terraform 0.12 support (terraform-aws-modules#266)

* run terraform 0.12upgrade

* Cleanup for Terraform 0.12 (closes terraform-aws-modules#265, terraform-aws-modules#228)

* Fixed merge conflicts

* Updated CHANGELOG

* Upgrade Docker Image to fix CI (terraform-aws-modules#270)

* Added VPC Endpoints for SNS, Cloudtrail, ELB, Cloudwatch (terraform-aws-modules#269)

* Updated CHANGELOG

* Updated Terraform versions in README

* Updated CHANGELOG

* Fixed opportunity to create the vpc, vpn gateway routes (bug during upgrade to 0.12)

* Updated CHANGELOG

* Fixed broken 2.3.0

* Updated CHANGELOG

* Updated CHANGELOG

* Update tflint to 0.8.2 for circleci task (terraform-aws-modules#280)

* Updated VPC endpoint example (fixed terraform-aws-modules#249)

* Updated CHANGELOG

* Updated pre-commit-terraform to support terraform-docs and Terraform 0.12 (terraform-aws-modules#288)

* Updated CHANGELOG

* Enable backwards compatibility

* KAN-380 terraform 0.12 upgrade

* enable backwards compatibility

Co-authored-by: Tristan Escalada <tristan@escalada.us>
Co-authored-by: Anton Babenko <anton@antonbabenko.com>
Co-authored-by: Rupert Broad <rupert.broad@exact.com>
Co-authored-by: Scott Crooks <sc250024@users.noreply.github.com>
Co-authored-by: Mayur Nagekar <mayur@meetbeam.com>
Co-authored-by: ebarault <eric.barault@gmail.com>
Co-authored-by: tbugfinder <github@online.ms>
Co-authored-by: Michiel Dhadamus <michiel.dhadamus@dataminded.be>
Co-authored-by: Kinnaird McQuade <kmcquade@users.noreply.github.com>
Co-authored-by: tharun-allu <tharun-allu@users.noreply.github.com>
Co-authored-by: Kyle <1kylecameron@gmail.com>
Co-authored-by: bmihaescu <bmihaescu@gmail.com>
Co-authored-by: Nikos Loutas <nloutas@gmail.com>
Co-authored-by: Rafael Bernardo <rafaelbernardo@protonmail.com>
Co-authored-by: Blaine Schanfeldt <git@blaines.me>
Co-authored-by: Andreas Wittig <andreas@widdix.de>
Co-authored-by: Ilia Lazebnik <Ilia.lazebnik@gmail.com>
Co-authored-by: Niklas Wagner <Skaro@Skaronator.com>
Co-authored-by: Sebastian Geidies <sebastian.geidies@bcgdv.com>
Co-authored-by: ugur.guneri <ugur.guneri@yiluhub.com>
@github-actions
Copy link

github-actions bot commented Nov 5, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants