Skip to content

Commit f5e5b29

Browse files
authored
Merge pull request #14 from arunlalp/master
[TEC-69] Provision AWS tag policy using Terraform
2 parents f7e56a1 + edef58e commit f5e5b29

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ terraform destroy -var-file=../../../vars/dev/ec2.tfvars
6565

6666
**Note**: Always review the execution plan (`terraform plan`) before applying changes to avoid unintended modifications.
6767

68+
## Terraform AWS Organization Tag Policy Implement.
69+
70+
1. Navigate to the `environment/dev` folder:
71+
72+
```bash
73+
cd environment/tag-policy
74+
```
75+
76+
2. Open the `tag-policy.tfvars` file and modify it with your desired details. This file contains variables used in the Terraform configuration.
77+
78+
## Deployment
79+
80+
1. Initialize Terraform in the working directory:
81+
82+
```bash
83+
terraform init
84+
```
85+
86+
2. Create an execution plan:
87+
88+
```bash
89+
terraform plan -var-file=../../../vars/dev/tag-policy.tfvars
90+
```
91+
92+
3. Apply the changes to create the Tag Policy:
93+
94+
```bash
95+
terraform apply -var-file=../../../vars/dev/tag-policy.tfvars
96+
```
97+
98+
4. To destroy the Tag Policy:
99+
100+
```bash
101+
terraform destroy -var-file=../../../vars/dev/tag-policy.tfvars
102+
```
103+
104+
**Note**: Always review the execution plan (`terraform plan`) before applying changes to avoid unintended modifications.
105+
68106
## Command Reference
69107

70108
Update all outputs:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
module "tag-policy" {
6+
source = "../../../modules/tag-policy"
7+
region = var.region
8+
policy_name = var.policy_name
9+
policy_type = var.policy_type
10+
target_id = var.target_id
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "policy_id" {
2+
value = module.tag-policy.policy_id
3+
description = "ID of the tag policy"
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "region" {
2+
type = string
3+
description = "Region for the provider"
4+
}
5+
6+
variable "policy_name" {
7+
type = string
8+
description = "Name for the tag policy"
9+
}
10+
11+
variable "policy_type" {
12+
type = string
13+
description = "Type of the policy"
14+
}
15+
16+
variable "target_id" {
17+
type = number
18+
description = "ID of the target"
19+
}

modules/tag-policy/main.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Provider Configuration
2+
provider "aws" {
3+
region = var.region
4+
}
5+
6+
# Create Tag Policy
7+
resource "aws_organizations_policy" "tag_policy" {
8+
name = var.policy_name
9+
description = "Resource Provision"
10+
11+
content = jsonencode({
12+
"tags" = {
13+
"Name" = {
14+
"tag_key" = {
15+
"@@assign" = "Name"
16+
},
17+
"enforced_for" = {
18+
"@@assign" = [
19+
"ec2:instance",
20+
"ec2:security-group"
21+
]
22+
}
23+
},
24+
"Environment" = {
25+
"tag_key" = {
26+
"@@assign" = "Environment"
27+
},
28+
"tag_value" = {
29+
"@@assign" = [
30+
"dev",
31+
"stage",
32+
"prod"
33+
]
34+
},
35+
"enforced_for" = {
36+
"@@assign" = [
37+
"ec2:instance",
38+
"ec2:security-group"
39+
]
40+
}
41+
},
42+
"Owner" = {
43+
"tag_key" = {
44+
"@@assign" = "Owner"
45+
},
46+
"tag_value" = {
47+
"@@assign" = [
48+
"Techiescamp"
49+
]
50+
},
51+
"enforced_for" = {
52+
"@@assign" = [
53+
"ec2:instance",
54+
"ec2:security-group"
55+
]
56+
}
57+
},
58+
"CostCenter" = {
59+
"tag_key" = {
60+
"@@assign" = "CostCenter"
61+
},
62+
"tag_value" = {
63+
"@@assign" = [
64+
"project-pet-clinic"
65+
]
66+
},
67+
"enforced_for" = {
68+
"@@assign" = [
69+
"ec2:instance",
70+
"ec2:security-group"
71+
]
72+
}
73+
},
74+
"Application" = {
75+
"tag_key" = {
76+
"@@assign" = "Application"
77+
},
78+
"tag_value" = {
79+
"@@assign" = [
80+
"web-app"
81+
]
82+
},
83+
"enforced_for" = {
84+
"@@assign" = [
85+
"ec2:instance",
86+
"ec2:security-group"
87+
]
88+
}
89+
}
90+
}
91+
})
92+
93+
type = var.policy_type
94+
}
95+
96+
resource "aws_organizations_policy_attachment" "account_attachment" {
97+
policy_id = aws_organizations_policy.tag_policy.id
98+
target_id = var.target_id
99+
}
100+
101+
102+
103+

modules/tag-policy/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "policy_id" {
2+
value = aws_organizations_policy.tag_policy.id
3+
description = "ID of the tag policy."
4+
}

modules/tag-policy/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "region" {
2+
type = string
3+
description = "Region for the provider."
4+
}
5+
6+
variable "policy_name" {
7+
type = string
8+
description = "Name for the tag policy."
9+
}
10+
11+
variable "policy_type" {
12+
type = string
13+
description = "Type of the policy."
14+
}
15+
16+
variable "target_id" {
17+
type = number
18+
description = "ID of the target."
19+
}

vars/dev/tag-policy.tfvars

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Tag Policy Vars
2+
region = "eu-north-1"
3+
policy_name = "Techiescamp"
4+
policy_type = "TAG_POLICY"
5+
target_id = "814200988517"

0 commit comments

Comments
 (0)