Skip to content

Latest commit

 

History

History
88 lines (48 loc) · 2.87 KB

EC2_AWS_BOTO3.md

File metadata and controls

88 lines (48 loc) · 2.87 KB

Certainly! Here's a basic example of how you can use boto3 to create an EC2 instance in AWS using Python. This script will launch an EC2 instance with specified parameters.

Prerequisites:

    1. AWS Credentials: Make sure you have configured your AWS credentials. You can set up your credentials using the AWS CLI (aws configure) or by setting environment variables.
    1. Boto3 Library: Install the boto3 library if you haven’t already. You can install it using pip:
pip install boto3

Python Script to Create an EC2 Instance

import boto3

def create_ec2_instance():
    # Create a session using your AWS credentials and region
    session = boto3.Session(
        aws_access_key_id='YOUR_ACCESS_KEY',        # Replace with your AWS access key
        aws_secret_access_key='YOUR_SECRET_KEY',    # Replace with your AWS secret key
        region_name='YOUR_REGION'                   # Replace with your desired AWS region (e.g., 'us-west-2')
    )

    # Create an EC2 resource object
    ec2 = session.resource('ec2')

    # Define the instance parameters
    instance = ec2.create_instances(
        ImageId='ami-0abcdef1234567890',  # Replace with the ID of the AMI you want to use
        InstanceType='t2.micro',          # Replace with the desired instance type
        MinCount=1,                       # Minimum number of instances to launch
        MaxCount=1,                       # Maximum number of instances to launch
        KeyName='your-key-pair',          # Replace with the name of your key pair
        SecurityGroupIds=['sg-0abc1234'], # Replace with your security group ID
        SubnetId='subnet-0abc1234',       # Replace with your subnet ID (optional)
    )

    # Print the instance ID of the created instance
    print("Created instance with ID:", instance[0].id)

if __name__ == "__main__":
    create_ec2_instance()

Explanation:

    1. Session Creation: Creates a boto3 session with AWS credentials and region. Replace placeholders with your actual credentials and region.
    1. EC2 Resource Object: Obtains an EC2 resource object to interact with the EC2 service.
    1. Instance Creation: create_instances() method is used to launch the EC2 instance with specified parameters:
  • ImageId: The ID of the Amazon Machine Image (AMI) to use for the instance.

  • InstanceType: The type of instance to create (e.g., t2.micro).

  • MinCount and MaxCount: Specifies how many instances to launch.

  • KeyName: The name of the key pair for SSH access.

  • SecurityGroupIds: List of security group IDs to associate with the instance.

  • SubnetId: Optional; specifies the subnet in which to launch the instance.

  • Print Instance ID: Outputs the ID of the newly created instance.

Note:

Ensure that the specified AMI ID, key pair, and security group IDs exist in your AWS account. Replace placeholders with appropriate values as per your AWS setup.