Wednesday, April 6, 2022

[SOLVED] Add instance names/fleet name/ASG name against resource IDs in AWS Cost & Usage report

Issue

I have setup a cost and usage report in a s3 bucket. I'm trying to get different cost for EC2 instances from the report and I need to add a custom column which has a fleet name OR ASG name kind of details against the instance IDs.

I have got the instance Name and their IDs using EC2 API in a dictionary, but I'm not able to add my custom column to the report so that I can use groupBy to get different types of costs associated with instances.


Solution

There is no straight forward way to do this. We have to add a new column to the dataframe derived from csv file(cost & usage report), then use the instance info derived using ec2 API(Use the tag option). Once we have the new column populated, we can use groupby() on the new column to get our desired data from the dataframe

running_instances = ec2_cli.instances.filter(Filters=[{
        'Name': 'instance-state-name',
        'Values': ['running']}])
    ec2info = defaultdict()

for instance in running_instances:
    for tag in instance.tags:
        if 'Name'in tag['Key']:
            name = tag['Value']
    ec2info[instance.id] = {
            'Name': name,
            'Type': instance.instance_type
    }

df.insert(loc=17, column='Instance_Name', value='Other')
instance_id = []

def update_col(x):
    for key, val in ec2info.items():
        if x == key:
            if ('MyAgg' in val['Name']) | ('MyAgg-AutoScalingGroup' in val['Name']):
                return 'SharkAggregator'
            if ('MyColl AS Group' in val['Name']) | ('MyCollector-AutoScalingGroup' in val['Name']):
                return 'SharkCollector'
            if ('MyMetric AS Group' in val['Name']) | ('MyMetric-AutoScalingGroup' in val['Name']):
                return 'Metric'

df['Instance_Name'] = df.ResourceId.apply(update_col)
df.Instance_Name.fillna(value='Other', inplace=True)


Answered By - Ravi
Answer Checked By - Senaida (WPSolving Volunteer)