Skip to main content

Command Palette

Search for a command to run...

Launching AWS EC2 Instance using Python

Updated
1 min readView as Markdown

Prerequisites:

Required AWS account and ec2 knowledge

Install boto3 in Python

pip install boto3
python3  -m pip install boto3 ( incase Python3 )

example_ec2 = boto3.client('ec2', 'ca-central-1', aws_access_key_id='', aws_secret_access_key='')

the function to describe all the instances and current state

requested_response = example_ec2.describe_instances()

print(requested_response)

To create an instance using python

import boto3

/

Function for connecting to EC2

user_ec2 = boto3.client('ec2', 'ap-south-1', aws_access_key_id='', aws_secret_access_key='')

Function for running instances

instance_run_example= user_ec2.run_instances(InstanceType="t2.micro", MaxCount=1, MinCount=1, ImageId="ami-***********************")

print(instance_run_example)

EC2 instance terminates using Python and the boto3 library

always import boto3

import boto3

  • first Connect to the EC2 service

ec2 = boto3.client('ec2', 'ca-central-1', aws_access_key_id='', aws_secret_access_key='')

Terminate the instance using the specified ID

user_output = ec2.terminate_instances(InstanceIds=['instance-id-1'])

Print the user_response

print(user_output)

More from this blog

Welcome to Devops Learning !!!!

24 posts