What is AWS EC2
Amazon Elastic Compute Cloud (Amazon EC2) literally translates to a scalable computing cloud that provides a wide range of instance types. These instance types are optimized to suit different use cases. The instance types include CPU, memory, storage, and network capacity. Different combinations allow us to flexibly choose the appropriate combination of resources for the application. Each instance type includes one or more instance sizes, allowing you to scale resources to the needs of the target workload. Here is a list of EC2 instance types , and a cut of the image is shown here
To put it simply: EC2 is a virtual machine configured according to your needs. If you need to do CPU-intensive tasks on the virtual machine, choose a high-CPU and low-memory instance. If it is I/O-intensive, then Choose low-CPU and high-memory instances and spend the least money to provide services that best meet your needs
Install Apache service on EC2
With this basic understanding, we can install the Apache service on EC2 to learn more about EC2
Preparation
Since you have opened AWS free tier (binding credit card), you can play for 12 months for free. If you do the same, it is recommended to set the billing threshold . When you use the service that generates fees, you will be reminded to prevent over Multiple overhead
New EC2
Click the Launch Instance button in the figure below to create an EC2 instance
New EC2 instances mainly in accordance with the seven-step diagram above under completion, first select AMI (Amazon Machine Images), in fact, just select the operating system, marked Free tier eligible
that we can use for free, the following figure with a red border is marked The AMI packaged by Amazon will contain the configuration of ECS agent and Docker (these services will not be used in this demo). Other AMIs may need to be installed manually, so Amazon AMI is recommended if there is no special requirement
Click the Select button on the right to enter the next step
Select the system configuration, there is only one type of Free tier eligible (1 vCPU, 1GB Memory)
Continue to click Confiture Instance Details to configure the detailed content. The following figure illustrates some fields. In the case of demonstration, the default settings are mostly used
Without additional non-required
configuration, you can continue to click Add Storage to configure the storage area, the default will have 8GB of storage space, enough for us to install Apache service, so there is no additional add additional Volume
Continue to the next step Add Tags
The next step is Configure Security Group . The Security Group is actually like a firewall, divided into Inbound Rule (default deny all) and Outbound Rule (default allow all). The following figure configures two (SSH/HTTP) methods to ask EC2 instances. The former is used Subsequent installation of the Apache service, the latter is used for browser verification services, where the source is 0.0.0.0/0 (refers to any access source, this is a very loose rule, and this is not allowed in actual business)
From here, you can click Review and Launch to publish the EC2 instance
Check the configuration content. If there is no problem, finally click Launch . Before the final launch, you need to generate a key pair (download it to the local) for SSH access. If you have already generated it, you can also choose to use the existing one.
Finally, Launch Instances , you get an EC2 instance in the running state
As can be seen from the above figure, we are also assigned a Public IP 54.145.44.9
If we do not use Elastic IP, we will get a new Public IP every time we launch an EC2 service, so yours should be different from mine
Next, we can connect to EC2 with the Key Pair just now to install the Apache service
Connect to EC2
Click on the map Connect
button, there are three connections:
The first connection method gives you the complete steps as you create a github repo. By default, if you directly use the downloaded Key Pair to connect to EC2, you will get an error saying "File permissions are too large". So through chmod 400
connection to EC2 presentation given reasonable file permissions, then do it in Mac
Which
ec2-user
is the Amazon Linux 2 AMI default user (premise connection must be connected to configure the Inbound Rule Support of SSH mode Security Group)
If you don’t have command line tools or Putty by your side, you can also directly use the third method of connecting through a browser:
Click Connect , it will also enter EC2
As can be seen from the above figure, there are two IPs, one Public IP and one Private IP. If you connect via SSH, please use the former
The second connection method has no IAM configuration by default, so there is no way to connect through this method for the time being:
At this point, we can enter EC2 to install the Apache service
Install Apache service
Switch to super administrator status
sudo su
Do system update first
yum update -y
Install httpd
yum install -y httpd.x86_64
Start httpd service
systemctl start httpd.service
Enable httpd service
systemctl enable httpd.service
At this point, the Apache service has been installed
Test the Apache service
Test by curl naming directly on the command line
curl localhost:80
As can be seen from the figure below, the Apache service has been installed successfully
This is not particularly friendly, so visit again through the browser (via Public IP), you can see the Apache service test page friendly
Follow the test page instructions and change the test page content
echo "Hello AWS from $(hostname -f)" > /var/www/html/index.html
Refresh the browser page
The completed Apache service has been installed
Use EC2 User Data
It’s okay if you install one software after the EC2 instance is built. If you want to install multiple software and run on multiple EC2 instances, the above installation method is obviously unfriendly. AWS EC2 supports the use of User Data to solve our above Pain points
In fact, it is the shell script that needs to be executed when the EC2 instance is started for the first time
We can write all the steps of installing the Apache service above into a shell script
#!/bin/bash
sudo su
yum update -y
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello AWS from $(hostname -f)" > /var/www/html/index.html
With this script, we can add the User Data script content in the advanced options of the Configure Instance step of EC2 instance
The follow-up steps are the same as above, so I won’t repeat them here, it is much more convenient
Use EC2 AMI
If you create an EC2 instance, you need to enter a script. In case of a copy deviation, it will also cause some problems and changes. So AWS also supports the function of packaging EC2 into an image to solve this pain point (friends who are familiar with Docker should understand The role of creating Image) as shown below
Enter relevant information
An AMI is built like this
The AMI created by yourself can be viewed by clicking AMIs in the left menu
Later, you can choose to create it from your own AMI during Create Instance. Here, the introduction of the EC2 installation of Apache services is complete.
to sum up
I believe that through this article, you have a basic concept of EC2. By installing Apache services, using User Data, and finally using AMI, you will gradually enrich your understanding of the corresponding functions of EC2. The above functions are just The tip of the iceberg of EC2, in fact, he can combine EC2 Template to do AWS Batch Job work, and multiple instances to manage through ASG. Explain later
At last
After understanding the basic functions of EC2, stop or terminate the EC2 instance in time to prevent the overhead from being in the running state for a long time
Personal feelings
EC2 is a very mature service, with a variety of instance types to respond to various needs, while horizontal expansion and vertical expansion strategies can also calmly respond to various sudden increases.
Before using cloud services, you must consider infrastructure, network security, load balancing, health checks, and monitoring. The above settings must be combined through various components, and repeated debugging can achieve a relatively good balance. With the help of other services such as VPC, ALB, and CloudWatch, AWS EC2 can perfectly integrate the traditional construction process, greatly reducing bugs caused by system integration issues. No need to build any infrastructure, only simple configuration, while ensuring stability and high availability, greatly shortening an APP online cycle. At present, all the company's services have been released to EC2 instances in accordance with the corresponding requirements. A single management portal makes the management of multiple services more calm.