An AWS Code Build spec for your typescript node project
Tags: javascript, aawscodebuildspe, howtosetupcicdf, typescriptbuildon
Published: 2019-11-12 17:57:10

---
title: An AWS Code Build spec for your typescript node project
published: true
date: 2019-06-15 17:00:49 UTC
tags: JS,A AWS Code Build spe,How to setup CI/CD f,typescript build on 
canonical_url: https://softwaresurvivor.com/2019/06/15/an-aws-code-build-spec-for-your-typescript-node-project/
---

 ![AWS Code Build](https://softwaresurvivor.com/wp-content/uploads/2019/07/baccab28f77c594ee6b73de0224e75bd.png)

[AWS Code Build](https://aws.amazon.com/codebuild/) is a great addition to your [AWS](https://aws.amazon.com/) infrastructure. Today, we are going to focus on one aspect of the software deployment process. **Building!** Before we dive in, let me provide some context about the architecture and my plan for the CI/CD process. The project behind this post is a [node.js](https://nodejs.org/en/) [express](https://expressjs.com/) project written in [typescript](https://www.typescriptlang.org/). The API is served on [Elastic Bean Stalk](https://aws.amazon.com/elasticbeanstalk/). We want to auto deploy our code versions to elastic beanstalk by using AWS Codebuild and then later [Code Pipeline](https://aws.amazon.com/codepipeline/) to push the resulting build to the Elastic Bean Stalk instance.

**Deployments suck**. You build this great product on your local machine and now you have to deploy it to the masses. You spent so much time focusing on the product, that you forgot to scope out the deployment process. It’s DevOps! How hard can it be?! … err.. guilty. There are so many ways to do deployment, as is common in this field. You can easily spend a whole day researching different deployment tools for your environment. You try to understand why certain products exists, weigh the pros and cons of each based on opinionated words of others. Perhaps integrations between different platforms are possible. Travis for AWS? How are those set up? Death by a thousand choices and words. Now with the setup above, we can focus on our code, and let automation tools worry about the builds. I also wanted to keep it all on AWS.

Let’s worry about Git. Branch management is important, but that’s a different topic for another day. In my project, I have two special branches, a ‘staging’  and a ‘production’ one. I have two AWS Code Build configurations watching these two branches for any changes. If a change is detected, for example, I merged my development branch into staging or staging into production, a code build will fire and the pipeline is executed. A minute later, our project has a new release! Magic!

For AWS Code Build, you need a buildspec.yml in your git repo to tell the server how to install and build your project. In this case, my package json file has an npm command called build-ts that runs `tsc`.  You can also include your tests command here, but I’ll come back to that at a later post.

The artifacts section is also important. Elastic Bean stalk expects us to zip the contents of the result, not the resulting build folder. So I am grabbing three things here. The package.json file, the read me, and the contents of the dist folder.

```
//buildspec.yml
version: 0.2
phases:
    install:
        commands:
            - 'npm install'
    build:
        commands:
            - 'npm run build-ts'
artifacts:
    type: zip
    files:
        - package.json
        - package-lock.json
        - 'dist/**/*'
        - LICENSE
        - README.md
        - .npmrc

```

Head on over to AWS Pipeline to setup a pipeline. When you set up a pipeline, you can set the source, and later a build project. It’s easier than going to each step, setting it up individually, and later connecting them. I plan on showing each step in detail in my next post, but hopefully you’re at a point where you either need the build spec, or just a general overview of this type of AWS infrastructure. Happy coding!

![Code Pipeline](https://softwaresurvivor.com/wp-content/uploads/2019/07/a6f8ed3dbf2b27748e0f7ae332f821b6.png)