How to apply AWS EC2 with Boto3 & Python

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.

Prerequi…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by MakendranG

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

Prerequisites

  • 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

Creating EC2 Instances with Boto3

  • Open code editor.
code ec2_create_instance.py
  • Copy and paste the Python script 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 into code editor and store the file.

The Python script tags the instance ID created above with the Name of BOTO3-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 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 BOTO3-DEMO ('Values': ['BOTO3-DEMO'] ).

  • Open command-line and execute the ec2_describe_instance script.
python ~\ec2_describe_instance.py

Starting, Stopping and Terminating EC2 Instances with Boto3

  • Open code editor.
code ec2_manage_instance.py
  • Copy and paste the Python script 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 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

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.

Follow me and share your thoughts,
GitHub
LinkedIn
Twitter


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by MakendranG


Print Share Comment Cite Upload Translate Updates
APA

MakendranG | Sciencx (2022-09-08T04:58:14+00:00) How to apply AWS EC2 with Boto3 & Python. Retrieved from https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/

MLA
" » How to apply AWS EC2 with Boto3 & Python." MakendranG | Sciencx - Thursday September 8, 2022, https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/
HARVARD
MakendranG | Sciencx Thursday September 8, 2022 » How to apply AWS EC2 with Boto3 & Python., viewed ,<https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/>
VANCOUVER
MakendranG | Sciencx - » How to apply AWS EC2 with Boto3 & Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/
CHICAGO
" » How to apply AWS EC2 with Boto3 & Python." MakendranG | Sciencx - Accessed . https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/
IEEE
" » How to apply AWS EC2 with Boto3 & Python." MakendranG | Sciencx [Online]. Available: https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/. [Accessed: ]
rf:citation
» How to apply AWS EC2 with Boto3 & Python | MakendranG | Sciencx | https://www.scien.cx/2022/09/08/how-to-apply-aws-ec2-with-boto3-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.