Setup an AWS instance using Python

First, you need to install boto3 package via pip:

pip install boto3

Then, create a new Python file, let's say aws_setup.py and add the following code:

import boto3

# EC2 instance details
AMI = 'ami-0c55b159cbfafe1f0'  # Amazon Linux 2 AMI (HVM), SSD Volume Type
INSTANCE_TYPE = 't2.micro'  # Free tier instance type
KEY_NAME = 'your_key_pair_name'  # EC2 key pair name
SECURITY_GROUP_ID = 'sg-0123456789abcdef'  # Security group ID
REGION = 'us-east-1'  # Region where you want to create the instance

# Create EC2 client
ec2 = boto3.client('ec2', region_name=REGION)

# Launch a new EC2 instance
response = ec2.run_instances(
    ImageId=AMI,
    InstanceType=INSTANCE_TYPE,
    KeyName=KEY_NAME,
    SecurityGroupIds=[SECURITY_GROUP_ID],
    MinCount=1,
    MaxCount=1,
)

# Print the new instance ID
instance_id = response['Instances'][0]['InstanceId']
print(f"New instance created: {instance_id}")

Make sure to replace the values of AMI, KEY_NAME, SECURITY_GROUP_ID, and REGION with your own values.

Then, simply run the script using the following command:

python aws_setup.py

This will create a new EC2 instance in the specified region with the specified configuration.

written by ChatGPT