Go to Polices in AWS Organizations and select Tag policies. be sure to enable it if it isn't enabled prior. then create a policy like this. you can use the given json editor for this.
{
"tags": {
"User": {
"tag_key": {
"@@assign": "User"
},
"enforced_for": {
"@@assign": [
"ec2:instance"
]
}
},
"Duration": {
"tag_key": {
"@@assign": "Duration"
},
"enforced_for": {
"@@assign": [
"ec2:instance"
]
}
}
}
}
In here, I have created a tag policy for User tag and Duration Tag. Once the tag policy is created, make sure to attach it to the target OU/Account.
After this, go to Policies again and select Service control policies (SCPs)
and if it isn't available, make sure to enable it. In there, create a new SCPs policy and add the following policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyEC2CreationSCP1",
"Effect": "Deny",
"Action": [
"ec2:RunInstances"
],
"Resource": [
"arn:aws:ec2:*:*:instance/*",
"arn:aws:ec2:*:*:volume/*"
],
"Condition": {
"Null": {
"aws:RequestTag/User": "true"
}
}
},
{
"Sid": "DenyEC2CreationSCP2",
"Effect": "Deny",
"Action": [
"ec2:RunInstances"
],
"Resource": [
"arn:aws:ec2:*:*:instance/*",
"arn:aws:ec2:*:*:volume/*"
],
"Condition": {
"Null": {
"aws:RequestTag/Duration": "true"
}
}
}
]
In here I have created SCP to enforce previous tag policy. Once the SCPs are created, make sure that you attach it to the target OU/Account.