-
Create an instance through the EC2 service; a detailed guide of this can be found within Creating an EC2 instance.
-
Create a suitable name for the instance and select the correct operating system and version.
For the example application, the below was selected.
- Select an appropriate instance type for the applications computation requirements and ensure the correct key pair selected, as per the screenshot below.
- 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 instep 7
.
- Check the instance has been configured correctly on the summary page and when ready, launch the instance as shown.
- Whilst the instance is setting up, check and amend, if required, the
inbound rules
within thesecurity group
. Firstly, navigate tosecurity group
and clickActions
thenedit inbound rules
.
- Check and amend the configuring as shown to limit the
ssh
connection to the users IP and a newcustom TCP
with port3000
and0.0.0.0/0
as an IP. Finally, save the rules.
-
Once the instance has passed all its checks, open a git bash terminal and navigate to the folder containing the application.
-
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
- 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]
- Confirm the application is now within the instance with the
ls
command.
- 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
- 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
-
Navigate to the application folder using the
cd
command. -
Within the application folder, install npm and start the application as per the commands below.
npm install
pm2 start app.js
- The application should now be available on the web browser with the Public IPv4 address and port number, in this example
3000
.
-
Ensure there are first two launch templates for
application
andMongoDB
; if required, create aMongoDB
template following this tutorial. -
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 withMongoDB
is 27017 and allow any IP address to connect (0.0.0.0/0
). -
Launch the two instances, for the
application
andMongoDB
. -
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 directorycd
into the.ssh
folder or change the key route to"~/.ssh/<key_file_name>.pem"
.
ssh -i "~/.ssh/tech230.pem" [email protected]
- Amend the instances network interfaces to allow the application to connect, do this by accessing the file then changing the
bindIP
to0.0.0.0/0
.
Reminder to uncomment the port number, if required.
sudo nano /etc/mongodb.conf
- Restart and re-enable the database to realise these changes:
sudo systemctl restart mongod
sudo systemctl enable mongod
- 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
- 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]
- Proceed to adding the environment variable through the
.bashrc
file.
sudo nano .bashrc
- 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
- Read and execute the .bashrc file.
Source .bashrc
- Navigate to the app folder (there may be two app directories to cd into).
cd app
- Install the package manager
npm
.
If admin rights are required, try
sudo apt install npm
. Additionally, check ifnode seeds/seed/js
has been performed in the terminal, else enter it in as a command.
npm install
- Run the application.
If the posts page is not being displayed in the next step, restart the application using
pm2 stop app.js
then runningpm2 start app.js --update-env
to update the environment variables
pm2 start app.js
- 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