How to deploy a Typescript Parse Server on Elasticbeanstalk
Tags: devops, typescript, parseserver, tutorial
Published: 2022-12-01 14:03:43

_Originally posted on [ubiqueIoT](https://www.ubiqueiot.com/posts/parse-server-eb-deploy)_


Now that we have our Typescript Parse Server compiling and connecting to our MonoDB database, let's get it deployed to Elasticbeanstalk. 

Download and install the [Elasticbeanstalk CLI](https://github.com/aws/aws-elastic-beanstalk-cli) and the [AWS CLI.](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)

Run `aws configure` to sign in to your account with the CLI. 

Log into AWS console and [create a new elastic beanstalk instance](https://us-east-1.console.aws.amazon.com/elasticbeanstalk/home?region=us-east-1#/newEnvironment).

Choose `web server environment`.

![new application 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aaceo40va4c8g33x3fw7.png)

Give your new application a name:

![new application 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2whrm20xs3pv255aa27.png)

It is helpful here to choose a specific domain rather than letting it auto generate so that we can update our Parse config. Take note of the domain that you give your instance.

![new application 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txugvy6t6mpqam3rfg3t.png)

Choose a Node16 environment with the default settings:

![new application 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icj0yfe7ththqw1v05sp.png)

Allow the instance to be created with a sample application and click `create environment`.

![new application 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/681gmxngobfgqb9dbl3k.png)

While your environment is deploying, navigate to `./.ebextensions/app.config` of your Parse Server project and update the `SERVER_URL` property with the domain you specified. 

For example:

```yml
SERVER_URL: "http://parseservertutorial1.us-east-1.elasticbeanstalk.com/parse"
```

The `./.ebextensions/app.config` file contains all of the environment variables that our Elasticbeanstalk instance will be deployed with. 

Here you can add the `APP_ID` and `MASTER_KEY` that you want to use for production. 

Note that this file should not be checked into git to keep these keys secure. 

We can also update the `DATABASE_URI` environment variable. You can then remove it from `./src/index.js` so that we keep our connection string secure. 

## Configuring the Elasticbeanstalk Environment

After the sample code is deployed, we'll need to change the instance type to `t2.small`. 

❗️ This is critical, otherwise the `npm install` command will fail on the server. `t2.micro` is too small of an instance for Parse Server.

In the side panel of your Elasticbeanstalk console, choose the configuration option.

![instance size 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4i1uyuy5anp4ezvpxulf.png)

Scroll down until you find the capacity section and choose Edit. 

![instance size 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xiy6y4jovxz3k0ivgdp.png)

Find the 'instance types' section and remove `t2.micro` while keeping `t2.small` or larger.

![instance size 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r500g78mhfvojakrowbc.png)

## Initialize our Parse Server as an Elasticbeanstalk instance

In the project's terminal run:

```bash
eb init
```

Answer the prompts by selecting the specified region and project name of our new instance when prompted. 

Make sure to choose `n` when asked if you wish to continue with CodeCommit (assuming you are using Github).

Note that `eb init` will update our `.gitignore` file automatically for us.

Next, we need to make sure that our compiled javascript gets deployed and not our typescript.

In your root directory, create a new folder called `./scripts/`

Add a file named `./scripts/dist.sh` and fill it with the following shell command:

```shell
# If the directory, `dist`, doesn't exist, create `dist`
stat dist || mkdir dist
# Archive artifacts
zip dist/$npm_package_name.zip -r dist package.json package-lock.json .ebextensions
```

Next, move on over to your `package.json` file and add two new scripts:

```json
    "dist": "sh ./scripts/dist.sh",
    "build": "tsc"
```

Test them out by first running `npm run build` and then running `npm run dist`.

You can confirm that everything is working by confirming there's a new .zip file in your `./dist` folder. 

Now we'll need to tell the Elasticbeanstalk CLI which zip to upload. Head on over to the `./elasticbeanstalk/config.yml` file that was generated with the `eb init` command.

Add a new stage called `deploy` at the very bottom:

```yml
branch-defaults:
  master:
    environment: Parseservertutorial1-env
environment-defaults:
  Parseservertutorial1-env:
    branch: null
    repository: null
global:
  application_name: parse-server-tutorial-1
  default_ec2_keyname: null
  default_platform: Node.js 16 running on 64bit Amazon Linux 2
  default_region: us-east-1
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: null
  sc: git
  workspace_type: Application
# New!
# Replace the "parse-server-example" with the name of your zip file
deploy:
  artifact: dist/parse-server-example.zip
```

Let's try to deploy and cross our fingers.

But first make sure to commit your changes, otherwise the elasticbeanstalk CLI will not pick them up. 

```bash
git add .
git commit -m "inital commit"
eb deploy
```

Once the deployment completes, you can test that your Parse Server is running as expected by running the following command:

```bash
curl --location --request POST 'http://<your domain here>/parse/functions/hello' \
--header 'X-Parse-Application-Id: <your app id here>' \
```

You should get a response from your `./cloud/functions.ts` file with a body of: `Hi`.

Now you have your Typescript Parse Server running on your Elasticbeanstalk environment!

[Remember to set up VPC peering to secure your database connection!](https://www.youtube.com/watch?v=8NITVf0L5X0&ab_channel=50kT)