I need to do the following:
I have some instances that contain a certain tag, I need to loop through those instances and for each instance which contains that certain tag, if that instance has an elastic ip attached, I need to tag that elastic ip with that same tag. My code is the following:
import boto3
import json
region_list = ['us-east-1']
session = boto3.Session(profile_name='default')
for region in region_list:
ec2 = session.resource('ec2',region)
client = boto3.client('ec2',region)
# Retrieve instances that contain this specific tag
instances = ec2.instances.filter(Filters=[{'Name':'tag:MyTargetTag', 'Values':['*']}])
for instance in instances:
for tag in instance.tags:
if tag['Key'] == "MyTargetTag":
MyTargetTag = tag['Value']
## check if this instance has an elasticip
## if it has, assign the value of MyTargetTag to it
response = client.add_tags(
ResourceArns=[
#elasticip ip identifier of some sort,
],
Tags=[
{
'Key': 'MyTargetTag',
'Value': MyTargetTag
},
]
)
I've read through the docs and videos and what not but honestly I don't understand it quite completely and I'm just doing trial and error without any success.