Created
July 8, 2022 03:58
-
-
Save Debdutta-Panda/ca979547c72fd5186de0bd9d7f367cb8 to your computer and use it in GitHub Desktop.
AWS VPC EC2 inter communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo su | |
apt update | |
apt upgrade -y | |
apt install nginx -y | |
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - | |
apt install nodejs -y | |
apt install build-essential -y | |
npm install pm2@latest -g | |
cd /var/www/html | |
mkdir app | |
cd app | |
//////////// | |
setup node js app | |
npm init -y | |
npm install --save express | |
npm install --save body-parser | |
npm install axios | |
npm install cors | |
///////////// | |
nano index.js | |
////////////////////// | |
const express = require('express') | |
const bodyParser = require('body-parser'); | |
const cors = require('cors'); | |
const axios = require('axios'); | |
const app = express(); | |
const port = 3000; | |
app.use(cors()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.get('/myapi', (req, res) => { | |
res.send({success:true,message:'This is my api'}) | |
}); | |
app.get('/handleFromOther',(req,res)=>{ | |
res.send({success:true,message:'From ec2_put_index_of_instance'}) | |
}) | |
app.get('/otherapi', (req, res0) => { | |
axios | |
.get('http://private_ip_of_other_instance/handleFromOther') | |
.then(res => { | |
res0.send(res.data) | |
}) | |
.catch(error => { | |
console.error(error); | |
}); | |
}); | |
app.listen(port, () => console.log(`Hello world app listening on port ${port}!`)); | |
////////////////////// | |
nano /etc/nginx/sites-available/default | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
root /var/www/html; | |
index index.html index.htm index.nginx-debian.html; | |
server_name _; | |
location / { | |
proxy_pass http://localhost:3000; | |
proxy_set_header Host $host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto $scheme; | |
} | |
} | |
service nginx restart | |
//////////// | |
pm2 start index.js | |
pm2 startup systemd | |
pm2 save | |
systemctl start pm2-root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment