Introduction to Amazon EC2

Introduction to Amazon EC2

Introduction

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.

Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment. Amazon EC2 reduces the time required to obtain and boot new server instances to minutes, allowing you to quickly scale capacity, both up and down as your computing requirements change.

Amazon EC2 changes the economics of computing by allowing you to pay only for capacity that you actually use. Amazon EC2 provides developers the tools to build failure-resilient applications and isolate themselves from common failure scenarios.

Topics covered

  1. Launch a web server with termination protection enabled
  2. Monitoring Your EC2 instance
  3. Modifying the security group that your web server is using to allow HTTP access
  4. Resizing your Amazon EC2 instance to scale
  5. Test termination protection
  6. Terminating your EC2 instance

1. Launching your EC2 instance

In the AWS Management Console on the Services menu, choose EC2.

In the left navigation pane, choose EC2 Dashboard to ensure that you are on the dashboard page.

Choose Launch instance, and then select Launch instance.


Naming your EC2 instance

When you name your instance, AWS creates a key-value pair. The key for this pair is Name, and the value is the name you enter for your EC2 instance.

In the Name and tags pane, in the Name text box, enter the name of your EC2 instance.


Choosing an Amazon Machine Image (AMI)

An AMI provides the information required to launch an instance, which is a virtual server in the cloud. An AMI includes the following:

  • A template for the root volume for the instance (for example, an operating system or an application server with applications)
  • Launch permissions that control which AWS accounts can use the AMI to launch instances
  • A block device mapping that specifies the volumes to attach to the instance when it is launched

The Quick Start list contains the most commonly used AMIs. You can also create your own AMI or select an AMI from the AWS Marketplace, an online store where you can sell or buy software that runs on AWS.

Locate the Application and OS Images (Amazon Machine Image) pane.

Under AMI Machine Image (AMI), notice that the Amazon Linux 2 AMI image is selected by default. You can decide to keep this setting or choose your preferred.


Choosing an instance type

Amazon EC2 provides a wide selection of instance types optimized to fit different use cases. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications. Each instance type includes one or more instance sizes so that you can scale your resources to the requirements of your target workload.

For example, a t3.micro instance has 2 virtual CPUs and 1 GiB of memory.

Configuring a key pair

Amazon EC2 uses public-key cryptography to encrypt and decrypt login information. To log in to your instance, you must create a key pair, specify the name of the key pair when you launch the instance, and provide the private key when you connect to the instance.

It is an optional feature but mostly recommended.

Configuring the network settings

You use this pane to configure networking settings.

The VPC indicates which virtual private cloud (VPC) you want to launch the instance into. You can have multiple VPCs, including different ones for development, testing, and production.

In the Network settings pane, choose Edit

Here you can configure the VPC and Security Group.

In the security group, you give the name and the description.

A security group acts as a virtual firewall that controls the traffic for one or more instances. When you launch an instance, you associate one or more security groups with the instance. You add rules to each security group that allow traffic to or from its associated instances. You can modify the rules for a security group at any time; the new rules are automatically applied to all instances that are associated with the security group.

Adding storage

Amazon EC2 stores data on a network-attached virtual disk called Amazon Elastic Block Store (Amazon EBS).

You launch the EC2 instance using a default 8 GiB disk volume. This is your root volume (also known as a boot volume).

You can do this in the Configure Storage pane.

Configuring advanced details

In the Advanced details pane, you can set advanced features like termination protection, among others.

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance. These commands can be used to perform common automated configuration tasks and even run scripts after the instance starts.

For example, in the User Data text box, you can define your scripts that will run when the instance launches.

#!/bin/bash
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo '<html><h1>Hello From Your Web Server!</h1></html>' > /var/www/html/index.html        


In this case, the script does the following:

  • Install an Apache web server (httpd)
  • Configure the web server to automatically start on boot
  • Activate the Web server
  • Create a simple web page


Launching an EC2 instance

Now that you have configured your EC2 instance settings, it is time to launch your instance.

In the right pane, choose Launch instance

Choose View all instances

The instance appears in a Pending state, which means it is being launched. It then changes to Running, which indicates that the instance has started booting. There will be a short time before you can access the instance.

The instance receives a public DNS name that you can use to contact the instance from the Internet.


2. Monitor Your Instance

Monitoring is an important part of maintaining the reliability, availability, and performance of your Amazon Elastic Compute Cloud (Amazon EC2) instances and your AWS solutions.

Select the instance by checking the box next to the instance and navigate to the bottom of the screen to the Status checks tab.


With instance status monitoring, you can quickly determine whether Amazon EC2 has detected any problems that might prevent your instances from running applications. Amazon EC2 performs automated checks on every running EC2 instance to identify hardware and software issues.


Select the Monitoring tab.

This tab displays Amazon CloudWatch metrics for your instance.

You can choose a graph to see an expanded view.


Amazon EC2 sends metrics to Amazon CloudWatch for your EC2 instances. Basic (five-minute) monitoring is enabled by default. You can enable detailed (one-minute) monitoring.

In the Actions menu, select Monitor and troubleshoot Get Instance Screenshot.

This shows you what your Amazon EC2 instance console would look like if a screen were attached to it.


If you are unable to reach your instance via SSH or RDP, you can capture a screenshot of your instance and view it as an image. This provides visibility as to the status of the instance, and allows for quicker troubleshooting.


3. Update Your Security Group and Access the Web Server

When we launched the EC2 instance, we provided a script that installed a web server and created a simple web page. In this section, we will access content from the web server.

When we select the instance by checking the box, select the Details tab, and copy the Public IPv4 address of our instance to the clipboard.

Open a new tab in our web browser, paste the IP address we just copied, and then press Enter.

We are not currently able to access our web server because the security group is not permitting inbound traffic on port 80, which is used for HTTP web requests. This is a demonstration of using a security group as a firewall to restrict the network traffic that is allowed in and out of an instance.

To correct this, we will now update the security group to permit web traffic on port 80.

  • In the EC2 Management Console tab, in the left navigation pane, select Security Groups located under Network & Security.
  • Select Web Server security group. The Web in this case is the name we gave as our security group name.
  • Select the Inbound rules tab.
  • The security group currently has no rules.
  • Select Edit inbound rules then select Add rule and configure the rule with the following settings:
  • Type: HTTP
  • Source: Anywhere-IPv4
  • Select Save rules

When we return to the web server tab that we previously opened and refresh the page.

We should see the message Hello From Your Web Server!

We can add, edit, or delete rules using the Edit inbound rules menu.


4. Resize Your Instance: Instance Type and EBS Volume

As your needs change, you might find that your instance is over-utilized (too small) or under-utilized (too large). If so, you can change the instance type. For example, if a t3.micro instance is too small for its workload, you can change it to an m5.medium instance. Similarly, you can change the size of a disk.

Stop Your Instance

Before you can resize an instance, you must stop it.

  • On the EC2 Management Console, in the left navigation pane, select Instances.
  • Web Server (the name of our instance) should already be selected.
  • Select Instance state > Stop instance.
  • Select Stop
  • Your instance will perform a normal shutdown and then will stop running.
  • Wait for the Instance State to display: stopped


Change The Instance Type

In the Actions menu, select Instance Settings > Change Instance Type, then configure to the best of your choice.

Then select Apply

When the instance is started again, it will be of the new instance type you have configured above.

Resize the EBS Volume

  • In the left navigation menu, select Volumes located under Elastic Block Store.
  • Select the volume by checking the box, and navigate to the Actions menu, select Modify Volume.
  • Change the volume to your most suitable size.
  • Select Modify
  • Select Modify to confirm and increase or decrease the size of the volume.

Start the Resized Instance

You will now start the instance again, which will now have more or less memory and more or less disk space.


5. Termination Protection

You can delete your instance when you no longer need it. This is referred to as terminating your instance. You cannot connect to or restart an instance after it has been terminated.

  • Select your instance
  • In the Actions menu, select Instance settings > Change termination protection.
  • Check Enable followed by Save

When you now select your instance and then navigate to the top and select Instance state menu, select Terminate instance, There is a message that says: On an EBS-backed instance, the default action is for the root EBS volume to be deleted when the instance is terminated. Storage on any local drives will be lost. It will ask if you are sure that you want to terminate the instance. You will be able to select the Terminate button.

You will notice that the instance did not terminate and a red error message pops up at the top that says: Failed to terminate an instance: The instance may not be terminated. This is because it has termination protection enabled.



要查看或添加评论,请登录

STEVEN ODHIAMBO的更多文章

社区洞察

其他会员也浏览了