Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS IAM Policy to Enforce Tagging

Is there a way to enforce tagging while creating EC2-Instances? I,e user cannot launch an instance without certain tags. And can I use that tags to give control to particular instance depending on the tag?

like image 499
Ramu Avatar asked Jan 24 '18 16:01

Ramu


Video Answer


2 Answers

I had a similar use case while I was working for a customer. The answer is yes you can !

You can enforce users to apply specific tags with IAM Policies.

For example you can attach a policy to a user/role (preferably role) that denies the ec2:RunInstances action with a condition that checks if a tag Key and Value are not what you are expecting. It can be a bit confusing as this policy uses double negation, Deny and StringNotLike but I believe its easier to enforce tagging that way as you can add this policy to a role that has the Administrator policy and still work.

    {
        "Sid": "ConditionalEC2creationName",
        "Effect": "Deny",
        "Action": "ec2:RunInstances",
        "Resource": "arn:aws:ec2:*:*:instance/*",
        "Condition": {
            "StringNotLike": {
                "aws:RequestTag/Name": "*"
            }
        }
    },
    {
        "Sid": "ConditionalEC2creationEnv",
        "Effect": "Deny",
        "Action": "ec2:RunInstances",
        "Resource": "arn:aws:ec2:*:*:instance/*",
        "Condition": {
            "StringNotLike": {
                "aws:RequestTag/Env": "*"
            }
        }
    }

Unfortunately i couldn't make it work in a single block because I didn't have time to optimise it. I think it has to do with ForAllValues, ForAnyValue.

ForAllValues – The condition returns true if there's a match between every one of the specified key values in the request and at least one value in the policy. It also returns true if there is no matching key in the request, or if the key values resolve to an empty data set, such as an empty string.

ForAnyValue – The condition returns true if any one of the key values in the request matches any one of the condition values in the policy. For no matching key or an empty data set, the condition returns false.

like image 74
Giorgos Dimitriou Avatar answered Sep 20 '22 14:09

Giorgos Dimitriou


You can achieve this using Amazon Config.

Select Rules -> Add Rule -> required tag

You won't prevent someone from creating an instance without a tag, but you will be able to see it flagged in the Config dashboard, or you can trigger a SNS action to notify you via email.

like image 21
Kasia Gogolek Avatar answered Sep 20 '22 14:09

Kasia Gogolek