Auto Tag AWS Ec2 Instances when they get provisioned

Praveen Vallepu
3 min readSep 28, 2022

--

Since AWS consumption increasing drastically in the last decade, Organizations have provisioned a huge number of resources like Ec2, S3, and RDS to deploy their applications. As the number of resources increases, identifying resources will get challenging. Tagging resources will help the Organization identify the owner of that resource and filter the resources using the same Tag. Tagging can identify the resource environment, creation date, Monitoring, and type of application. Also, Tagging can be used to track the billing costs as well. Developers and Cloud engineers provision Ec2 instances daily basis for testing different use cases. If they add the owner Tag to the Ec2 instances, it will be easy to identify who is responsible for that particular instance.

I have a customer who uses one of the AWS accounts as a sandbox environment, so all their engineers provision the Ec2 instances as part of testing. Since It is a sandbox environment, the customer wants to Tag the Ec2 instances when they get created to use the same Tag to include in future automation for terminating or stopping the instances. In this example, we Tag the Ec2 instances when they get created using CloudFormation, Lambda Function using python Boto3, and EventBridge.

Ec2_Auto_Tag CloudFormation Template:

https://github.com/praveenv4/Task/blob/main/Cloudformation/Ec2_Auto_Tag.yaml

Create Ec2_Auto_Tag CloudFormation Stack in the AWS account:

  1. Log in to the AWS account and go to Cloudformation service
  2. Choose Create Stack and click on “with new resources (standard).”

3. In this example, our template is ready, so select “Upload a template file” and click on “Choose file” then select the file from your local machine.

4. After choosing the template, click Next, provide a name like “Ec2_Auto_Tag” and provide the required parameters, then click next.
Ec2TagKey: Provide Ec2 instance Tag key, which will be used to Tag instance. This template uses the “AutoStop” key as a default.
Ec2TagVaule: Provide Ec2 instance Tag value, which will be used to Tag instance. This template uses the “Enable” value as a default.
Region: us-east-1 (enter your AWS sandbox region)

5. Now, Click on Next, then check the acknowledge box and click Create Stack

6. After a few minutes, Stack creation will be completed.

Once the above template got provisioned, whenever there is an Ec2 instance provisioned, the EventBridge rule will be triggered from the CloudTrail API events and Invoke the Lambda. Then Lambda will read the events and get the new instance ID, Tag it as per the given Tag Key and Value.

Conclusion:

Using the above solution, Tagging can be done automatically without manual intervention, and the same Tags can be used for different automation, Billing, and Identification in the future.

Updated by Praveen Vallepu | on 27 SEPTEMBER 2022

--

--

Praveen Vallepu

Sr. AWS DevOps Engineer | 5x AWS Certified | Certified Kubernetes Administrator (CKA) | Python | Docker | Terraform