Applying AWS EC2 with Boto3 & Python - Part 2
Tags: boto3, aws, python, ec2
Published: 2022-05-25 22:11:16


Applying AWS EC2 with Boto3 & Python

1. [Applying AWS EC2 with Boto3 & Python - Part 1](https://dev.to/makendrang/using-aws-ec2-with-boto3-python-part-1-52g5)

2. [Applying AWS EC2 with Boto3 & Python - Part 2](https://dev.to/makendrang/using-aws-ec2-with-boto3-python-part-2-3gmi)



`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/fdaw5o4bhw7wvlvte1rk.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)

## Starting, Stopping and Terminating EC2 Instances with Boto3

- Open code editor.

```
code ec2_manage_instance.py
```


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

In the Python script, primarily based on the code we can stop `stop_instance()`, start `start_instance()` or terminate the instance `terminate_instance()` with instance ID.

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

```
python ~\ec2_manage_instance.py
```

## Finding Specific Details of Multiple EC2 Instances at Once

By the usage of `describe_instance()` approach, We can get particular unique attributes on many different EC2 instances.

- Open code editor.

```
code ec2_multiple_instances.py
```

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

The Python script establishes a client connection to AWS. Once connected, it then makes use the `describe_instances()` approach as shown earlier to query various attributes of all running EC2 instances. It’s limiting results to only running instances by filtering on one of the available attributes, `instance-state-name`, with the value of `running`.

To return only certain attributes, the script makes use of a `for` loop to iterate over every reservation and each instance inside of every reservation to print out the `InstanceID`, `InstanceType`, `PrivateIPAddress` and `PublicIpAddress` of every instance found.

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

```
python ~\ec2_multiple_instances.py
```


To be aware of more about the usage 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 until end. I hope you learned some thing distinctive today. If you loved this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the remark box.


Above blog is submitted as part of 'Devtron Blogathon 2022' - https://devtron.ai/
Check out Devtron's GitHub repo - https://github.com/devtron-labs/devtron/ and give a ⭐ to show your love & support.
Follow Devtron on LinkedIn - https://www.linkedin.com/company/devtron-labs/ and Twitter - https://twitter.com/DevtronL/, to keep yourself updated on this Open Source project.