How to apply AWS EC2 with Boto3 & Python - Part 1
Tags: boto3, aws, python, ec2
Published: 2022-05-16 17:14:37

How to apply AWS EC2 with Boto3 & Python

1. [How to apply AWS EC2 with Boto3 & Python - Part 1](https://dev.to/kcdchennai/how-to-use-aws-ec2-with-boto3-python-part-1-i48)


2. [How to apply AWS EC2 with Boto3 & Python - Part 2](https://dev.to/kcdchennai/aws-5149)


`Boto3` is the Python SDK for AWS. It can be used to immediately interact with AWS sources from Python scripts.

In this article, we will appear at how we can use the Boto3 EC2 Python SDK to perform a number of operations on AWS EC2.


![EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9f0tcn7hyl5koq4e6rq6.jpg)

## Prerequisites

- [AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)
- Python v3.6 or want to be hooked up on our local machine.
- A code editor. We can use any textual content editor to work with Python files. 
- AWS IAM user, an access key ID, and a secret key need to be set up on our local computer with access to create and manipulate EC2 instances.
- Boto3 Python AWS SDK must be already installed on the local machine. If not, refer this [Boto documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)

## Creating EC2 Instances with Boto3


- Open code editor.

```
code ec2_create_instance.py
```


- Copy and paste the [Python script](https://github.com/MakendranG/aws-ec2-boto3/blob/main/ec2_create_instance.py) into code editor and store the file. 

The Python script creates a single AWS EC2 instance using an image ID `ami-09d56f8956ab235b3` using an instance type of `t2.micro`.

- Open command-line and execute the `ec2_create_instance` script. If successful, we should see a single message of `AWS EC2 Instance Launched successfully`. 

```
python ~\ec2_create_instance.py
```

## Tagging EC2 Instance with Boto3


In a AWS environment, an organization could have hundreds of resources to manage. To simplify managing resources, AWS provides a feature called `tagging` that allows us to categorize resources based on environment, department, or any other organization-specific criteria.


- Open code editor.

```
code tag_ec2_instance.py
```

- Copy and paste the [Python script](https://github.com/MakendranG/aws-ec2-boto3/blob/main/tag_ec2_instance.py) into code editor and store the file. 

The Python script tags the instance ID created above with the `Name` of KCDCHENNAI-DEMO using the `create_tags()` method.



- Open command-line and execute the `tag_ec2_instance` script. 

```
python ~\tag_ec2_instance.py
```

## Describing EC2 Instance with Boto3

We can use `describe` instances to find EC2 instances matching a specific architecture, image ID, instance type, or tags. Using the `describe API` and Boto3, we can construct a Python script to query EC2 instances by tag.

- Open code editor.

```
code ec2_describe_instance.py
```

- Copy and paste the [Python script](https://github.com/MakendranG/aws-ec2-boto3/blob/main/ec2_describe_instance.py) into code editor and store the file. 

Using the `describe_instances()` method, this script uses a filter described in JSON to discover all attributes related with all EC2 instances with a tag called Name (tag:Name) with a value of KCDCHENNAI-DEMO `('Values': ['KCDCHENNAI-DEMO'] )`.



- Open command-line and execute the `ec2_describe_instance` script. 

```
python ~\ec2_describe_instance.py
```



To comprehend greater about the use of EC2 in Boto, refer [Boto documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#id1009)

Thanks for reading my article till end. I hope you realized something unique today. If you loved this article then please share to your friends and if you have hints or thoughts to share with me then please write in the comment box.