{"id":1086,"date":"2023-08-28T15:29:21","date_gmt":"2023-08-28T15:29:21","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1086"},"modified":"2023-08-28T15:29:21","modified_gmt":"2023-08-28T15:29:21","slug":"aws-tag-policy-terraform","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1086","title":{"rendered":"AWS Tag Policy Configuration Using Terraform"},"content":{"rendered":"<p>In this blog, you will learn to configure <a href=\"https:\/\/devopscube.com\/aws-tag-policy\/\">AWS Tag Policies<\/a> using Terraform. I have added step-by-step guides for the whole setup.<\/p>\n<h2 id=\"terraform-tag-policy-deployment-workflow\">Terraform Tag Policy Deployment Workflow<\/h2>\n<p>Here is the workflow explanation<\/p>\n<ol>\n<li>Download the Terraform code from the GitHub repository into your system where you have installed Terraform and AWS CLI.<\/li>\n<li>Run the specific script which contains <code>tag-policy<\/code> module by passing proper variables in the variable file.<\/li>\n<li>Terraform will execute the <code>tag-policy<\/code> module to create a policy to enforce compliance for specific resources such as EC2 Instances, Security Groups, AWS Lambda, etc.<\/li>\n<li>The newly created policy will be associated with the account that we have mentioned in the variables file.<\/li>\n<li>The policy ID will be shown in the output as part of the Terraform configuration.<\/li>\n<li>Terraform will automatically save the state file in the S3 bucket in your AWS account.<\/li>\n<\/ol>\n<h2 id=\"terraform-tag-policy-code-repository\"><strong>Terraform Tag Policy Code Repository<\/strong><\/h2>\n<p>The Tag Policy Terraform code is included in the <a href=\"https:\/\/github.com\/techiescamp\/terraform-aws.git?ref=devopscube.com\">Terraform AWS<\/a> repository. To follow the guide, clone the repository to your workstation.<\/p>\n<pre><code>git clone https:\/\/github.com\/techiescamp\/terraform-aws.git<\/code><\/pre>\n<p>Fork and clone the repository if you intend to reuse and make changes as per your requirements.<\/p>\n<h2 id=\"terraform-aws-organization-s-tag-policy-provisioning-workflow\"><strong>Terraform AWS Organization\u2019s Tag Policy Provisioning Workflow<\/strong><\/h2>\n<p>The Terraform script for Tag Policy is structured in the following way.<\/p>\n<pre><code>\u251c\u2500\u2500infra\n\u2502   \u251c\u2500\u2500 tag-policy\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n|   |   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u251c\u2500\u2500modules\n\u2502   \u251c\u2500\u2500 tag-policy\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n\u2502   \u2502   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u2514\u2500\u2500vars\n    \u2514\u2500\u2500 dev\n        \u2514\u2500\u2500 tag-policy.tfvars\n<\/code><\/pre>\n<p><strong><code>vars<\/code><\/strong> folder contains the variables file named <code>tag-policy.tfvars<\/code><\/p>\n<p><strong>infra<\/strong>\/<strong>tag-policy<\/strong> folder contains the terraform code (<code>main.tf<\/code>) that calls the Tag Policy module from the modules directory.<\/p>\n<p>The modules directory contains the following resources:<\/p>\n<p><strong><code>tag-policy<\/code> Module<\/strong>: The <code>tag-policy<\/code> module contains an AWS Organizations policy creation block with JSON code to create tags with specified tag keys, tag values, and enforcement for values.<\/p>\n<p>The policy attachment block is used to attach the created policy to a related account using the account ID.<\/p>\n<h2 id=\"implementing-tag-policy-using-terraform\">Implementing Tag Policy Using Terraform<\/h2>\n<p>Follow the steps provided in this section to implement a tag policy in an AWS account.<\/p>\n<p>Assuming the &#8220;<strong>terraform-aws<\/strong>&#8221; folder as the root directory for this guide.<\/p>\n<h3 id=\"step-1-adjust-the-variables-for-the-tag-policy-module-\"><strong>Step 1:<\/strong> Adjust the variables for the tag-policy module.<\/h3>\n<p>Open the <code>vars\/dev\/tag-policy.tfvars<\/code> file and edit the variables based on your requirements. Primarily, you may want to change <code>target_id<\/code> and <code>enforce_for_values<\/code>.<\/p>\n<p>Additionally, you can change <code>tag_value<\/code> and <code>region<\/code> if you want.<\/p>\n<pre><code># Tag Policy Vars\nregion      = \"eu-north-1\"\npolicy_name = \"Techiescamp\"\npolicy_type = \"TAG_POLICY\"\ntarget_id   = \"814200988517\"\n\nname_tag_key         = \"Name\"\nenvironment_tag_key  = \"Environment\"\nowner_tag_key        = \"Owner\"\nowner_tag_value      = [\"techiescamp\"]\ncostcenter_tag_key   = \"CostCenter\"\ncostcenter_tag_value = [\"techiescamp-commerce\"]\napplication_tag_key  = \"Application\"\nenforce_for_values   = [\"dynamodb:*\", \"ec2:dhcp-options\", \"ec2:elastic-ip\", \"ec2:fpga-image\", \"ec2:instance\",\n                        \"ec2:internet-gateway\", \"ec2:launch-template\", \"ec2:natgateway\", \"ec2:network-acl\",\n                        \"ec2:network-interface\", \"ec2:route-table\", \"ec2:security-group\", \"ec2:snapshot\",\n                        \"ec2:subnet\", \"ec2:volume\", \"ec2:vpc\", \"ec2:vpc-endpoint\", \"ec2:vpc-endpoint-service\",\n                        \"ec2:vpc-peering-connection\", \"ec2:vpn-connection\", \"ec2:vpn-gateway\", \"elasticfilesystem:*\",\n                        \"elasticloadbalancing:*\", \"iam:instance-profile\", \"iam:mfa\", \"iam:policy\", \"kms:*\",\n                        \"lambda:*\", \"rds:cluster-pg\", \"rds:cluster-endpoint\", \"rds:es\", \"rds:og\", \"rds:pg\", \"rds:db-proxy\",\n                        \"rds:db-proxy-endpoint\", \"rds:ri\", \"rds:secgrp\", \"rds:subgrp\", \"rds:target-group\", \"resource-groups:*\",\n                        \"route53:hostedzone\", \"s3:bucket\", \"s3:bucket\"]                \n\n<\/code><\/pre>\n<h3 id=\"step-2-initialize-terraform-configuration-\"><strong>Step 2:<\/strong> Initialize Terraform Configuration:<\/h3>\n<p>Once the modifications are completed, save the code.<\/p>\n<p>Open the parent directory.<\/p>\n<pre><code>cd infra\/tag-policy<\/code><\/pre>\n<p>Inside the &#8220;<strong>tag-policy<\/strong>&#8221; folder, you will find the &#8220;<strong>main.tf<\/strong>&#8221; file, which contains the configurations of the <code>tag-policy<\/code> module.<\/p>\n<pre><code>provider \"aws\" {\n  region = var.region\n}\n\nmodule \"tag-policy\" {\n  source      = \"..\/..\/modules\/tag-policy\"\n  region      = var.region\n  policy_name = var.policy_name\n  policy_type = var.policy_type\n  target_id   = var.target_id\n\n  name_tag_key         = var.name_tag_key\n  environment_tag_key  = var.environment_tag_key\n  owner_tag_key        = var.owner_tag_key\n  owner_tag_value      = var.owner_tag_value\n  costcenter_tag_key   = var.costcenter_tag_key\n  costcenter_tag_value = var.costcenter_tag_value\n  application_tag_key  = var.application_tag_key\n  enforce_for_values   = var.enforce_for_values\n}<\/code><\/pre>\n<h3 id=\"step-3-verify-terraform-plan\"><strong>Step 3<\/strong>: Verify Terraform Plan<\/h3>\n<p>Assuming you are in the <code>infra\/tag-policy<\/code> directory, and to preview the changes, use the Terraform plan command.<\/p>\n<pre><code>terraform plan -var-file=..\/..\/vars\/dev\/tag-polcy.tfvars<\/code><\/pre>\n<h3 id=\"step-4-apply-terraform-changes\"><strong>Step <\/strong>4: Apply Terraform Changes<\/h3>\n<p>Apply the Terraform configuration to create the AWS resources by using the following command.<\/p>\n<pre><code>terraform apply -var-file=..\/..\/vars\/dev\/tag-policy.tfvars<\/code><\/pre>\n<p>Confirm the action by typing &#8220;yes&#8221; when prompted.<\/p>\n<p>Terraform will now implement the tag policy based on your customized configuration.<\/p>\n<h3 id=\"step-5-validate-the-output\"><strong>Step 5:<\/strong> Validate the Output<\/h3>\n<p>After applying the Terraform configuration, use the Terraform output command to retrieve essential details<\/p>\n<p>The <code>output.tf<\/code> file is configured to show the Policy ID as the output.<\/p>\n<p>Verify the output to ensure that the tag policy has been successfully implemented in your AWS account.<\/p>\n<h3 id=\"step-6-clean-up-setup\"><strong>Step <\/strong>6: Clean Up Setup<\/h3>\n<p>If you plan to remove the tag policy across your AWS account, you can destroy the configuration using the Terraform destroy command.<\/p>\n<pre><code>terraform destroy -var-file=..\/..\/vars\/dev\/tag-policy.tfvars<\/code><\/pre>\n<p>Confirm the action by typing &#8220;yes&#8221; when prompted. Terraform will remove all the resources created in the previous steps.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Using a Tag Policy will improve the organization&#8217;s tagging strategy and prevent users from creating resources with non-compliant tags.<\/p>\n<p>With <strong>AWS Resource Groups &amp; Tag Editor<\/strong>, resources and their related tags can be effectively managed. This service allows for filtering AWS Resources based on tags, making resource management more efficient and organized.<\/p>\n<p>You can also check out the <a href=\"https:\/\/devopscube.com\/terraform-autoscaling-group\/\">AWS autoscaling terraform<\/a> guide to deploy autoscaling groups and <a href=\"https:\/\/devopscube.com\/aws-load-balancers\/\">AWS load balancers<\/a> using Terraform.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/aws-tag-policy-terraform\/\" target=\"_blank\" rel=\"noopener noreferrer\">AWS Tag Policy Configuration Using Terraform \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/aws-tag-policy-terraform\/<\/p>\n","protected":false},"author":1,"featured_media":1087,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1086","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1086"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1086\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1087"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}