Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 25, 2022 10:12 pm GMT

Using AWS EC2 with Boto3 & Python - Part 1

How to use AWS EC2 with Boto3 & Python

  1. How to use AWS EC2 with Boto3 & Python - Part 1
  2. How to use AWS EC2 with Boto3 & Python - Part 2

Boto3 is the Python SDK for AWS. It can be used to directly interact with AWS resources from Python scripts.

In this article, we will look at how we can use the Boto3 EC2 Python SDK to perform various operations on AWS EC2.

Prerequisites

  • AWS account
  • Python v3.6 or later need to be installed on our local machine.
  • A code editor. We can use any text editor to work with Python files.
  • AWS IAM user, an access key ID, and a secret key should be set up on our local machine with access to create and manage EC2 instances.
  • Boto3 Python AWS SDK should 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 save 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 save 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 ~ag_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 build 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 save the file.

Using the describe_instances() method, this script uses a filter defined in JSON to find all attributes associated 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 know more about using EC2 in Boto, refer Boto documentation

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


Original Link: https://dev.to/makendrang/using-aws-ec2-with-boto3-python-part-1-52g5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To