|||

A hashicorp packer project to provision an AWS AMI with node, pm2 & mongodb

Packer

Packer is an amazing tool to provision multi cloud virtual machine images with code. Making it easier to audit/maintain images and check provisioning code it into source control. Packer has three main building blocks:

  1. builders
  2. provisioners
  3. post processors

Our sample project to deploy node, pm2 and mongodb on an ubuntu 20 x86_64 server has three main files:

1. app-server.json [ this will contain the packer config json ]

{
    "builders": [
        {
            "type": "amazon-ebs",
            "profile": "my-local-aws-credentials-profile",
            "region": "ap-southeast-2",
            "ami_name": "ubuntu-node-mongo-packer-v1",
            "source_ami": "ami-0a43280cfb87ffdba",
            "instance_type": "t3.medium",
            "ssh_username": "ubuntu"
        }
    ],
    "provisioners": [
        {
            "type": "file",
            "source": "hellonode.js",
            "destination": "/tmp/hellonode.js"
        },
        {
            "type": "shell",
            "script": "app-server.sh"
        }
    ],
    "post-processors": [
        {
            "type": "manifest",
            "output": "output.json"
        }
    ]
}

2. app-server.sh [ this will contain the shell commands to provision the server ]

#!/bin/sh
sleep 30

# Install node js
curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt-get install -y nodejs

# Install pm2
sudo npm install -g pm2

# Install Mongo
sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 656408E390CFB1F5
echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable mongod.service
sudo systemctl start mongod.service
sudo systemctl status mongod.service

# Configure pm2 to run hellonode on startup
mkdir -p ~/code/app-dist
mv /tmp/hellonode.js ~/code/app-dist/hellonode.js
cd  ~/code/app-dist/
sudo pm2 start hellonode.js
sudo pm2 startup systemd
sudo pm2 save
sudo pm2 list

3. hellonode.js [ A sample nodejs http server for testing ]

var http = require("http");
var port = 8080;
var now = new Date();
http
  .createServer(function (req, res) {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.write("Hello World - this is node.js\n");
    res.write("Date on server: " + now.toGMTString());
    res.end("\nbye!");
  })
  .listen(port, "");
console.log("Server running at port: " + port);

And finally to create the AMI just run the command below and a new AMI will appear in the console that can be used to launch new instances.

packer build app-server.json
Up next Some notes on Zeebe (A scalable process orchestrator) How to samples with AWS CDK
Latest posts Flutter CI/CD with Azure Devops & Firebase - iOS - Part 1 Flutter CI/CD with Azure Devops & Firebase - Android - Part 2 How to samples with AWS CDK A hashicorp packer project to provision an AWS AMI with node, pm2 & mongodb Some notes on Zeebe (A scalable process orchestrator) Docker-Compose in AWS ECS with EFS volume mounts Domain Driven Design Core Principles Apple Push Notifications With Amazon SNS AWS VPC Notes Building and Deploying apps using VSTS and HockeyApp - Part 3 : Windows Phone Building and Deploying apps using VSTS and HockeyApp - Part 2 : Android Building and Deploying apps using VSTS and HockeyApp - Part 1 : iOS How I diagnosed High CPU usage using Windbg WCF service NETBIOS name resolution woes The troublesome Git-Svn Marriage GTD (Getting things done) — A simplified view Javascript Refresher Sharing common connection strings between projects A simple image carousel prototype using Asp.net webforms and SignalR Simple logging with NLog Application logger SVN Externals — Share common assembly code between solutions Simple async in .net 2.0 & Winforms Clean sources Plus Console 2 — A tabbed console window