Export tags for a list of Amazon EC2 instances to a CSV file - AWS Prescriptive Guidance

Export tags for a list of Amazon EC2 instances to a CSV file

Created by Sida Ju (AWS) and Pac Joonhyun (AWS)

Summary

This pattern shows how to programmatically export tags for a list of Amazon Elastic Compute Cloud (Amazon EC2) instances to a CSV file.

By using the example Python script provided, you can reduce how long it takes to review and categorize your Amazon EC2 instances by specific tags. For example, you could use the script to quickly identify and categorize a list of instances that your security team has flagged for software updates.

Prerequisites and limitations

Prerequisites

  • Python 3 installed and configured

  • AWS Command Line Interface (AWS CLI) installed and configured

Limitations

The example Python script provided in this pattern can search Amazon EC2 instances based on the following attributes only:

  • Instance IDs

  • Private IPv4 addresses

  • Public IPv4 addresses

Tools

  • Python is a general-purpose computer programming language.

  • virtualenv helps you create isolated Python environments.

  • AWS Command Line Interface (AWS CLI) is an open-source tool that helps you interact with AWS services through commands in your command-line shell.

Code repository

The example Python script for this pattern is available in the GitHub search-ec2-instances-export-tags repository.

Epics

TaskDescriptionSkills required

Clone the GitHub repository.

Note

If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.

Clone the GitHub search-ec2-instances-export-tags repository by running the following Git command in a terminal window:

git clone https://github.com/aws-samples/search-ec2-instances-export-tags.git
DevOps engineer

Install and activate virtualenv.

  1. Install virtualenv by running the following command:

    python3 -m pip install virtualenv
  2. Create a new virtual environment by running the following command:

    python3 -m venv env
  3. Activate the new virtual environment by running the following command:

    source env/bin/activate

For more information, see the virtualenv User Guide.

DevOps engineer

Install dependencies.

  1. Open the code directory by running the following command in the terminal:

    cd search-ec2-instances-export-tags
  2. Install the requirements.txt file by running the following pip command:

    pip3 install -r requirements.txt
DevOps engineer

Configure an AWS named profile.

If you haven’t already, configure an AWS named profile that includes the required credentials to run the script. To create a named profile, run the aws configure command.

For more information, see Using named profiles in the AWS CLI documentation.

DevOps engineer
TaskDescriptionSkills required

Create the input file.

Create an input file that contains a list of the Amazon EC2 instances that you want the script to search and export tags for. You can list instance IDs, private IPv4 addresses, or public IPv4 addresses.

Important

Make sure that each Amazon EC2 instance is listed on its own line in the input file.

Input file example

1 i-0547c351bdfe85b9f 2 54.157.194.156 3 172.31.85.33 4 54.165.198.144 5 i-0b6223b5914111a4b 6 172.31.85.44 7 54.165.198.145 8 172.31.80.219 9 172.31.94.199
DevOps engineer

Run the Python script.

Run the script by running the following command in the terminal:

python search_instances.py -i INPUTFILE -o OUTPUTFILE -r REGION [-p PROFILE]
Note

Replace INPUTFILE with the name of your input file. Replace OUTPUTFILE with the name you want to give the CSV output file. Replace REGION with the AWS Region that your Amazon EC2 resources are in. If you’re using an AWS named profile, replace PROFILE with the named profile that you’re using.

To get a list of supported parameters and their description, run the following command:

python search_instances.py -h

For more information and to see an output file example, see the README.md file in the GitHub search-ec2-instances-export-tags repository.

DevOps engineer

Related resources