Deploy a Node.js HTTP server to AWS

In this tutorial we will deploy the sample express Node.js HTTP server in your own AWS account with AnyCloud.
All the code can be found in this template repository which you can use to create a new repository for your AnyCloud project.

Enable programmatic AWS access to VMs for AnyCloud

1) Create a new IAM user in your AWS account using their console/UI as described here.
2) Create a new access key under that IAM user using their console/UI as described here.
3) Enable programmatic access for that IAM user, and attach the built-in AmazonEC2FullAccesspolicy to it as described here.
4) Take the accessKeyId and secretAccessKey from step 2 and create AWS Credentials stored locally at ~/.anycloud/credentials.json only.
You will need to pick a name or alias for the Credentials. The default value will be aws. In this example, we will call it mystartup-aws.
$ anycloud credentials new
? Pick cloud provider for the new credentials ›
Name for new Credentials: mystartup-aws
AWS Access Key ID: ******************
AWS Secret Access Key: ******************
Successfully created "mystartup-aws" Credentials

Configure your project

1) Initialize a git repository
git init
git add -A
git commit -m "Initial commit"
2) Initialize your package.json and install express
npm init
npm install express --save
3) Define an HTTP server listening on port 8088 in an index.js file
const express = require('express')
const app = express()
const port = 8088
app.get('/', (req, res) => {
res.send('Hello World!')
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
4) Define the Dockerfile
FROM node:lts
COPY . .
RUN npm install
CMD node index.js
5) Test the Dockerfile locally by installing Docker Desktop, building the Docker image and then running the server within the container
$ docker build -t anycloud/app .
$ docker run -p 8088:8088 -d anycloud/app:latest
$ curl localhost:8088
Which should return Hello World!
6) Use the AnyCloud CLI to create an anycloud.json file in the project directory and define a Deploy Config.
You will need to pick a name, or alias, for the Deploy Config. The default value will be staging. You will also need to associate Credentials to this Deploy Config.
$ anycloud config new
? Name for new Deploy Config › staging
? Pick Credentials to use ›
❯ mystartup-aws
Create new Credentials
? Do you want to choose a specific region for this Deploy Config? › y
? Region name › us-east-1
? Do you want to select which virtual machine type to use for this Deploy Config? › y
? Virtual Machine Type › t2.micro
? Do you want to add another region to this Deploy Config? › n
? Minimum number of VMs per region or cloud › 1
? Would you like to define a maximum number of VMs? › n
Successfully created "staging" Deploy Config.
7) Make sure all of the changes in the git repo are committed or they won't be deployed.

Deploy an App

1) Make sure you installed the AnyCloud CLI. Now deploy your server to your AWS account using the AnyCloud CLI.
$ anycloud new
? Pick Deploy Config for App ›
❯ staging
? Optional App name ›
▇ Creating new App
It might take a few minutes for your App to start while the virtual machine is provisioned and upgraded.
2) Check the status of your App
$ anycloud list
Apps deployed:
│ App ID │ Url │ Deploy Config │ Size │ Status │
│ crimson-tick-5 │ │ staging │ 1 │ up │
Deploy Configs used:
│ Deploy Config │ Region │ VM Type │
│ staging │ us-east-1 │ t2.micro │
3) The size of your App represents the number of virtual machines used to back your App. Apps scale elastically based on request load automatically. Now curl your AnyCloud App!
$ curl
Which should return Hello World!
4) Terminate your AnyCloud App when you no longer need it
anycloud terminate
? Pick App to terminate ›
❯ crimson-tick-5