Creating AWS EC2 Instance - Manual & Terraform

Creating AWS EC2 Instance - Manual & Terraform

AWS EC2 Instance - Overview, Types, and Use Cases

Introduction

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides scalable computing capacity in the cloud. It allows users to rent virtual computers to run their own applications. These virtual computers are known as instances and are designed to be highly configurable and flexible. Amazon EC2 is a core component of Amazon Web Services (AWS) and is widely used for hosting websites, running applications, and managing large-scale data processing.

EC2 Instance

An EC2 instance is a virtual server running in the cloud. It is essentially a virtual machine that can be configured to run specific software applications and services. Each instance is assigned a specific amount of CPU, memory, and storage resources, and users can choose the configuration that best suits their needs. Instances are billed based on the amount of time they are running, and users can start and stop instances as needed.

Types of EC2 Instance

Amazon EC2 offers a variety of instance types optimized for different use cases. Here are some of the most common types of instances:

  1. General-purpose instances: These instances are ideal for a wide range of workloads, including web servers, small and medium databases, and development environments.

  2. Compute-optimized instances: These instances are designed for compute-intensive workloads that require high-performance processors. They are ideal for scientific modeling, batch processing, and video encoding.

  3. Memory-optimized instances: These instances are optimized for workloads that require high memory capacity. They are ideal for running large-scale, in-memory databases, such as SAP HANA, Apache Spark, and Cassandra.

  4. Storage-optimized instances: These instances are optimized for workloads that require high storage capacity and high disk throughput. They are ideal for running big data applications, such as Hadoop and data warehousing.

Use of EC2 Instance

EC2 instances can be used for a variety of purposes, including:

  1. Hosting websites and web applications

  2. Running backend services and APIs

  3. Running software development environments

  4. Running big data applications

  5. Running machine learning workloads

  6. Running gaming servers

  7. Running desktop applications in the cloud

Conclusion

Amazon EC2 provides users with a flexible, scalable, and cost-effective way to run applications and services in the cloud. With a wide variety of instance types to choose from, users can select the configuration that best suits their needs. Whether hosting a website, running a database, or processing big data, EC2 instances offer a powerful and reliable platform for running workloads in the cloud.

EC2 Instance Creation - UI

This documentation will guide you through the process of creating an Amazon EC2 instance using the user interface (UI) provided by the AWS Management Console. Amazon EC2 is a web service that allows you to create and manage virtual servers in the cloud. The UI provides a graphical interface to simplify the process of setting up and configuring your EC2 instances.

Prerequisites

Before you begin, make sure you have the following:

  1. An AWS account: You need an active AWS account to access the AWS Management Console.

  2. IAM permissions: Ensure that your IAM user or role has the necessary permissions to create and manage EC2 instances. The required permissions include ec2:RunInstances, ec2:DescribeImages, ec2:CreateTags, and others depending on your specific requirements.

Step 1: Access the AWS Management Console

  1. Open a web browser and go to the AWS Management Console at https://console.aws.amazon.com/.

  2. Enter your AWS account credentials (email address and password) and click on the "Sign In" button.

Step 2: Navigate to the EC2 Dashboard

  1. After successfully signing in, you will be redirected to the AWS Management Console homepage.

  2. In the search bar at the top, type "EC2" and click on the "Amazon EC2" option that appears in the results. Alternatively, you can find the EC2 service by navigating through the "Services" menu at the top.

Step 3: Launch an Instance

  1. On the EC2 Dashboard, click on the "Launch Instance" button to start the instance creation process.

  1. The first step is to choose an Amazon Machine Image (AMI) that will serve as the template for your instance. You can select from a variety of pre-configured AMIs provided by AWS or use your own custom AMI. You can filter the AMI list based on various criteria like region, operating system, or other specific requirements. Once you've selected an AMI, click on the "Select" button.

  2. The next step is to choose an instance type. Amazon EC2 provides a range of instance types optimized for different workloads. You can select an instance type based on your specific requirements, such as CPU, memory, storage, and networking capabilities. After selecting an instance type, click on the "Next: Configure Instance Details" button.

  3. In the "Configure Instance Details" section, you can specify various settings for your instance, such as the number of instances to launch, network settings, IAM role, and more. Make the necessary configurations according to your needs. When you are finished, click on the "Next: Add Storage" button.

  4. On the "Add Storage" page, you can configure the storage options for your instance. You can add, remove, or modify the storage volumes attached to your instance. You can also specify the size, type, and other settings for each volume. After configuring the storage, click on the "Next: Add Tags" button.

  5. The "Add Tags" page allows you to add metadata to your instance in the form of tags. Tags are key-value pairs that help you organize and categorize your AWS resources. You can add tags for purposes such as cost allocation, resource management, or identification. Click on the "Add Tag" button to define tags for your instance. Once you have added the desired tags, click on the "Next: Configure Security Group" button.

  6. On the "Configure Security Group" page, you can define the network security rules for your instance. A security group acts as a virtual firewall that controls inbound and outbound traffic. You can create a new security group or select an existing one. Specify the rules to allow or restrict incoming connections based on protocols, ports, and IP ranges. After configuring the security group, click on the "Review and Launch" button.

  7. Review the instance configuration on the "Review Instance Launch" page. Double-check all the settings you've made so far to ensure they are correct. If you need to make any changes, click on the corresponding sections on the left side of the page. Once you are satisfied with the configuration, click on the "Launch" button.

  8. A dialog box will appear asking you to select or create a key pair. A key pair consists of a public key and a private key and is required to securely access your EC2 instance. You can either select an existing key pair or create a new one. If you create a new key pair, make sure to download the private key file (.pem) and store it in a secure location. After making your selection, click on the "Launch Instances" button.

  1. The instance creation process will now begin, and you will be redirected to the "Instances" view. Here, you can monitor the progress of the instance launch. Once the status of your instance changes to "running," it means the instance is ready to use.

Step 4: Access and Manage Your Instance

  1. To access your newly created EC2 instance, select it from the list of instances on the "Instances" view.

  2. In the bottom panel, you will find information about the instance, including its public IP address or DNS name.

  3. Depending on the operating system of your instance, you can connect to it using various remote access methods such as SSH (for Linux) or RDP (for Windows). Use your preferred remote access tool and provide the necessary credentials and connection details to log in to the instance.

Conclusion

Congratulations! You have successfully created an Amazon EC2 instance using the AWS Management Console's user interface. You can now customize and manage your instance based on your requirements. Remember to monitor your instances regularly, manage security groups, and optimize resource usage to ensure the efficient operation of your EC2 infrastructure.

Connecting EC2 Instance - Manually

Step 1: Log in to the AWS Management Console

  • Open your web browser and go to the AWS Management Console website (https://console.aws.amazon.com).

  • Enter your AWS account credentials (username and password) and click "Sign In".

Step 2: Navigate to the EC2 service

  • After logging in, you'll see a dashboard with various AWS services. Look for the "EC2" service and click on it. This will take you to the EC2 management console.

Step 3: Locate your EC2 instance

  • In the EC2 management console, you'll see a list of your EC2 instances. Find the instance you want to connect to and select it by clicking on the checkbox next to its name.

Step 4: Access the instance through the web-based console

  • With your instance selected, look for the "Connect" button in the top navigation bar and click on it.

  • In the dropdown menu that appears, select the "EC2 Instance Connect (browser-based SSH connection)" option.

Step 5: Start the browser-based SSH session

  • A new browser tab or window will open, showing the browser-based SSH session interface.

  • If prompted, review and accept the end-user license agreement.

  • If your instance has multiple users, select the user account you want to use for the SSH session.

Step 6: Connect to the EC2 instance

  • Once you've selected the user account, you'll be connected to the EC2 instance through the browser-based SSH session.

  • You can now use the command line interface (CLI) directly within the browser window to interact with your instance.

That's it! You have successfully connected to your EC2 instance using the AWS Management Console's web-based SSH session. You can now run commands, configure settings, and manage your instance as needed.

Remember to properly secure your instance by regularly updating your SSH keys, using secure passwords, and following AWS security best practices.

Creating EC2 Instance through Terraform

Introduction

This documentation provides step-by-step instructions on how to use Terraform to create an EC2 (Elastic Compute Cloud) instance on Amazon Web Services (AWS). Terraform is an open-source infrastructure-as-code tool that allows you to define and provision infrastructure resources using declarative configuration files.

Prerequisites

Before proceeding, ensure that you have the following:

  1. An AWS account with appropriate permissions to create EC2 instances.

  2. Terraform installed on your local machine. You can download and install Terraform from the official website: https://www.terraform.io/downloads.html.

Step 1: Configure AWS Credentials

To authenticate with AWS, you need to provide your AWS access key and secret access key. Set these values as environment variables or use an AWS credentials file.

If you choose to set environment variables, use the following commands in your terminal

Alternatively, you can create an AWS credentials file (~/.aws/credentials) and add the following content:

Replace your-access-key and your-secret-access-key with your actual AWS credentials.

Step 2: Create a Terraform Configuration File

Create a new directory for your Terraform project and navigate into it:

Create a new file named main.tf using your preferred text editor, and add the following content:

In the above configuration, we specify the AWS provider, the region to create the instance in, and the resource definition for an EC2 instance. We specify the AMI ID (Amazon Machine Image) and the instance type for the EC2 instance. Feel free to change these values as per your requirements.

Step 3: Initialize Terraform

Initialize Terraform in your project directory by running the following command:

This command downloads the necessary provider plugins and sets up the working directory.

Step 4: Preview and Validate the Changes

Before creating the EC2 instance, you can preview the execution plan to see the resources that Terraform will create. Run the following command:

Terraform will analyze the configuration files and display a summary of the changes it will make. Ensure that the output matches your expectations and that there are no errors.

Step 5: Apply the Changes

To create the EC2 instance, run the following command:

Terraform will prompt you to confirm the action. Type yes and press Enter to proceed. The command will take a few moments to provision the resources.

Once the operation is complete, Terraform will display the output, including the public IP address of the newly created EC2 instance.

Step 6: Verify the EC2 Instance

You can verify the creation of the EC2 instance by logging into the AWS Management Console or by using the AWS CLI.

Here are the steps to verify the creation of the EC2 instance:

AWS Management Console:

  1. Open the AWS Management Console in your web browser.

  2. Navigate to the EC2 service.

  3. Click on "Instances" in the left navigation menu.

  4. You should see a list of EC2 instances. Look for the instance with the name "my-ec2-instance" (or the name you specified in the Terraform configuration).

  5. Verify that the instance is in the "running" state and that the instance details match the configuration you provided.

AWS CLI:

  1. Open your terminal or command prompt.

  2. Ensure that you have the AWS CLI installed and configured with your AWS credentials.

  3. Run the following command to list all EC2 instances:

  1. Look for the instance with the name "my-ec2-instance" (or the name you specified in the Terraform configuration) in the output. Verify that the instance is in the "running" state and that the instance details match the configuration you provided.

Step 7: Destroy the Resources

If you want to remove the created EC2 instance and associated resources, you can use the following command:

Terraform will prompt you to confirm the destruction of the resources. Type yes and press Enter to proceed. The command will take a few moments to destroy the resources.

Note: Be cautious when using the terraform destroy command, as it permanently removes the resources created by Terraform.

Conclusion

Congratulations! You have successfully created an EC2 instance using Terraform. This documentation provided a step-by-step guide on how to configure your AWS credentials, create a Terraform configuration file, initialize Terraform, preview and validate changes, apply the changes to create the EC2 instance, and verify the creation of the instance. Additionally, it explained how to destroy the created resources if needed.

For detailed course on terraform please visit:

infrasity.com/p/devops-fundamentals-build-a..

Did you find this article valuable?

Support Infrasity Blog by becoming a sponsor. Any amount is appreciated!