Automation: How to update an AWS EC2 instance and install NGINX upon launch.

Aaron Bachman
3 min readDec 15, 2022

--

Today we are continuing our journey into the cloud with a short walkthrough aimed at saving a little time through automation. We are going to launch an AWS EC2 instance; update, install and start NGINX via the user-data field during the launch. This how-to guide is a continuation from last weeks article: A Journey into the cloud: Setting up a web server in the AWS cloud.

When we launch an EC2 instance we have the option to passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

Pre-requisites:

  • An AWS account: sign up for free here
  • Internet access; a computer, an email address
  • Access to command line, CLI or terminal
  • Basic knowledge of Linux
  • Basic knowledge of BASH scripting
  • Basic knowledge of AWS EC2 instances

Step 1: Getting started

Log into your AWS cloud user account and navigate to the EC2 dashboard by searching for EC2 in the search bar.

Click the orange Launch instance button.

Name your EC2 instance. For todays tutorial I will be selecting a Ubuntu OS that is free tier eligible.

For instance type we will again select the t2.micro (free tier eligible).

Create a Key pair for your login: I am using an RSA type, and .pem file format. This key will download automatically once created. Save it in a file dedicate for keys, or someplace where you will remember it.

For our network settings I have elected to create a security group, and allow SSH traffic from anywhere (Not best practice), Allow HTTPS traffic from the internet, and allow HTTP traffic from the internet.

For storage I will leave the default setting selected.

Underneath configure storage click advanced details and scroll all the way to the bottom. You will see a tab that says user data; and here where automation starts. We will be using scripting to update all packages, install an NGINX web server, and start the service when we launch the instance.

Enter the script below and click the orange Launch instance button.

After your instance in created go to your instance: and copy the public IPv4 address. Do not click open address.

And you will see:

Congratulations on completing a step further on our journey into the cloud!

Thank you all for reading, and please let me know if you have a suggestions or feedback!

--

--

Aaron Bachman

Level Up in Tech student. DevOps, Cloud engineering, AWS, Terraform.