There are a few options to add the custom tags for AWS EC2 instances created by BigAnimal:
- Add the tags using AWS Tag Editor(see the article);
- Add the tags from the portal of EDB Cloud Service;
- Add the tags using AWS EventBridge service and Lambda function.
The article focuses on the option 2 and 3.
How to add the tags from the portal of EDB Cloud Service
1. Login the portal of EDB Cloud Service, goto 'Cloud Providers' page:
2. Click 'Your Cloud Account' then 'Manage Tags' to add the tags:
How to add the tags using AWS EventBridge service and Lambda function
1. Goto AWS Lambda at AWS console:
2. Create the Lambda function to add the custom tags on the EC2 instances filtered by the tag:
- ManagedBy:BigAnimal
The following is the sample code:
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='us-east-1')
instances = ec2.describe_instances(Filters=[{'Name': 'tag:ManagedBy', 'Values': ['BigAnimal']}])
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
instance_id = instance['InstanceId']
tag_key = 'newtagkey'
tag_value = 'newtagvalue'
response = ec2.create_tags(
Resources=[instance_id],
Tags=[
{
'Key': tag_key,
'Value': tag_value
}
]
)
return {
'statusCode': 200,
'body': 'Additional tags created for BigAnimal EC2 instances in the us-east-1 region'
}
Note:
- The IAM role of the Lambda function needs to have the required EC2 permissions(By default, Lambda will create an execution role with permission to upload logs to Amazon CloudWatch Logs only).
- Lambda function timeout is 3 seconds by default, it can be increased by 'Edit basic settings':
3. Goto Amazon EventBridge page, click 'Create rule':
3.1. Define rule detail, select 'Schedule':
3.2. Define schedule:
3.3 Select target:
Select 'AWS service' then select the Lambda function you created from the dropdown list.
Complete step 4 and 5 to create the rule.
Once above complete, the custom tags will be added at the schedule.