Last active
June 30, 2021 17:31
-
-
Save fitzgeraldsteele/52b93df99c58ed97a56e9682e9c62c6d to your computer and use it in GitHub Desktop.
cloud-init for Azure Metadata service
This file contains hidden or 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
#cloud-config | |
package_upgrade: true | |
packages: | |
- nginx | |
- nodejs | |
- npm | |
write_files: | |
- owner: www-data:www-data | |
path: /etc/nginx/sites-available/default | |
content: | | |
server { | |
listen 80; | |
location / { | |
proxy_pass http://localhost:3000; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection keep-alive; | |
proxy_set_header Host $host; | |
proxy_cache_bypass $http_upgrade; | |
} | |
} | |
- owner: azureuser:azureuser | |
path: /home/azureuser/myapp/index.js | |
content: | | |
var express = require('express') | |
var app = express() | |
var os = require('os'); | |
app.get('/', function (req, res) { | |
const request = require('request'); | |
const options = { | |
url: 'http://169.254.169.254/metadata/instance?api-version=2021-02-01', | |
headers: { | |
'Metadata': 'true' | |
} | |
}; | |
request(options, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var metadataObj = JSON.parse(body) | |
let ejs = require('ejs'), | |
html = ejs.render( | |
'<h2><%= metadataObj.compute.name %></h2> \ | |
<p>Name: <%= metadataObj.compute.name %></p> \ | |
<p>Private IP Address: <%= metadataObj.network.interface[0].ipv4.ipAddress[0].privateIpAddress %></p> \ | |
<p>Resource id: <%= metadataObj.compute.resourceId %></p> \ | |
<p>Scale set name: <%= metadataObj.compute.vmScaleSetName %></p></p> \ | |
<p><pre><%= JSON.stringify(metadataObj, null, 2) %></pre></p>', | |
{ metadataObj: metadataObj } | |
) | |
res.send(html) | |
} | |
else { | |
console.log("Error " + response.statusCode) | |
} | |
}) | |
}) | |
app.get('/health', function (req, res) { | |
res.send('PONG!') | |
}) | |
app.listen(3000, function () { | |
console.log('Hello world app listening on port 3000!') | |
}) | |
runcmd: | |
- service nginx restart | |
- cd "/home/azureuser/myapp" | |
- npm init | |
- npm install express -y | |
- npm install nodemon -y | |
- npm install ejs -y | |
- nodejs index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment