# 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](https://www.instagram.com/acanubhav94/) or [LinkedIn](https://www.linkedin.com/in/anubhav-chaturvedi-a7465a72/).For any kind of work related to DevOps, Site Reliability you can contact me at [helpmeanubhav@gmail.com](mailto:helpmeanubhav@gmail.com)

> Special credits to my team members: [**Gaurav Kachariya**](https://www.linkedin.com/in/gaurav-kachariya/) **and** [**Krisha Amlani**](https://www.linkedin.com/in/krisha-amlani/)**.**

> Thank You…
