Skip to content

Latest commit

 

History

History
202 lines (126 loc) · 6.18 KB

ec2_app_deployment.md

File metadata and controls

202 lines (126 loc) · 6.18 KB

Deploying an application on an EC2 instance

  1. Create an instance through the EC2 service; a detailed guide of this can be found within Creating an EC2 instance.

  2. Create a suitable name for the instance and select the correct operating system and version.

For the example application, the below was selected.

  1. Select an appropriate instance type for the applications computation requirements and ensure the correct key pair selected, as per the screenshot below.

  1. For this example, as a security group has already been configured, edit the network settings and select the correct security group from the select existing security group tab.

If a new security group is required, select create security group and configure the inbound rules as shown in step 7.

  1. Check the instance has been configured correctly on the summary page and when ready, launch the instance as shown.

  1. Whilst the instance is setting up, check and amend, if required, the inbound rules within the security group. Firstly, navigate to security group and click Actions then edit inbound rules.

  1. Check and amend the configuring as shown to limit the ssh connection to the users IP and a new custom TCP with port 3000 and 0.0.0.0/0 as an IP. Finally, save the rules.

  1. Once the instance has passed all its checks, open a git bash terminal and navigate to the folder containing the application.

  2. The contents of the application can now be transferred to the EC2 instance with the scp (secure copy) command.

scp -i "~/.ssh/tech230.pem" -r app [email protected]:/home/ubuntu
  1. After the transfer is complete, connect to the instance using the ssh command shown in the connect section within the instance summary on AWS.
ssh -i "tech230.pem" [email protected]

  1. Confirm the application is now within the instance with the ls command.

  1. Provision the package manager with the latest packages and nginx installation as per the commands below.
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install nginx -y
sudo systemctl start nginx
  1. Install the applications dependant Node packages as per the commands below.
sudo apt-get install python-software-properties -y
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install nodejs -y
sudo npm install pm2 -g
  1. Navigate to the application folder using the cd command.

  2. Within the application folder, install npm and start the application as per the commands below.

npm install
pm2 start app.js

  1. The application should now be available on the web browser with the Public IPv4 address and port number, in this example 3000.

Deploying a two-tier architecture

  1. Ensure there are first two launch templates for application and MongoDB; if required, create a MongoDB template following this tutorial.

  2. Before launching the instances, review the security group used and add an additional inbound rule to allow communication between the two instances. The port to communicate with MongoDB is 27017 and allow any IP address to connect (0.0.0.0/0).

  3. Launch the two instances, for the application and MongoDB.

  4. Firstly, configure the MongoDB instance through initially connecting to the instance with the ssh command found in the connect section of the instance summary on AWS.

The ssh command should be displayed as the following; reminder to change directory cd into the .ssh folder or change the key route to "~/.ssh/<key_file_name>.pem".

ssh -i "~/.ssh/tech230.pem" [email protected]
  1. Amend the instances network interfaces to allow the application to connect, do this by accessing the file then changing the bindIP to 0.0.0.0/0.

Reminder to uncomment the port number, if required.

sudo nano /etc/mongodb.conf
  1. Restart and re-enable the database to realise these changes:
sudo systemctl restart mongod
sudo systemctl enable mongod
  1. Secondly, configure the application; create a new terminal and ensure the application folder has been transferred on to the application instance; if already complete, skip this step.

An example of the secure copy command.

scp -i "~/.ssh/tech230.pem" -r app [email protected]:/home/ubuntu
  1. Connect to the application instance with the ssh command found in the connect section of the instance summary on AWS.

An example of the ssh command.

ssh -i "~/.ssh/tech230.pem" [email protected]
  1. Proceed to adding the environment variable through the .bashrc file.
sudo nano .bashrc
  1. Add the following export command to the end of the script.

Add the Private IPv4 address as shown in the application instance summary.

export DB_HOST=mongodb://<private_IP_address>:27017/posts
  1. Read and execute the .bashrc file.
Source .bashrc
  1. Navigate to the app folder (there may be two app directories to cd into).
cd app
  1. Install the package manager npm.

If admin rights are required, try sudo apt install npm. Additionally, check if node seeds/seed/js has been performed in the terminal, else enter it in as a command.

npm install
  1. Run the application.

If the posts page is not being displayed in the next step, restart the application using pm2 stop app.js then running pm2 start app.js --update-env to update the environment variables

pm2 start app.js
  1. Check to see if the application is running with the posts page by the public IPv4 address and /posts.

For example, 34.255.2.50/posts

final page