How to Start an AWS EC2 Instance using Python SDK

In this blog post, I will show you how to start a single AWS EC2 instance using Python and the AWS SDK for Python.

About AWS SDK for Python

The Aws SDK for Python is implemented using a Python library \ module called Boto 3.

Install AWS Python SDK

To install the SDK, I will use the following command:

pip install boto3

Start EC2 Instance

In the code below, I am starting my EC2 instance, which is located in the Australia south-east region.

The SDK will use the AWS CLI profile configured on the machine you run the code from.

Simply, change the settings in the code to reflect the instance ID and region and run the code.

import boto3
ec2 = boto3.client('ec2') 
region = 'ap-southeast-2' 
instances = ['i-087b3f3791a065ba6'] 
ec2.start_instances(InstanceIds=instances) 
 print('started your instances: ' + str(instances)) 

Below you can see the code in my VS Code console.

After the code run, it will take less than 3 seconds for the portal to show the new status.

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.