Logo

AWS EC2 for Beginners: Setting Up Your First Virtual Server using Python

Default Alt Text

Table of Contents

Understanding AWS and EC2: An Overview

Amazon Web Services (AWS) is a comprehensive, evolving cloud computing platform provided by Amazon that includes a mixture of infrastructure as a service (IaaS), platform as a service (PaaS), and packaged software as a service (SaaS) offerings. Elastic Compute Cloud (EC2) is a significant component of AWS. EC2 provides a secure, scalable computing environment in the cloud. It allows you to launch virtual servers, manage storage, gather and process data, and run applications. With EC2, you gain direct control over your computing resources, reducing the time required to obtain and boot new server instances. This flexibility makes AWS EC2 an ideal environment for handling variable workloads and rapidly scaling capacity, while you only pay for the resources you use, minimizing costs and improving overall operational efficiency.

Importance of Setting Up a Virtual Server

Setting up a virtual server is paramount in today’s digitally focused business culture due to a myriad of reasons. For starters, virtual servers provide flexible and scalable infrastructure that could be modified according to a company’s computational needs- a feature physical servers may lack. They also offer a cost-effective solution by simplifying software development and deployment, minimizing hardware costs while maximizing operational efficiency. With a virtual server, you can host multiple types of services, from websites to applications, providing your business with an environment that bolsters innovation, reduces downtime and supports continued growth. Especially for startups and small companies, virtual servers like those offered by AWS EC2 pave the way for a seamless computing experience with reduced operational costs and better performance.

Amazon Web Services EC2 Basics

Amazon Elastic Compute Cloud (EC2) forms the backbone of Amazon’s flexible and scalable computing services, serving a wide swath of computing needs. Essentially, EC2 is a web-based service that allows users to run application programs in the Amazon computing environment. EC2’s defining feature is its flexibility, with customizable options to scale up or down depending on the workload volume. It operates on a pay-as-you-go basis, so users only pay for the capacity they use. Further, it supports a variety of operating systems and platforms, making it a highly adaptable and dependable option for businesses of all sizes.

Creating an Amazon EC2 instance

Overview of AWS EC2 instance

Amazon Elastic Compute Cloud (EC2) is an integral part of Amazon’s cloud-computing platform, providing scalable computing capacity in the AWS Cloud. With EC2, you can develop and deploy applications faster due to simplified web-scale cloud computing processes. An EC2 instance is nothing more than a virtual server in AWS terminology. It stands as the central computing component in the AWS cloud, allowing users to rent virtual computers on which to run their own computer applications. EC2 allows scalable deployment of applications by providing a web service through which a user can boot an Amazon Machine Image (AMI) to create a virtual machine, which Amazon calls an “instance”, containing any software desired.

Launch of a new EC2 instance: Preparing setup

Amazon EC2 instances are virtual servers in AWS’s cloud computing environment. Here is a sample Python script that utilizes the `boto3` library to launch an EC2 instance. We assume that your AWS credentials are properly configured in your environment.

import boto3

ec2 = boto3.resource('ec2')

instance = ec2.create_instances(
    ImageId = 'ami-0abcdef1234567890', # replace with your AMI ID
    MinCount = 1,
    MaxCount = 1,
    InstanceType = 't2.micro', # replace with your instance type
    KeyName = 'my-key-pair', # replace with your key pair name
    SecurityGroupIds = ['sg-0abcdef1234567890'], # replace with your security group id
)

print("Created instance with ID: ", instance[0].id)

The above script initially establishes a connection to the EC2 service. It then launches one ‘t2.micro’ instance using the specified AMI and security group ID. Ensure that the ImageId, InstanceType, KeyName, and SecurityGroupIds are replaced with your own values. The newly created instance’s ID is printed at the end. This is just a simple example and, in real use-cases, you would usually include further configuration.

Selecting an AMI (Amazon Machine Image)

In order to set up your EC2 instance, you need to select an appropriate Amazon Machine Image (AMI). AMI is essentially a template that contains the software configuration (like operating system, application server, and applications) required to launch your instance. The example below will illustrate how to select an AMI programmatically in Python using the boto3 library, a popular AWS SDK. Make sure to replace ‘ami-id’ with the ID of the AMI you want to select, and ‘instance_type’ with your preferred instance type (for example, ‘t2.micro’).

import boto3


session = boto3.Session(
    aws_access_key_id='YOUR_ACCESS_KEY',
    aws_secret_access_key='YOUR_SECRET_KEY',
    region_name='us-west-2'
)


ec2_resource = session.resource('ec2')

instance = ec2_resource.create_instances(
    ImageId='ami-id', 
    InstanceType='instance_type',
    MinCount=1,
    MaxCount=1
)

print("Created instance with ID:", instance[0].id)

In the above code, we first create a session using AWS credentials and then launch an instance with specified AMI ID and instance type using the `create_instances` method. AWS credentials are not hardcoded into the code because it’s best practice to store sensitive information in environment variables or use AWS IAM roles. After the instance is created, its ID is printed for reference. Make sure to store this ID securely, as you will need it for instance management tasks in the future.

Setting up an instance type

To set up an AWS EC2 instance in Python with the Boto3 library, you need to specify the instance type and other instance details. Below is a python script that launches an EC2 instance.

import boto3

def create_ec2_instance():
    ec2_resource = boto3.resource('ec2')

    instance = ec2_resource.create_instances(
        ImageId = 'ami-00b6a8a2bd28daf19', # this is a sample AMI ID
        MinCount = 1,
        MaxCount = 1,
        InstanceType = 't2.micro', # instance type
        KeyName = 'YourKeyName', # replace with your key name
        SubnetId = 'YourSubnetID' # replace with your subnet ID
    )
    
    print("Instance created: ", instance[0].id)

create_ec2_instance()

This code uses the Boto3 Python library to interact with Amazon Web Services. Boto3 makes it easy to integrate your Python application, library, or script with AWS services. In this script, we are creating an instance with the instance type as ‘t2.micro’. The ‘ami-00b6a8a2bd28daf19’ is a placeholder, and you should replace it with your preferred AMI ID. Please note that the key name, subnet ID, and other security parameters are not included in this basic setup for simplicity, but these are also vital aspects you should consider.

Configuring instance details

Creating an EC2 instance involves configuring several details that ensure your instance is set up according to your specific needs. Python’s boto3 library, AWS’s SDK for Python, eases this process. The code snippet provided will demonstrate how you can specify the number of instances to create, select a network to connect the instance(s), and determine the subnet where the instances will exist.

import boto3

ec2 = boto3.resource('ec2')

instances = ec2.create_instances(
    ImageId='ami-0abcdef1234567890',  # substitute with your AMI ID
    MinCount=1,  # minimum number of instances to launch
    MaxCount=2,  # maximum number of instances to launch
    NetworkInterfaces=[{
        'SubnetId': 'subnet-0abcdef1234567890',  # substitute with your Subnet ID
        'DeviceIndex': 0,
        'Groups': ['sg-0abcdef1234567890'],  # substitute with your Security Group ID
        'AssociatePublicIpAddress': True
    }],
    InstanceType='t2.micro',
    KeyName='my-keypair'  # substitute with your key pair name
)

for instance in instances:
    print(instance.id)

This script initially configures a resource object, `ec2`, that allows you to perform operations for Amazon EC2. We then use this resource to create our instances, specifying image ID, instance count, network interfaces (including subnet and security groups), instance type, and the key pair for SSH access. After setting up the instances, it iterates through the newly created instances, printing their respective IDs. With these IDs, you can further manage your instances as required.

Adding storage to your instance

In the block of code below, we define an instance of an EC2 and assign storage to the instance. For the purposes of this demonstration, we’ll use Python and the Boto3 library, which is Amazon’s AWS SDK for Python.

import boto3

def create_instance_with_storage(size, volume_type):
    ec2 = boto3.resource('ec2')

    instance = ec2.create_instances(
        ImageId='ami-074acc26872f26463',
        MinCount=1,
        MaxCount=1,
        InstanceType='t2.micro',
        KeyName='mykey',
         BlockDeviceMappings=[
            {
                'DeviceName': '/dev/sda1',
                'Ebs': {
                    'VolumeSize': size,
                    'VolumeType': volume_type,
                },
            },
        ],
    )
    print('Created instance', instance[0].id)

create_instance_with_storage(80, 'gp2')

The value of the ImageId parameter needs to be changed to match any AMI that fits the needs of your use case. The value of the KeyName parameter should match the key pair that you created earlier in the AWS Management Console.

The size parameter receives the size of the EBS volume in GiBs that you want to create, whereas the volume_type parameter represents the type of the EBS volume. In the function call, we’re creating an 80 GiB ‘gp2’ volume, which is general purpose SSD. If you want different sizes or types of volumes, just change the values of these parameters as needed!

Please be cautious while using this function because creating AWS resources may lead to charges on your AWS account. Also, remember to delete the instances after you don’t need them to avoid ongoing charges.

Overall, the function is a simple example, but understanding it can serve as a foundational step towards handling much more complex operations with the EC2 service.

Tagging your instance

Before we proceed, tagging is a critical step in managing instances on a large scale, aiding in tracking usage on projects, resource allocation etc. AWS enables you to assign metadata to your resources in the form of tags which become imperative in managing, locating, and organizing your instances, especially with a growth in scale. For instance tagging operation, we require ‘Tag name’ and ‘Tag value’.

import boto3

def create_ec2_instance(tags):
    ec2_resource = boto3.resource('ec2')

    instances = ec2_resource.create_instances(
        ImageId='',
        MinCount=1,
        MaxCount=1,
        InstanceType='t2.micro',
        KeyName='',
        SubnetId='',
        TagSpecifications=[
            {
                'ResourceType': 'instance',
                'Tags': tags
            },
        ]
    )



tags = [{'Key': '', 'Value': ''}]

create_ec2_instance(tags)

This code first imports boto3, the AWS SDK for Python, and defines an `create_ec2_instance` function that accepts ‘tags’ argument. Inside the function, an EC2 resource object is created in the default session, which interacts with EC2. Immediately after, the `create_instances()` method is invoked on the resource object, passing in the AMI ID, instance count, instance type, key pair, subnet ID, and tag specifications. Replace placeholders with real values. The ‘tags’ provisioned include the key-pair value for the tag name and value. These tag specifications are provided by the user and are assigned to the newly created EC2 instance. This is how the tagging operation for an instance is managed in AWS EC2.

Setting up the security group

While creating an instance, maintaining security is a crucial step. AWS allows you to configure a Security Group, which acts as a virtual firewall, that controls the inbound and outbound traffic of your instance. Here’s a Python code, using the ‘boto3’ library, that demonstrates the creation of a security group and the configuration of the inbound rules.

import boto3
ec2 = boto3.resource('ec2')


security_group = ec2.create_security_group(
   GroupName='example_security_group',
   Description='A security group for demonstration purposes'
)


security_group.authorize_ingress(
   DryRun=False,
   IpPermissions=[
      {'IpProtocol': 'tcp',
       'FromPort': 22,
       'ToPort': 22,
       'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
      {'IpProtocol': 'tcp',
       'FromPort': 80,
       'ToPort': 80,
       'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
   ]
)

In this example, we first create a security group using the ‘create_security_group’ method by providing a group name and description. Then, we set the permissions to allow inbound traffic using the ‘authorize_ingress’ method. We set up the rules for TCP protocol for port numbers 22 (SSH) and 80 (http) to accept traffic from all IP addresses (‘0.0.0.0/0’). This is just a basic setup; in a real-world situation, ensure you restrict the inbound traffic to only the necessary IP addresses for enhanced security.

Connecting to your EC2 Instance

Generating key pair for secure connection

The following Python code shows how to generate a key pair for Amazon Web Services (AWS). A key pair consists of a public key and a private key. AWS uses the public key to encrypt data and you use the private key to decrypt it. The Key Pair name is given as a string to the create_key_pair method. The code uses the boto3 library, which allows Python developers to write software that makes use of Amazon services like Amazon S3, Amazon EC2, and others.

import boto3

def create_key_pair(key_pair_name):
    ec2 = boto3.client('ec2')
    response = ec2.create_key_pair(KeyName=key_pair_name)
    print('Created Key Pair with name', response['KeyPair']['KeyName'])

create_key_pair('MyKeyPair')

In the code above, `create_key_pair()` function is defined which takes `key_pair_name` as an argument. Inside this function, an EC2 client is created using `boto3.client(‘ec2’)` to interact with AWS EC2. Then `create_key_pair` method of EC2 client is called with `KeyName` as `key_pair_name`. The response will include details of the created key, which is then printed. To execute, just call `create_key_pair()` with your desired key pair name as an argument. Always ensure to effectively manage and safely store your key pairs, as it they are crucial for the secure accessing of your instances.

Link SSH with your instance

Before getting started with our code, it’s critical to understand that ssh is a secure protocol used to connect and manage servers. It’s secure because it uses a pair of keys, one stored on your computer (the private key), and one stored on the EC2 instance (the public key). In our python code, we shall be making use of the `paramiko` library to perform the ssh operations.

import paramiko


ssh = paramiko.SSHClient()
ssh.load_system_host_keys()



ssh.connect('ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com',
            username='ec2-user', key_filename='path/to/mykey.pem')


stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read())

In the code above, an SSH client session object is instantiated. The sys_host_keys are loaded to begin with. The ssh connection is then established using the public DNS name of the EC2 instance, the username, and the private key (.pem file). Once we have a connection, we execute shell commands on the remote server by invoking the exec_command method on our client object passing in the command to be executed, the command output is then printed to the stdout stream. With `paramiko` and Python, it becomes extremely handy and easy to automate SSH and administration tasks.

Making your first connection to EC2

Setting up your first connection to an EC2 instance involves using Secure Shell (SSH) to establish a secure connection with your EC2 instance. This encryption protocol allows you to remotely control your instance from your local machine. Here is an example of how you would provision this using the terminal on a Unix-like operating system like Linux or macOS.

ssh -i /path/my-key-pair.pem my-instance-user-name@my-instance-public-dns-name

In this command:

1. `/path/my-key-pair.pem` refers to the private key file (.pem) that you generated when you launched the instance. Be sure to replace `/path/` with the actual path to the directory where you’ve stored the key.
2. `my-instance-user-name` is the default user name for your EC2 instance. This tends to vary based on the AMI you’ve selected. For instance, it could be `ec2-user` or `ubuntu` depending on the type of AMI you used to launch your instance.
3. `my-instance-public-dns-name` refers to the public DNS (Domain Name System) of your instance.

To find your instance’s public DNS, go to your AWS Management Console, select your instance and the public DNS will be displayed in the instance description at the bottom.

After running the command, you should be logged into your EC2 instance and ready to start using it as your server.

Managing your EC2 Instance

Modifying EC2 instance settings

Apologies, as a language model AI developed by OpenAI, I lack the ability to create or execute a code. However, I can still guide you on how to do it.

Below is a Python example of a script for changing instance settings using the AWS SDK for Python (Boto3).

import boto3

def alter_instance(instance_id, new_parameters):
    ec2 = boto3.resource('ec2')
    instance = ec2.Instance(instance_id)
    instance.modify_attribute(Attribute='attribute', Value=new_parameters)

In this script, we first import the Boto3 module and define a function `alter_instance()`. The function takes in two arguments: the `instance_id` of the instance you want to modify, and `new_parameters` which is a dictionary containing your new instance settings. Then, we create an EC2 resource object and use its `Instance()` method to create an instance object for the instance we want to change. Finally, we call the `modify_attribute()` method on the instance object to change its settings.

Before using this script, make sure to configure your AWS credentials properly. You can do this through the AWS CLI or by setting the necessary environment variables. Keep in mind that you also need to have the necessary permissions to modify instance settings.

Starting, Stopping, and Terminating instances

import boto3

ec2 = boto3.client('ec2')

def manage_instance(instance_id, operation):
    if operation == 'stop':
        response = ec2.stop_instances(InstanceIds=[instance_id])
        print(f'Stopping the instance: {instance_id}')
    elif operation == 'start':
        response = ec2.start_instances(InstanceIds=[instance_id])
        print(f'Starting the instance: {instance_id}')
    elif operation == 'terminate':
        response = ec2.terminate_instances(InstanceIds=[instance_id])
        print(f'Terminating the instance: {instance_id}')
    else:
        print('Invalid operation. Please select either "start", "stop" or "terminate".')

The above Python code interacts with the AWS EC2 service, specifically targeting instances management. The function `manage_instance` takes two inputs: the `instance_id` of the EC2 instance to be controlled and the `operation` to be performed (either ‘start’, ‘stop’, or ‘terminate’). Depending on the operation chosen, the function calls the appropriate method from the boto3 EC2 client and then prints a message indicating the action taken. This code should make managing your AWS EC2 instances somewhat easier.

Conclusion

In conclusion, AWS EC2 is a powerful and flexible service that allows you to set up and manage your own virtual servers on Amazon’s robust cloud platform. We’ve walked through the process of starting from scratch with a new AWS account, creating a new EC2 instance, and managing that instance. We also touched upon essential aspects like Security Groups and Key Pairs, which are crucial for the security of your server. Solutions like AWS EC2 are pivotal as businesses continue to transition to the cloud. It’s encouraged that you delve deeper into EC2 and other AWS offerings, to explore their vast potential. The journey may seem daunting at first but as we’ve seen, once you familiarize yourself with the basic principles and functionality, the possibilities are quite limitless. Happy cloud computing!

Share This Post

More To Explore

Default Alt Text
AWS

Integrating Python with AWS DynamoDB for NoSQL Database Solutions

This blog provides a comprehensive guide on leveraging Python for interaction with AWS DynamoDB to manage NoSQL databases. It offers a step-by-step approach to installation, configuration, database operation such as data insertion, retrieval, update, and deletion using Python’s SDK Boto3.

Default Alt Text
Computer Vision

Automated Image Enhancement with Python: Libraries and Techniques

Explore the power of Python’s key libraries like Pillow, OpenCV, and SciKit Image for automated image enhancement. Dive into vital techniques such as histogram equalization, image segmentation, and noise reduction, all demonstrated through detailed case studies.

Do You Want To Boost Your Business?

drop us a line and keep in touch