How to get the name of EC2 from its instance-id using AWS Lambda
How to get the name of EC2 from its instance-id using AWS Lambda
How to get the name of EC2 from its instance-id using AWS Lambda
If you are an AWS user, you might have encountered a situation where you have to get the name of an EC2 instance from its instance ID. This can be a daunting task if you are new to AWS. Fortunately, with the help of AWS Lambda, it is now possible to automate this task and get the instance name in a few simple steps. In this blog, we will guide you through retrieving the name of an EC2 instance from its instance ID using AWS Lambda. So, let’s get started!
def ec2_name(instanceId):
ec2r = boto3.resource('ec2')
running_instances = ec2r.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name': 'instance-id', 'Values': [instanceId]}])
for instance in running_instances:
for tag in instance.tags:
if 'Name' in tag['Key']:
name = tag['Value']
print(name)
def lambda_handler(event, context):
instanceIds = ["i-0d397eb19ba20063b", "i-0077a88cef38d609e"]
for i in instanceIds:
name = ec2_name(i)
print(name)
Output:
Note: It will return the name for only instances which are running.
I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com
Special credits to my team members: Gaurav Kachariya and Krisha Amlani.
Thank You…
If you are an AWS user, you might have encountered a situation where you have to get the name of an EC2 instance from its instance ID. This can be a daunting task if you are new to AWS. Fortunately, with the help of AWS Lambda, it is now possible to automate this task and get the instance name in a few simple steps. In this blog, we will guide you through retrieving the name of an EC2 instance from its instance ID using AWS Lambda. So, let’s get started!
def ec2_name(instanceId):
ec2r = boto3.resource('ec2')
running_instances = ec2r.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name': 'instance-id', 'Values': [instanceId]}])
for instance in running_instances:
for tag in instance.tags:
if 'Name' in tag['Key']:
name = tag['Value']
print(name)
def lambda_handler(event, context):
instanceIds = ["i-0d397eb19ba20063b", "i-0077a88cef38d609e"]
for i in instanceIds:
name = ec2_name(i)
print(name)
Output:
Note: It will return the name for only instances which are running.
I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com
Special credits to my team members: Gaurav Kachariya and Krisha Amlani.
Thank You…
If you are an AWS user, you might have encountered a situation where you have to get the name of an EC2 instance from its instance ID. This can be a daunting task if you are new to AWS. Fortunately, with the help of AWS Lambda, it is now possible to automate this task and get the instance name in a few simple steps. In this blog, we will guide you through retrieving the name of an EC2 instance from its instance ID using AWS Lambda. So, let’s get started!
def ec2_name(instanceId):
ec2r = boto3.resource('ec2')
running_instances = ec2r.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name': 'instance-id', 'Values': [instanceId]}])
for instance in running_instances:
for tag in instance.tags:
if 'Name' in tag['Key']:
name = tag['Value']
print(name)
def lambda_handler(event, context):
instanceIds = ["i-0d397eb19ba20063b", "i-0077a88cef38d609e"]
for i in instanceIds:
name = ec2_name(i)
print(name)
Output:
Note: It will return the name for only instances which are running.
I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on Instagram or LinkedIn.For any kind of work related to DevOps, Site Reliability you can contact me at helpmeanubhav@gmail.com
Special credits to my team members: Gaurav Kachariya and Krisha Amlani.
Thank You…