The technology stack that was inherited from the prior tech team at Research Blocks came with some outdated methodologies. One of those areas was in the deployment of React applicationa on EC2 instances. For Single Page Applications (SPAs), like React, there are better options for rapidly deploying a scalable production app. The Amplify environment also makes it very easy to stand up multiple instances of the application container. These canrepresent different builds such as a development, staging, or test environments.
While there is plenty of documentation available on how to go about building an React app, there is very little on problem solving or details on what is going on “inside the black box”. This article is a collection of notes on what was found delving deeper into the AWS tech stack for Amplify.
The first thing to know about Amplify is it is a hosting service for STATIC WEB ASSETS; as they like to say “Amplify Hosting provides a git-based workflow for hosting full-stack serverless web apps with continuous deployment.”. In other words, it does not work like a traditional web service such as Apache or nginx.
This also means it is not going to do well with actively listening to incoming web request. Amplify is not going to play a good host as an Express server. There are ways to do it using Lambda functions and some additional configuration that will be content for another article.
Build settings and Amplify.yml
While you can enter the various commands in the YML formatted Build settings on the Amplify console, learn from my mistakes and instead put an amplify.yml file in your source root directory. That way when you inadvertently tell it to start the node server within the build file you can go back and look at an older commit and revert to the working copy of the file.
Regardless of which method you use, a simple Amplify.yml (Build settings) should look similar to the following image.
What To Put In Build Settings
The build settings should only contain the commands that are needed to download required code libraries and subsequently compile the code into a set of static files. This is where your React app’s package.json comes into play; package.json will tell Node which modules are needed for the yarn install command (we use yarn vs. npm) as well as how to build the static files for the SPA with the yarn build command. In our case, run webpack and set some environment parameters.
What NOT To Put In Build Settings
Do NOT put in any commands that START a node server into the build settings. The build manager for Amplify looks for any specified commands to finish executing. Starting a node server runs a command that , as far as the “build monitor” is concerned, never finishes.
Remember, Amplify is going to stand up a static website for you. Along the way it will setup an https/http listener and route traffic to your Single Page Application accordingly. There is no need for a node server in this situation. Amplify is going to take over the responsibility for the apps it hosts.
Not super well documented, but again based on observations and assumptions, the main setting here is the baseDirectory. This tells the Amplify build manager “copy all the files under the specified directory and go through them out on the CDN as a static file set”. The subsequent files specification is a ‘stack’ of file paths you want included where the **/* specification means “any file in this directory and anything under it”.
Amplify’s build manager seems to us this setting to execute a zip command which grabs all the files in the directory and files list specified, zips them up and distributes it to the CDN.
The cache section tells Amplify which directories and files to carry over from one build to the next. While most of the building/distribution manager services on Amplify start with a fresh container, anything in the cache appears to be copied from one build to the next.
Almost all the Amplify documentation cites putting node_modules/**/* in this list to save the previously-mentioned yarn install command from having to download the same million-plus node files on every build. Instead it copies them from one container to the next saving all those electrons from flying around Internet servers to recreate the files every time. Instead the electrons all stay within Amazon’s borders and maybe speeds things up a bit.
On to bigger & better things… next up trying to see if we can trick Amplify into hosting an Express server WITHOUT lambda proxy functions.
I’ve been working on a React App for months that connects on localhost port 9000. Recently the app stopped working after an upgrade to Monterey. Turns out the upgrade also required a number of services to be re-installed via brew. One of those services, php-fpm, is now taking over port 9000 automatically on startup despite not having explicitly set the updated PHP version to run at start.
As such, opening http://localhost:9000/ on the browser was routing to PHP apps and not the node app I was looking for.
MacOS Listing Port Usage
The first step to finding what other services might be fighting with the node app is to run the lsof command.
sudo lsof -i :9000
The :9000 should be replaced with whichever port you are investigating. Port 9000 is the default for most node servers.
In my case the results looked like this:
This tells us that php-fpm will first answer any port 9000 queries on IPV4 localhost.
Node server has been relegated to only answer port 9000 on IPV6.
The node server on IPV6 can be verified by pointing the browser to http://[::1]:9000/ and connecting to the app.
[::1] is the IPV6 syntax for localhost.
Listing Homebrew Services
To stop the php-fpm service it needs to be managed via brew since that is where I started the service from in the first place. The service php-fpm does not exist, so what is calling it? Let’s check brew services…
brew services list
Turns out is is the email@example.com service causing the issue.
Stopping Brew Services
Now that we know which service is fighting with node server we can stop it.
brew services stop firstname.lastname@example.org
That stops php-fpm from intercepting port 9000 traffic on localhost and the original http://localhost:9000 access I was running previously is now back in action.
A quick intro to Vue. If you are a jQuery fan I highly recommend looking into adding Vue to your developer toolbox.
This is not an in-depth article — have too much going on these days for that. It is a more a short-hand techie crib sheet of how I got a deployment repo to auto-pull the latest changes to its develop branch over to my staging server automatically. This is pulling down a fully software environment to a directory on the server.
I am completely baffled by this one and hope one of my techie friends can help.
I’m using a PHP class with magic methods to set and get the properties of that class. The idea is to use private properties in the class so that the PHP magic methods can take over and determine whether to update a WordPress user meta entry, blog entry, or standard option based on which proper of the class is being retrieved or stored.