dragonmaster_admin
dragonmaster_admin

Reputation: 1

AWS Lambda to get specific tag value

I am looking to get specific tags from AWS in Lambda.

[{'Key': 'other', 'Value': 'other'}, {'Key': 'snstopic', 'Value': 'arn:aws:sns:us-west-2:123456789:TestEmail'}, {'Key': 'Name', 'Value': 'test-user'}]

I want the value of the key Name.

This is how I am doing it now; but the issue is that the tag Name won't always be #2.

name = exported_json[2]['Value']

This is running in AWS Lambda.

Upvotes: 0

Views: 729

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270174

You can use:

exported_json = [{'Key': 'other', 'Value': 'other'}, {'Key': 'snstopic', 'Value': 'arn:aws:sns:us-west-2:123456789:TestEmail'}, {'Key': 'Name', 'Value': 'test-user'}]

names = [dict['Value'] for dict in exported_json if dict['Key'] == 'Name']

if len(names) > 0:
  print(names[0])

The if at the end handles the situation where there is no Name tag, but if there are multiple Name tags is just returns the first one.

Upvotes: 1

Related Questions