Getting Started with AWS and Python: Basic Concepts and Hands-On Examples on EC2.

virajpatoliya
3 min readMar 25, 2023

--

In this post, we’ll explore some of the basic concepts of AWS and how we can use Python to interact with them.

AWS, or Amazon Web Services, is a cloud computing platform that provides a wide range of services to help businesses and individuals build and deploy their applications on the cloud. Some of the key services provided by AWS include EC2 (Elastic Compute Cloud), S3 (Simple Storage Service), RDS (Relational Database Service), and Lambda.

Python is a popular programming language that is widely used for developing web applications, data analysis, machine learning, and more. Python is a great language to use with AWS because it has a variety of libraries and modules that make it easy to interact with AWS services.

Let’s dive into some basic concepts of AWS and Python.

  1. Setting up AWS credentials

To use AWS services with Python, we first need to set up our AWS credentials. This includes our AWS access key and secret access key. We can set up our credentials using the AWS CLI (Command Line Interface) or by manually creating an AWS configuration file.

To set up our credentials using the AWS CLI, we can run the following command in our terminal:

aws configure

This will prompt us to enter our AWS access key, secret access key, default region name, and default output format. Once we’ve entered this information, our credentials will be saved in a configuration file located in our home directory.

We can also create the configuration file manually by creating a file named ~/.aws/credentials and adding our credentials in the following format:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

2. Using the AWS SDK for Python (Boto3)

Boto3 is the official AWS SDK for Python. Boto3 makes it easy to interact with AWS services by providing a Pythonic interface to AWS resources.

To use Boto3, we first need to install it using pip:

pip install boto3

Once we’ve installed Boto3, we can import it in our Python script and create an AWS client object. For example, to create an EC2 client object, we can do the following:

import boto3
ec2 = boto3.client('ec2')

We can then use the methods provided by the EC2 client object to perform various operations on our EC2 instances. For example, we can use the describe_instances method to retrieve information about our instances:

response = ec2.describe_instances()

3. Creating an EC2 instance using Python

Now that we have our AWS credentials set up and have installed Boto3, let’s create an EC2 instance using Python.

To create an EC2 instance, we first need to specify the instance details such as the AMI (Amazon Machine Image), instance type, and security groups. We can do this by creating a dictionary with the instance details:

instance_details = {
'ImageId': 'ami-0c55b159cbfafe1f0',
'InstanceType': 't2.micro',
'KeyName': 'my-key-pair',
'MinCount': 1,
'MaxCount': 1,
'SecurityGroups': ['my-security-group']
}

We can then use the run_instances method of our EC2 client object to launch the instance:

response = ec2.run_instances(**instance_details)

This will return a dictionary containing information about the instance that was launched, including the instance ID.

4. Conclusion

In this post, we’ve explored some basic concepts of AWS and Python. We’ve learned

We learnt a bit about EC2 instance and AWS SDK or Python Boto3.

Then we created an EC2 instance using create_instance(), verified the created instance in console and we stopped the ec2 instance using Boto3.

I also specified how you can terminate your EC2 instance instead of stopping if you don’t need it anymore.

Were you able to create an ec2 instance using python boto3 using this tutorial? Let me know in the comment section.

--

--