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
Once downloaded the code you have to follow those steps:
- Go into your AWS account
- Type âElastic Beanstalkâ in the search bar and click on the first result
- From the top-right choose your region. In my case, I will use Ireland
- Click âCreate new environmentâ
- Keep âWeb server environmentâ selected and click âSelectâ
- Now you can set an application name (Iâll choose âts-exampleâ)
- (Optional) Go on âEnvironment nameâ and override the autogenerated one
- (Optional) Customize the autogenerated domain if you want
- In the Platform section select node.js
Â
Now in âApplication codeâ leave the default âSample applicationâ.
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.
Â
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.
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:
- Click âCreate Pipelineâ on the top-right of the page
- You can choose the name that you want
- Let the service role name be autogenerated
- Click âNextâ
- Now we have to add the source stage
- As source provider select Github version 2
- You will be asked to connect your Github account so follow the procedure and give the authorization
- In the repository name field select the repository that you created with the typescript code
- Choose your branch name (usually main)
- Click on Next
- Now we have to configure the Build stage (where Typescript will be compiled in Javascript)
- As the build provider select CodeBuild
- Now you have to create a building project
- Choose the name that you prefer
- In the Environment card select Amazon Linux 2 as Operating System
- In the runtime select Standard
- I recommend the x86_64-standard image
- leave everything else standard and click on the finish button
- Click Next
- In the Deploy stage set
- Elastic Beanstalk as deploy provider
- The application you have already created as the application name
- The environment name as your environment
- Click Next
- 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)
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â.
Â