From d83a91f11d032242f5f9abf1b2366b607a7fc0d6 Mon Sep 17 00:00:00 2001 From: Nikita Dugar Date: Mon, 14 Oct 2019 10:53:16 +0530 Subject: [PATCH] add dynamic tags --- README.md | 29 +++++++++++++++++---------- README.yaml | 21 ++++++++++--------- _example/.terraform.tfstate.lock.info | 1 + _example/example.tf | 3 ++- main.tf | 3 ++- variables.tf | 6 ++++++ 6 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 _example/.terraform.tfstate.lock.info diff --git a/README.md b/README.md index 0aa8330..7cf10c4 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,10 @@ This module has a few dependencies: + ## Examples + **IMPORTANT:** Since the `master` branch used in `source` varies based on new modifications, we suggest that you use the release versions [here](https://github.com/clouddrove/terraform-aws-ec2/releases). @@ -70,11 +72,11 @@ This module has a few dependencies: Here is an example of how you can use this module in your inventory structure: ```hcl module "ec2" { - source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.1" - name = "ec2-instance" - application = "clouddrove" - environment = "test" - label_order = ["environment", "name", "application"] + source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2" + name = "ec2-instance" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] instance_count = 2 ami = "ami-08d658f84a6d84a80" ebs_optimized = false @@ -86,16 +88,20 @@ Here is an example of how you can use this module in your inventory structure: disk_size = 8 vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids] subnet_ids = tolist(module.public_subnets.public_subnet_id) - assign_eip_address = true - ebs_volume_enabled = true - ebs_volume_type = "gp2" - ebs_volume_size = 30 - user_data = "./_bin/user_data.sh" + assign_eip_address = true + ebs_volume_enabled = true + ebs_volume_type = "gp2" + ebs_volume_size = 30 + user_data = "./_bin/user_data.sh" + instance_tags = {"snapshot"=true} } ``` + + + ## Inputs | Name | Description | Type | Default | Required | @@ -125,6 +131,7 @@ Here is an example of how you can use this module in your inventory structure: | instance_count | Number of instances to launch. | number | `1` | no | | instance_enabled | Flag to control the instance creation. | bool | `true` | no | | instance_initiated_shutdown_behavior | Shutdown behavior for the instance. | string | `` | no | +| instance_tags | Instance tags. | map | `` | no | | instance_type | The type of instance to start. Updates to this field will trigger a stop/start of the EC2 instance. | string | - | yes | | ipv6_address_count | Number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. | number | `0` | no | | ipv6_addresses | List of IPv6 addresses from the range of the subnet to associate with the primary network interface. | list | `` | no | @@ -161,8 +168,8 @@ Here is an example of how you can use this module in your inventory structure: -## Testing +## Testing In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system. You need to run the following command in the testing folder: diff --git a/README.yaml b/README.yaml index 49c3cf7..37898af 100644 --- a/README.yaml +++ b/README.yaml @@ -36,11 +36,11 @@ usage : |- Here is an example of how you can use this module in your inventory structure: ```hcl module "ec2" { - source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.1" - name = "ec2-instance" - application = "clouddrove" - environment = "test" - label_order = ["environment", "name", "application"] + source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2" + name = "ec2-instance" + application = "clouddrove" + environment = "test" + label_order = ["environment", "name", "application"] instance_count = 2 ami = "ami-08d658f84a6d84a80" ebs_optimized = false @@ -52,10 +52,11 @@ usage : |- disk_size = 8 vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids] subnet_ids = tolist(module.public_subnets.public_subnet_id) - assign_eip_address = true - ebs_volume_enabled = true - ebs_volume_type = "gp2" - ebs_volume_size = 30 - user_data = "./_bin/user_data.sh" + assign_eip_address = true + ebs_volume_enabled = true + ebs_volume_type = "gp2" + ebs_volume_size = 30 + user_data = "./_bin/user_data.sh" + instance_tags = {"snapshot"=true} } ``` \ No newline at end of file diff --git a/_example/.terraform.tfstate.lock.info b/_example/.terraform.tfstate.lock.info new file mode 100644 index 0000000..9957a73 --- /dev/null +++ b/_example/.terraform.tfstate.lock.info @@ -0,0 +1 @@ +{"ID":"47e23b37-aedf-eba4-4409-90e7f092db02","Operation":"OperationTypeApply","Info":"","Who":"nikita@nikita","Version":"0.12.9","Created":"2019-10-14T05:19:38.991300251Z","Path":"terraform.tfstate"} \ No newline at end of file diff --git a/_example/example.tf b/_example/example.tf index d90f8bf..857b38e 100644 --- a/_example/example.tf +++ b/_example/example.tf @@ -63,7 +63,7 @@ module "ssh" { } module "ec2" { - source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.1" + source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2" name = "ec2-instance" application = "clouddrove" @@ -88,4 +88,5 @@ module "ec2" { ebs_volume_type = "gp2" ebs_volume_size = 30 user_data = "./_bin/user_data.sh" + instance_tags = { "snapshot" = true } } \ No newline at end of file diff --git a/main.tf b/main.tf index f8cf45e..71e7df8 100644 --- a/main.tf +++ b/main.tf @@ -57,7 +57,8 @@ resource "aws_instance" "default" { { "Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index + 1)) - } + }, + var.instance_tags ) volume_tags = merge( diff --git a/variables.tf b/variables.tf index 24f8072..4ea4bc1 100644 --- a/variables.tf +++ b/variables.tf @@ -250,4 +250,10 @@ variable "cpu_credits" { type = string default = "standard" description = "The credit option for CPU usage. Can be `standard` or `unlimited`. T3 instances are launched as unlimited by default. T2 instances are launched as standard by default." +} + +variable "instance_tags" { + type = map + default = {} + description = "Instance tags." } \ No newline at end of file