This post is outdated and contains references to a pre-release version of Pulumi Crosswalk (`@pulumi/awsx`). For updated AWSx documentation and examples, [see the AWS Guides](/docs/clouds/aws/guides/).
In this blog post, we return to the PERN application we previously [migrated to Kubernetes](/blog/deploying-a-pern-stack-application-to-aws/) and replace the PostgreSQL database with MongoDB. Although it might seem like a difficult task initially, the straightforward design of Pulumi and Kubernetes allows us to easily transition the application form a PERN stack to a MERN one.
The main difference between PostgreSQL and MongoDB is data is stored. PostgreSQL is a relational database that allows users to create complex tables and relations between values. In contrast, MongoDB is a key-value database that stores data in simple collections instead of a relational model. Because the application takes the form of three distinct components--the client, server, and database--almost all of the changes will occur in the database component. In fact, we can reuse most of the existing code and will be able to change the database without additional hassle.
The first step to switching the stack is to clone the [PERN Kubernetes example](https://github.com/pulumi/examples/tree/master/aws-ts-k8s-voting-app). We'll use a sparse clone to copy only the `aws-ts-k8s-voting-app` directory.
The next step is to create a new directory and initialize a Pulumi project with `pulumi new aws-typescript`.
```bash
$ mkdir aws-ts-k8s-mern-voting-app && cd aws-ts-k8s-mern-voting-app
$ pulumi new aws-typescript
```
The project will require several configuration variables, which we set using `pulumi config set`. The variables are used to configure the MongoDB user account, initializing the database schema and table, and its region.
```bash
$ pulumi config set sql-user-name <NAME>
$ pulumi config set sql-user-password <PASSWORD> --secret
$ pulumi config set aws:region <REGION>
```
The `package.json` file lists the libraries used by the project. We will add the following to the `dependencies` section:
```json
"@pulumi/eks": "^0.20.0",
"@pulumi/cloud-aws": "^0.19.0"
```
As most of our project components are nearly identical to those in the PERN Kubernetes application, we can copy many of them into our directory.
Hosting MongoDB will require a rework of the database Docker container and startup scripts. Like before, the outer `databaseside` folder holds our Dockerfile, while the inner `database` folder contains code.
```bash
FROM ubuntu:18.04
WORKDIR /
EXPOSE 5432
RUN apt update && \
apt install -y mongodb
ADD database /database
CMD [ "/database/startDatabase.sh" ]
```
Our setup doesn't require any files, and so the only thing we need to put in the `database` folder is a `startDatabase.sh` script that launches the database, configures users, and creates a table.
```bash
#!/bin/bash
set -exu
FILE=/persistentVolume/mongod.lock
if test -f "$FILE"; then
echo "/persistentVolume already contains MongoDB, no need to initialize database."
Now that, server, client, and database have been fully converted to use MongoDB, we can make the last remaining change in the infrastructure by updating the database deployment and service.
const serversideListener = new k8s.core.v1.Service(...);
const clientAppName = ...;
const clientAppLabels = { ... };
const clientDeployment = new k8s.apps.v1.Deployment(...);
const clientsideListener = new k8s.core.v1.Service(...);
```
To give us a simple way to connect to our application, we export the clientside listener's address. We can now open a browser window with the URL and port number to view our application.
In this example, we showed how to change a PERN stack-based application to use MERN, and demonstrated the ease with which a designer can swap out different components. The isolated nature of containerized applications allows for a great deal of modularity. It can significantly reduce the amount of time spent renovating previously written code to fit any added new components.
The MERN application code is [on Github](https://github.com/pulumi/examples/tree/master/aws-ts-k8s-mern-voting-app).