Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an AWS IAM policy for automatic resource tagging?

I would like to set up different AWS Identity and Access Management (IAM) users so that if an AWS resource is created by that IAM user, the resource is automatically assigned a specific tag.

For example: if IAM user F creates a new EC2 instance, the instance is automatically tagged as User:MrF. Is it possible to build a custom policy that does this?

like image 328
Michael Avatar asked May 07 '14 12:05

Michael


People also ask

How do I create a tag policy in AWS?

To create a tag policy You must sign in as an IAM user, assume an IAM role, or sign in as the root user (not recommended) in the organization's management account. On the Tag policies page, choose Create policy. On the Create policy page, enter a Policy name and an optional Policy description.

How do I enforce AWS tags of resources?

When a tag policy is applied to your AWS account, users are unable to create resources using noncompliant tags. You can enforce specific tag policies by choosing the option 'prevent non-compliant operations for this tag', and selecting the resource types that supports tag policy enforcement.


2 Answers

My company GorillaStack have an open source lambda function that does exactly that.

The function 'listens' for CloudTrail logs to be delivered and tag the created resource with the ARN of the user that created it. It also support cross account tagging, for cases where a central account collects CloudTrail logs for other accounts.

  • Github: https://github.com/GorillaStack/auto-tag
  • Blog Post: http://blog.gorillastack.com/gorillastack-presents-auto-tag

It got a shout out at the 2015 re:Invent conference which is pretty cool :)

Hope that helps!

like image 113
e_m0ney Avatar answered Sep 23 '22 18:09

e_m0ney


This is not available when using the AWS APIs directly (i.e. there's no way to command all AWS API's to tag new resources automatically on your behalf), however, depending on the specifics of your use case you could work around that limitation by correlating the creating user with the resource via post hoc tagging:

Workaround

You could activate AWS CloudTrail, which records AWS API calls for your account and delivers log files to you and provides exactly the information you are after:

The recorded information includes the identity of the API caller, the time of the API call, the source IP address of the API caller, the request parameters, and the response elements returned by the AWS service.

Based on that information, a dedicated service of yours could analyze the logs and apply post hoc tags to all resources based on the logged user and created resource via the resp. API actions. Please see my answer to Which user launched EC2 instance? for some caveats/constraints to consider when going down this route.

like image 38
Steffen Opel Avatar answered Sep 21 '22 18:09

Steffen Opel