EC2 Launch template step by step
Published: 2022-06-23 15:58:46

## Developing on your local machine is not a hassle for any one any more, deploying you code on a machine is no longer a problem also, but automating the process might be intimidating for some.


In this article, I will go through the configuration needed to create an EC2 launch template to automate assigning a new server instance, pull the code, make the proper configuration and even start the application.


Requirements

1. AWS Account
2. Github or Bitbucket repository that contains your application (i used nodejs in mine)


First of all, head to the EC2 Dashboard, and click on the launch template on the side navbar


![Launch template btn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9eizwebvy8d5srmk7s5.png)


**Create a launch template**

- give the template a name

- Define the OS, in my case (Ubuntu server 22.04 t2.micro) because it's free eligible

- Select the security group, most importantly it has an allow rule for both (http port 80, and ssh port 22)

- Network settings

- Configure storage

- Under the Advanced details there is an important field called user data, this part is quite important.

User data can hold the scripts for the instance to run once it's initialized, like installing some packages, pulling the code, build and start it.


![User data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoduehqobr5abacfunoy.png)
 

Add thee following code, I will add a comment before each line describing what it does. **Lines that start will ## is comments**

```

## 1. This line is important for AWS EC2 to understand the context of this file
#!/bin/bash -ex

## 2. Log all incidents in a user-data.log file
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

## 3. Install nodejs
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt-get -y install nodejs
apt-get -y install build-essential

## 4. Install nginx for network routing
apt-get update
apt-get -y install nginx
ufw allow 'Nginx Full'

## 5. Remove nginx default configuration
cd /etc/nginx/sites-available
unlink default

## 6. Add your custom nginx configuration
tee -a myapp.com <<EOF
server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name myapp.com;
 
 location / {
   proxy_pass http://127.0.0.1:3000;
   proxy_http_version 1.1;
   proxy_set_header Upgrade \$http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host \$host;
 }
}
EOF

## 7. Remove nginx other configuration file
cd /etc/nginx/sites-enabled
unlink default


## 8. Link both configurations and restart nginx
ln -s /etc/nginx/sites-available/myapp.com /etc/nginx/sites-enabled/myapp.com
systemctl restart nginx

cd /home/ubuntu

## 8. Install pm2 for forever running an application
npm install pm2 -g

mkdir app
cd app

## 8. Clone your repository and you may use the password in the url to skip password check step
git clone https://<username>:<password>@bitbucket.org/<project-name>/<repo-name>.git

cd repo-name

## 9. Add .env file and the necessary environment variables
tee -a .env <<EOF
NODE_ENV=development

PORT=3000
EOF

npm install

npm run build

## 10. Use pm2 to start the application
pm2 start npm --name "repo-name" -- run start

```

**Now your server is initialized, configured, the code is pulled to the latest changes and even up and running**

To check go EC2 Dashboard and launch and instance, but this time from a launch template


![Launch instance from launch template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3oogxsmsnsuwi03s2en.png)


You should count from 2 to 5 minutes and viola you have your own new instance, use the public IP or the public dns -provided in the instance info- in the browser. Now you should see the response from your application.