When you launch an EC2 instance, you can use the user-data to bootstrap a instance creation script.

We can use the following code to start a web server and echo out it’s instance id:

1
2
3
4
5
6
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

Steps to create a new bootstrapped instance

Step 1 – Launch a new instance

Login to your EC2 console and click Launch instances.

Select an Amazon Machine Image (AMI). We will select Amazon Linux 2.

Choose an instance type from the list. We will use a Free Tier Eligible t2.micro. Click Next.

Keep all the options as default and scroll to the bottom of the page. In the User Data textfield, enter the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html
```<figure class="wp-block-image size-large">

<img decoding="async" loading="lazy" width="683" height="350" src="https://ataiva.com/wp-content/uploads/2022/03/1-683x350.png" alt="" class="wp-image-8763" srcset="https://ataiva.com/wp-content/uploads/2022/03/1-683x350.png 683w, https://ataiva.com/wp-content/uploads/2022/03/1-300x154.png 300w, https://ataiva.com/wp-content/uploads/2022/03/1-768x394.png 768w, https://ataiva.com/wp-content/uploads/2022/03/1-1536x787.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/1.png 1920w" sizes="(max-width: 683px) 100vw, 683px" /> </figure> 

Click Next.

Do not change any Storage options, and click Next.

### Step 2 &#8211; Add tags

Click `Add Tag` and enter a Key of `Name` and a value of `Bootstrapped Web Server`<figure class="wp-block-image size-large">

<img decoding="async" loading="lazy" width="800" height="254" src="https://ataiva.com/wp-content/uploads/2022/03/image-800x254.png" alt="" class="wp-image-8770" srcset="https://ataiva.com/wp-content/uploads/2022/03/image-800x254.png 800w, https://ataiva.com/wp-content/uploads/2022/03/image-300x95.png 300w, https://ataiva.com/wp-content/uploads/2022/03/image-768x243.png 768w, https://ataiva.com/wp-content/uploads/2022/03/image-1536x487.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/image-2048x649.png 2048w" sizes="(max-width: 800px) 100vw, 800px" /> </figure> 

Click Next.

### Step 3 &#8211; Create a security group

Select `Create a new security group` and give it a name and description of `EC2 default` and `Default security group access for EC2` respectively.

Click `Add Rule` and change the `Type` to `HTTP`.

Click Review and Launch.<figure class="wp-block-image size-large">

<img decoding="async" loading="lazy" width="701" height="350" src="https://ataiva.com/wp-content/uploads/2022/03/2-701x350.png" alt="" class="wp-image-8764" srcset="https://ataiva.com/wp-content/uploads/2022/03/2-701x350.png 701w, https://ataiva.com/wp-content/uploads/2022/03/2-300x150.png 300w, https://ataiva.com/wp-content/uploads/2022/03/2-768x384.png 768w, https://ataiva.com/wp-content/uploads/2022/03/2-1536x767.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/2.png 1920w" sizes="(max-width: 701px) 100vw, 701px" /> </figure> 

Click Launch.

### Step 4 &#8211; Create and download a key pair

`Create a new key pair` from the first dropdown and give it a name of `EC2 Instance`.

Click `Download Key Pair` and keep it safe. As this is the only time you will ever be able to download it.<figure class="wp-block-image size-large is-resized">

<img decoding="async" loading="lazy" src="https://ataiva.com/wp-content/uploads/2022/03/3-470x350.png" alt="" class="wp-image-8765" width="622" height="463" srcset="https://ataiva.com/wp-content/uploads/2022/03/3-470x350.png 470w, https://ataiva.com/wp-content/uploads/2022/03/3-300x223.png 300w, https://ataiva.com/wp-content/uploads/2022/03/3-768x572.png 768w, https://ataiva.com/wp-content/uploads/2022/03/3-1536x1144.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/3.png 1722w" sizes="(max-width: 622px) 100vw, 622px" /> </figure> 

Once downloaded, click `Launch Instances`.

### Step 5 &#8211; View the instance

On the Status Page, click on the instance id that is shown at the top.

In the below example, the instance id is `i-062bd33e3189adfcc`.<figure class="wp-block-image size-large is-resized">

<img decoding="async" loading="lazy" src="https://ataiva.com/wp-content/uploads/2022/03/4-505x350.png" alt="" class="wp-image-8766" width="704" height="488" srcset="https://ataiva.com/wp-content/uploads/2022/03/4-505x350.png 505w, https://ataiva.com/wp-content/uploads/2022/03/4-300x208.png 300w, https://ataiva.com/wp-content/uploads/2022/03/4-768x533.png 768w, https://ataiva.com/wp-content/uploads/2022/03/4-1536x1066.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/4.png 1920w" sizes="(max-width: 704px) 100vw, 704px" /> </figure> 

After a few seconds, you will see the new instance up and running.<figure class="wp-block-image size-large is-resized">

<img decoding="async" loading="lazy" src="https://ataiva.com/wp-content/uploads/2022/03/5-508x350.png" alt="" class="wp-image-8767" width="704" height="485" srcset="https://ataiva.com/wp-content/uploads/2022/03/5-508x350.png 508w, https://ataiva.com/wp-content/uploads/2022/03/5-300x207.png 300w, https://ataiva.com/wp-content/uploads/2022/03/5-768x530.png 768w, https://ataiva.com/wp-content/uploads/2022/03/5-1536x1059.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/5.png 1920w" sizes="(max-width: 704px) 100vw, 704px" /> </figure> 

Copy the `Public IPv4 address` and open a new browser tab/window and paste it in the address bar.<figure class="wp-block-image size-large">

<img decoding="async" loading="lazy" width="800" height="112" src="https://ataiva.com/wp-content/uploads/2022/03/6-800x112.png" alt="" class="wp-image-8768" srcset="https://ataiva.com/wp-content/uploads/2022/03/6-800x112.png 800w, https://ataiva.com/wp-content/uploads/2022/03/6-300x42.png 300w, https://ataiva.com/wp-content/uploads/2022/03/6-768x107.png 768w, https://ataiva.com/wp-content/uploads/2022/03/6-1536x214.png 1536w, https://ataiva.com/wp-content/uploads/2022/03/6.png 1920w" sizes="(max-width: 800px) 100vw, 800px" /> </figure> 

The instance is now up and running and serving HTTP traffic on port 80.