ManagedKube / kubernetes-ops

Running Kubernetes in production
Apache License 2.0
297 stars 136 forks source link

Terraform for EC2 Tagging #439

Closed bcarranza closed 1 year ago

bcarranza commented 1 year ago

EC2 Tagging

bcarranza commented 1 year ago

This is to tag existing EKS and Ec2 instances

Hi @oluojolola Here is a pull requested created by me due you don't have access to create pull request here. @sekka1 could you help us to add @oluojolola grants to create pull request in this repo?.

I understand that the first application will be in development so there is no problem in applying your changes, this is achieved by commenting this line in the pipeline!! https://github.com/exact-payments/gruntwork-infrastructure-live/blob/0816f9752557912d98b5b9850aafddf2fc4754b7/.github/workflows/terraform-pipeline-p2-dev.yaml#L214

bcarranza commented 1 year ago

@oluojolola We need to get @sekka1's approval, he needs some evidence to achieve the approval, I will create some post in order to you can quote reply every one of them!!

bcarranza commented 1 year ago

@oluojolola Add in the readme file a description , why we should use this solution? more human stuff!!!!

bcarranza commented 1 year ago

@oluojolola Could you add where we are using this branch in gruntwork-infrastructure live project!!?

bcarranza commented 1 year ago

@oluojolola could you add evidence about which are your inputs values to this solution? you can paste here github link whit that portion of code?

bcarranza commented 1 year ago

@oluojolola could you add terragrunt apply log evidence!!?

bcarranza commented 1 year ago

@oluojolola could you posted here the aws console evidence about the outcome to apply this solution? (pictures)

oluojolola commented 1 year ago

@oluojolola We need to get @sekka1's approval, he needs some evidence to achieve the approval, I will create some post in order to you can quote reply every one of them!!

This code is used to tag existing EC2 instances in Exactpay AWS accounts based on a given set of tags to be fed into the Cost Mnagement Dashboards

Below is the breakdown of each subset of the code

Data Blocks:

data "aws_instances" "existing_instances": This data block retrieves information about existing EC2 instances. It specifies a filter for instance_state_names to only include instances in the "running" state.

data "aws_caller_identity" "current": This data block retrieves information about the AWS caller identity, which includes details about the AWS account being used for deployment.

Resource Block:

resource "aws_ec2_tag" "tag_existing_instances": This resource block creates tags for existing EC2 instances. It makes us of dynamic block to iterate/loop over each existing instance's ID and create tags for those instances.

Dynamic Block:

dynamic "tag": This dynamic block creates tags dynamically based on the values provided in the var.account_tags variable. It iterates over the tags specified for the current AWS account.

for_each = var.account_tags[data.aws_caller_identity.current.account_id]: This expression iterates over the tags specified for the current AWS account by using the account_id from the caller identity data block as the key to retrieve the tags from the var.account_tags variable.

content: This block defines the content of each dynamically created tag. It includes the key and value attributes based on the tag.key and tag.value values obtained from the iteration.

In Conclusion, this code retrieves a list of existing EC2 instances that are in the "running" state in the current AWS accounts and creates tags dynamically for each existing instance based on the specified tags for the current AWS account. @bcarranza

oluojolola commented 1 year ago

@oluojolola Could you add where we are using this branch in gruntwork-infrastructure live project!!?

This will be deployed in All Exactpay AWS Accounts that run their workloads. The first Iteration will be in the dev/p2 account shown below @bcarranza https://github.com/exact-payments/gruntwork-infrastructure-live/tree/master/dev/us-west-2/dev/p2a/ec2-tags

oluojolola commented 1 year ago

@oluojolola could you add terragrunt apply log evidence!!?

Below is the evidence of the Terragrunt apply as requested @bcarranza

Screen Shot 2023-06-20 at 15 01 10
oluojolola commented 1 year ago

@oluojolola could you add evidence about which are your inputs values to this solution? you can paste here github link whit that portion of code?

Below Link shows the Input values that is being used by Exact for tagging existing resources in various accounts @bcarranza https://github.com/exact-payments/gruntwork-infrastructure-live/blob/d690a0da719425238de6a7c257ee3fffe72932b6/dev/us-west-2/dev/p2a/EC2-EKS_Tags/terragrunt.hcl#LL63C3-L63C3

oluojolola commented 1 year ago

@oluojolola could you posted here the aws console evidence about the outcome to apply this solution? (pictures)

find below evidence of tags applied in Dev Exactpay account @bcarranza image (3) image (3)