RSS Feed Subscribe to RSS Feed

 

HelloWorld on an AWS EC2 instance

This is a basic “HelloWorld” tutorial for AWS EC2. We create an EC2 instance, enable the required access, and install Apache to serve a trivial HelloWorld html file.

 

This by no means a step-by-step tutorial, but I’ve found it useful to write this out as a prep for my upcoming AWS Solutions Architect Associate exam.

1. Create an EC2 instance

Select all defaults. This will be create an EC2 instance in a default subnet in the default VPC, with a default Security Group and a default NACL. See the full list of default components that get created with a default VPC here: Default VPC Components

Look at the details of your newly created EC2 instance in the EC2 dashboard to get the public IP address (or query the instance metadata).

At this point, if you try to ssh in to your instance, it will not work.

This is because we are using a default Security Group, which deny’s all inbound traffic by default (although all outbound traffic is allowed by default).

So, we need create a new security group that allows ssh access so that we can set up our web server. We will also need HTTP/HTTPS access for when the web server is up and running, so we will add that too…

 

2. Create a custom Security Group

In the AWS console, go to

EC2 -> Security Group -> Create Security Group

Call it “WebSecurityGroup” (or whatever you choose)

Add a new rule to allow SSH. Specify the source as “My IP” (or CIDR block 0.0.0.0/0 if you’re OK with the insecurity of allowing access from anywhere!).

Also add HTTP and HTTPS.

Now with the new Security Group created,

Go to EC2 -> select you instance -> right click -> Networking -> Change Security Group

Deselect the default security group

Select your new WebSecurityGroup

These changes should take effect immediately.

3. Install Apache with a simple html file

a. SSH into your EC2 instance

e.g. ssh ec2-user@<your EC2 IP> -i yourkey.pem

(See the note at the very end on what “ec2-user” is)

b. Elevate privleges to root

$ sudo su

c. Install apache

yum install httpd -y

d. Ensure that all of software packages are up to date

$ yum update -y

e. Create HelloWord html

$ echo ‘Hello, World’ >/var/www/html/index.html

f. Start the web server

$ service httpd start

g. Finally, optionally, make sure the webserver always starts at startup

$ chkconfig httpd on

 

5. Access your running web server – HelloWorld!

If you try to access our server now

e.g., at http://<yourEC2ip>, you should be able to see our incredibly awesome “Hello, World”.

 

That’s it. We have a simple EC2 instance (running in a default VPC with a default NACL), running a web server, accessed via a custom Security Group that permits public access over HTTP(S).

 

Final note:

What is ec2-user?

Each Linux instance type launches with a default Linux system user account. For Amazon Linux, the user name is ec2-user. (Others are different, e.g., the user in Centos is “centos”).

Note that Linux system users should not be confused with AWS Identity and Access Management (IAM) users. In other words, you won’t see the ec2-user in IAM.

 

Tags: , ,

Leave a Reply