Wednesday, November 10, 2021

Published November 10, 2021 by with 0 comment

Simple Tutorial for Hosting a CRUD Node App on AWS Elastic Beanstalk

I had trouble finding (working) simple tutorials for running Node.js CRUD apps on AWS using Elastic Beanstalk so I wrote one from scratch and documented it.

The app

I've put two phases together. The first is a simple hello world using Node.js, and the second is a simple app that lets you add a number to a MySQL database and read back all numbers that you've added. Below is what it looks like when it's working:



Please do not use this as a reference for writing Node cleanly or anything like that. This is structured to (hopefully) be very minimal and easy to follow example for setting up Node.js and MySQL through this service and isn't intended to be a serious app.

Setting up your AWS environment

Set up an environment using the AWS documentation. When writing this, the steps were:
  • create an environment
  • choose 'Web server environment'
  • configure environment
    • give it a name
    • choose node js for type
    • use defaults for node and linux
    • choose 'upload your code' at the bottom
    • upload a zipped folder with the code from one of the examples I listed above; it should look like this (i.e., files directly in the zipped folder and not a folder containing them)

    • configure more options
      • create a database and choose mysql
  • create
  • wait 10 minutes or so
These might change though so the AWS documentation is likely a good place to start for initial environment creation.

When complete, it should look something like this:



Quick exploration of the environment

Two things that you might want to do right away are manage versions of your app, and look at logs.

To manage versions, simply go to 'Application versions' on the left and you can deploy, delete, etc. any of them:



To look at logs, simply go to 'Logs' on the left and you can pull them.

Quick overview of the hello world app


package file:

The package file for this is very simple. It just defines express v3.1.0 as a dependency and tells the environment that 'node app.js' is the starting script to run.

app:

Things worth noting here:

  • AWS manages the ports for you; 'const port = process.env.PORT || 3000;' says 'use port 3000 unless the host has configured a port for you'
  • 'const dir = `${__dirname}/public/`;' grabs the public folder for the app and puts it in dir
  • 'app.get("/", (req, res) => { res.sendfile(dir + "index.html"); });' serves up the index.html file in the public folder when the default url is visited
That's it.

What the CRUD app adds to the hello world example


package file:

This app needs to use mysql and there's a convenient async package for smoothing out async usage in node that I used here.

app:

There are a few new things here:
  • database configuration
    • AWS manages the db so generate a connection using the environment variables for it
    • in order
      • create the db if it doesn't exist
      • set it as the db to use
      • create the 'numbers' table if it doesn't exist
    • log error or success message depending on how that sequence of calls went
  • route configuration
    • the app has two endpoints
      • history: return all numbers entered so far
      • new: add a new number
route:

The new endpoints are just SQL queries and those are implemented in 'numbers.js' in the routes folder.

index.html

This is a basic numeric input and button. Page load gets the history of numbers entered. Clicking the button adds the number in the input then gets the history.

Basic summary then is:
  • navigating to the page returns index.html
  • index.html auto-calls the history and displays a formatted response
    • calling history actually calls the 'getNumbers' method in numbers.js
  • submitting a new number calls new and then history
    • calling new actually calls the 'addNumber' method in numbers.js

Summary

That's all that's required to host a simple Node.js + MySQL app on AWS Elastic Beanstalk. Hopefully nothing has changed significantly by the time you read this that breaks this tutorial as that happening to a couple of others I read through is what prompted me to write this. Feel free to comment if you hit issues.


      edit

0 comments:

Post a Comment