Skip to content

Commit

Permalink
Terraform for EC2 Tagging (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcarranza authored Jun 21, 2023
1 parent 5917c17 commit 0dbd79d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
40 changes: 40 additions & 0 deletions terraform-modules/aws/ec2-tag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## EC2 Tagging
- It collects information about running instances in the AWS (Amazon Web Services) cloud.
- It retrieves the identity of the AWS account that is executing the code.
- It creates a local variable called "instance_tags" that contains information about the instances and their associated tags.
- It applies the AWS EC2 tag to each instance based on the collected information.

## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_ec2_tag.tag_instances](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_tag) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_instances.existing_instances](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/instances) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_account_tags"></a> [account\_tags](#input\_account\_tags) | Tags for each AWS account.<br><br>This variable allows you to provide tags for different AWS accounts using a map structure. Each AWS account is identified by its unique account ID, and you can specify multiple tags for each account using key-value pairs.<br><br>Example Usage:<br><br>inputs:<br> {<br> "account\_id\_1" = {<br> "key1" = "value1"<br> "key2" = "value2"<br> "key3" = "value3"<br> "key4" = "value4"<br> }<br> "account\_id\_1" = {<br> "key1" = "value1"<br> "key2" = "value2"<br> "key3" = "value3"<br> "key4" = "value4"<br> }<br> ... (Add more AWS account tags here) ...<br> } | `map(map(string))` | `{}` | no |
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region | `string` | `"us-west-2"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_instance_tags"></a> [instance\_tags](#output\_instance\_tags) | n/a |
25 changes: 25 additions & 0 deletions terraform-modules/aws/ec2-tag/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
data "aws_instances" "existing_instances" {
instance_state_names = ["running"]
}

data "aws_caller_identity" "current" {}

locals {
instance_tags = flatten([
for ec2_id in data.aws_instances.existing_instances.ids : [
for key, value in var.account_tags[data.aws_caller_identity.current.account_id] : {
resource_id = ec2_id
key = key
value = value
}
]
])
}

resource "aws_ec2_tag" "tag_instances" {
for_each = { for idx, tag in local.instance_tags : idx => tag }

resource_id = each.value.resource_id
key = each.value.key
value = each.value.value
}
3 changes: 3 additions & 0 deletions terraform-modules/aws/ec2-tag/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "instance_tags" {
value = var.account_tags[data.aws_caller_identity.current.account_id]
}
34 changes: 34 additions & 0 deletions terraform-modules/aws/ec2-tag/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "account_tags" {
description = <<-EOF
Tags for each AWS account.
This variable allows you to provide tags for different AWS accounts using a map structure. Each AWS account is identified by its unique account ID, and you can specify multiple tags for each account using key-value pairs.
Example Usage:
inputs:
{
"account_id_1" = {
"key1" = "value1"
"key2" = "value2"
"key3" = "value3"
"key4" = "value4"
}
"account_id_1" = {
"key1" = "value1"
"key2" = "value2"
"key3" = "value3"
"key4" = "value4"
}
... (Add more AWS account tags here) ...
}
EOF
type = map(map(string))
default = {}
}

variable "aws_region" {
description = "AWS region"
type = string
default = "us-west-2"
}

0 comments on commit 0dbd79d

Please sign in to comment.