top of page

Create a Python script for AWS Automation

Updated: Aug 26, 2023

Step 1: Install Boto3 Library

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. It enables Python developers to create, configure, and man


age AWS services, such as EC2 and S3. To install Boto3, run the following command in your command prompt or terminal:


pip install boto3

Step 2: Set up AWS Credentials


To use Boto3, you will need to set up AWS credentials. AWS provides access keys that consist of an access key ID and a secret access key. You can create these keys in the AWS Management Console. Once you have your keys, you can set them up in your Python script using the following code:


import boto3

aws_access_key_id = 'YOUR_ACCESS_KEY'
aws_secret_access_key = 'YOUR_SECRET_KEY'

session = boto3.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name='YOUR_REGION_NAME'
)

Step 3: Create an EC2 Instance


To create an EC2 instance using Boto3, you will need to specify the following parameters:


- Image ID: The ID of the Amazon Machine Image (AMI) to use for the instance.

- Instance Type: The type of instance to launch, such as t2.micro or m5.large.

- Key Pair: The name of the key pair to use to log in to the instance.

- Security Group: The name of the security group to associate with the instance.


Here's an example of Python code to create an EC2 instance:


import boto3

aws_access_key_id = 'YOUR_ACCESS_KEY'
aws_secret_access_key = 'YOUR_SECRET_KEY'

session = boto3.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name='YOUR_REGION_NAME'
)

ec2 = session.resource('ec2')

instance = ec2.create_instances(
    ImageId='AMI_ID',
    InstanceType='INSTANCE_TYPE',
    KeyName='KEY_PAIR_NAME',
    SecurityGroups=['SECURITY_GROUP_NAME'],
    MinCount=1,
    MaxCount=1
)[0]

print(instance.id)

Step 4: Stop an EC2 Instance


To stop an EC2 instance using Boto3, you will need to specify the instance ID. Here's an example of Python code to stop an


EC2 instance:


import boto3

aws_access_key_id = 'YOUR_ACCESS_KEY'
aws_secret_access_key = 'YOUR_SECRET_KEY'

session = boto3.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name='YOUR_REGION_NAME'
)

ec2 = session.resource('ec2')

instance_id = 'INSTANCE_ID'

instance = ec2.Instance(instance_id)
response = instance.stop()

print(response)
282 views0 comments

Recent Posts

See All

Difference between DEVOPS and SRE with Example?

DevOps (Development and Operations) and SRE (Site Reliability Engineering) are two approaches or philosophies that aim to improve software development and operations, but they have different focuses a

Linux for DevOps Interview Questions

Question: Write a Linux command to list all files and directories in the current directory. Answer: ls Question: How would you create a new directory named "my_project" in the home directory of the cu

Comments


bottom of page