How to deploy a Typescript application on AWS using Elastic Beanstalk + CodePipeline

How to deploy a Typescript application on AWS using Elastic Beanstalk + CodePipeline

notion image

Introduction

Deploying an application in a scalable way could be really complex. In this article, we will understand how to deploy on AWS (Amazon Web Services) a Typescript Express application.
Please note that this tutorial will not focus on the code but mainly on the setup of the infrastructure.
As you will see, deploying an application using Elastic Beanstalk is really simple.
There are just a few prerequisites:
  • An AWS account
  • A Github account (we could use CodeCommit, but Github is more broadly used)
  • Basic knowledge of Typescript
 
Are you ready? Go!

The code

You can download the code from this Github repository. As I said before, the main goal of this article is to understand how to setup the infrastructure, so the code is really simple.
We have just one GET endpoint at “/”.
We will get this response "Ok! - Version 1”

The buildspec.yml

This buildspec file is used by CodeBuild (we will discuss it later) to generate the artifacts (in this case the Javascript code) from the Typescript code. You can learn more about how the buildspec.yml works here

Create the Beanstalk Environment

notion image
Once downloaded the code you have to follow those steps:
  1. Go into your AWS account
  1. Type “Elastic Beanstalk” in the search bar and click on the first result
  1. From the top-right choose your region. In my case, I will use Ireland
  1. Click “Create new environment”
  1. Keep “Web server environment” selected and click “Select”
  1. Now you can set an application name (I’ll choose “ts-example”)
  1. (Optional) Go on “Environment name” and override the autogenerated one
  1. (Optional) Customize the autogenerated domain if you want
  1. In the Platform section select node.js
notion image
 
Now in “Application code” leave the default “Sample application”.
notion image
With this configuration, Elastic Beanstalk will (through a Cloudformation template) autogenerate a lot of components like AutoScaling Group, Load Balancer, and so on. We are not interested in the details. Click on “Create environment” and you will see a black terminal with a lot of logs like in Matrix!
The creation of the environment will require a few minutes. Just relax and wait for the end of the process.
notion image
 
After a few minutes, you will find a strange link, everything is fine!
By clicking on the link, you will find a demo page but we will override this page in minutes.
notion image

Create the CI/CD Pipeline

Now that we have our Elastic Beanstalk environment ready, we need to create the CI/CD pipeline using CodePipeline.
Before going on CodePipeline ensure to have the code in one of your personal repositories (could be either public or private).
Now follow these steps:
  1. Click “Create Pipeline” on the top-right of the page
  1. You can choose the name that you want
  1. Let the service role name be autogenerated
  1. Click “Next”
  1. Now we have to add the source stage
    1. As source provider select Github version 2
    2. You will be asked to connect your Github account so follow the procedure and give the authorization
    3. In the repository name field select the repository that you created with the typescript code
    4. Choose your branch name (usually main)
    5. Click on Next
  1. Now we have to configure the Build stage (where Typescript will be compiled in Javascript)
    1. As the build provider select CodeBuild
    2. Now you have to create a building project
      1. Choose the name that you prefer
      2. In the Environment card select Amazon Linux 2 as Operating System
      3. In the runtime select Standard
      4. I recommend the x86_64-standard image
      5. leave everything else standard and click on the finish button
        1. notion image
    3. Click Next
  1. In the Deploy stage set
    1. Elastic Beanstalk as deploy provider
    2. The application you have already created as the application name
    3. The environment name as your environment
    4. Click Next
  1. Now you will see a resume of all the stages and… That’s it! Just click on “Create pipeline”
After the creation of the pipeline, CodePipeline will start the process and you will see the steps (Provisioning - Building - Deploying)
notion image
If you see something like this… Great job! You did it 😃
Now you have to go on the Elastic Beanstalk URL and you should see the default response.
 

Troubleshooting

If you see that the deployment went well but for some reason, the application is not working correctly you have to go (in the left bar) on “Logs” in the Elastic Beanstalk environment. Then click on “Request Logs” and then “Last 100 Lines”. Here you can find a lot of information.
Some common mistakes are:
  • You are using a library that is not declared as a dependency in the package.json
  • You made errors with env variables
Anyhow, you can just read the application logs and you will find your answer :)

Conclusions

If you are running this environment (like me) just like a test, please remember to terminate the environment (going to “Actions” in the environment details and then click on “Terminate environment”). You will be asked to write the name of the environment for security purposes. This process requires a few minutes.
Going on CodePipeline you should also delete the pipeline by selecting it and clicking on “Delete pipeline”.
Â