- The wh_nodejs_app project is used to deploy a simple nodejs appication on an AWS instance.
- The nodejs application will display the current timestamp of the AWS instance.
- The nodejs app will be routed via nginx.
- This project will provision the instance via terraform and deploy nodejs and nginx via ansible.
- The application will be available on the public ip of the AWS instance on port 80. The public IP will be provided in the output of terraform command.
- Nginx and nodejs servers are placed in the same machine.
- Nodejs application runs on 8090 port
- Nginx default port is 8080
- Nodejs app is routed via nginx at port 80
- The application will be deployed on Ubuntu 16.04 machine.
- Install Python >=2.7
- Terraform and ansible installed on Centos machine. Both configured on same machine.
- Terraform version v0.9.2 (latest). Installation steps: https://www.terraform.io/intro/getting-started/install.html
- Ansible version 2.2.1.0 (latest). Installation steps: http://docs.ansible.com/ansible/intro_installation.html
- Clone this repository:
git clone https://github.com/aratik711/wh_nodejs_app.git
- The ssh key should be present in the ~/.ssh directory of the user you will be executing the terraform and ansible. If not then generate a passphraseless ssh key using
ssh-keygen -t rsa
- The user with which you will be executing the terraform and ansible commands should have sudo rights and must have passwordless sudo access.
- To enable sudo. Login with root user. Edit /etc/sudoers and add the line:
username ALL=(ALL) ALL
Replace the username with your username. - To enable passwordless sudo. Login with root user. Edit /etc/sudoers and add the line:
username ALL=(ALL) NOPASSWD: ALL
Replace the username with your username.
Save and exit. You will now have passwordless sudo rights.
- To enable sudo. Login with root user. Edit /etc/sudoers and add the line:
- Execute the following commands:
cd wh_nodejs_app
ssh-keygen -t rsa -f wh_terraform/setup/dta/wh_nodejs_key -q -N ""
openssl rsa -in wh_terraform/setup/dta/wh_nodejs_key -outform pem > wh_terraform/setup/dta/wh_nodejs_key.pem
export AWS_ACCESS_KEY_ID="Your AWS access key"
export AWS_SECRET_ACCESS_KEY="Your AWS secret key"
Note: Here the key-name is wh_nodejs_key. You can change it as per your requirement.
You will have to change the key-name in terrafrm variable as mentioned below.
The private-key and pem file should have the same name.
Note: Do edit the variables marked as required.
- Edit the wh_nodejs_app/wh_terraform/variables.tf
- Set the
tf_home
variable to the directory where the wh_terraform directory is cloned.(required) - Set the
aws_key_name
to the name of the key to be created in the region of your choice. Please enter the name of the key you created in the above ssh-keygen steps(optional) - Set the
aws_region
to the ID of the region of your choice.(optional) - Set the
instance_type
to the size of instance you require.(default: t2.medium)(optional) - Set the
server_names
variable to the name you want the instance to display in AWS EC2 console.(optional) - Set the
username
to the user you want to create in the new machine.(optional)
Note: If the username is changed here then please also execute the following steps:- Set the
user
variable in wh_nodejs_app/wh_ansible/nginx.yml and wh_nodejs_app/wh_ansible/nodejs.yml. - Set the
app_user
variable in wh_nodejs_app/wh_ansible/group_vars/all/main.yml
- Set the
- Set the
user_password
to set the password of the above created user.(required) - Set the
hostnames
variable to set the internal hostname of the instance(optional)
Note: If the hostname is changed here then execute the following steps:- Edit the wh_nodejs_app/wh_ansible/inventories/servers. Change the
hostname
in nodejs and nginx block.
Both the hostnames will be same.
- Edit the wh_nodejs_app/wh_ansible/inventories/servers. Change the
- Set the
- To provision the machine with terraform execute the following command:
cd wh_nodejs_app/wh_terraform
terraform get
terraform apply -state=state/dta/wh-aws-nodejs.tfstate
- The above command should display the hostname/public IP address, username, password to connect to the instance. You should be able to do a passwordless ssh to the instance from the machine where terraform was executed.
- From the same machine from where terraform command was executed, execute the following
cd wh_nodejs_app/wh_ansible
ansible-playbook -i inventories/servers site.yml
- After a few minutes your nodejs application will be accessible on the public IP of the instance on port 80. Just enter the public IP of the instance provided in terraform output in your browser and you will be able to see the current timestamp of the instance in your browser.
- The code has been tested on AWS Seoul and Virginia region but will work on other regions as well.
- The nodejs app displays static timestamp (current timestamp when the webpage was loaded).
- The terraform and ansible controller machines have to be Centos 6/7 OS.
- The nodejs and nginx will be deployed on Ubuntu 16 OS. The image used will be "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04" and of type "hvm"
- The code only supports passphraseless ssh currently.
- Edit /etc/hosts and remove the entry of the AWS nodejs instance.
- Edit ~/.ssh/known_hosts remove the entry for AWS nodejs instance
- Go to AWS EC2 console -> Click on nodejs instance -> Actions -> Instance Settings -> Change Termination Protection -> Disable
- Execute the following commands:
cd wh_nodejs_app/wh_terraform
terraform destroy -state=state/dta/wh-aws-nodejs.tfstate
You will be asked for confirmation typeyes
. All of the created resources will be deleted.