Automatically deploy your NestJS API to AWS Elastic Beanstalk using GitHub Actions
Published: 2024-01-10 10:36:42

After last week's post about deploying your NestJS API to AWS Elastic Beanstalk(https://dev.to/juanpireslima/deploy-your-nestjs-api-on-aws-elastic-beanstalk-1on1), in this post, I'm going to help you make this process automatic by using GitHub Actions.

**We're going to start this post from where we stopped on the last one,** which we:
- Created an AWS Elastic Beanstalk Application from scratch
- Deployed our NestJS API manually to AWS Elastic Beanstalk

If you have already created your Elastic Beanstalk Application and are all set up to automate the deployment process, you can keep reading without any problem, either way, I highly recommend that you **read the previous post before this one** if you haven't yet.

## Set up GitHub Actions 👷
The first thing we're going to do is set up our GitHub Actions, for that, we can go to our repository page, click on actions, and then click on "set up a workflow yourself":
![GitHub Actions start page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8strhbc85m1twzalkzc.png)
After doing so, it's going to open a page for you to write the actions file, on the actions file, we need to set a name for our action, which I'm going to call "deploy", set up a trigger, which on my case it's going to run on every push to the main branch, and then we'll be setting up our jobs, the only job we're going to have is the one called "build-and-deploy", this job is going to run on ubuntu and it's going to:
- Install dependencies
- Build our project
- Compress files(since we need to send a .zip to Elastic Beanstalk)
- Configure AWS Credentials
- Install Elastic Beanstalk CLI
- Deploy our project to Elastic Beanstalk

![Workflow file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zt1kz0emgsma1sevlmth.png)

To correctly set up our AWS, we'll need to add some variables on our GitHub settings, you can do so by going to settings -> Secrets and variables -> Actions; there you'll be able to set up both variables and secrets, in our actions example, our AWS access key and region is a variable, while the secret is a secret, you should be creating them on the "Repository variables" section(I won't be covering the creation of AWS access credentials on this post):
![Managing GitHub Actions Variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ownir7tjamuerupx5txx.png)
One more thing you might've noticed is that on the deployment step, we're sending a label to Elastic Beanstalk, Elastic Beanstalk uses labels to version our project, so we must send a different label for each version of our API, that's why I'm using "github.event.repository.pushed_at" at the end, so I can get the timestamp of the last time something have been pushed to the repository, therefore making my label unique for each iteration on my code.

**PS:** _The word "test" after "eb deploy" is **the name of the environment** I'm deploying to, while "my-api" after "eb init" is the **name of my Elastic Beanstalk Application.**_

## Set up Elastic Beanstalk config 🔧
Before running our newly created action, we must create a config file for Elastic Beanstalk so it knows our deployment artifact will be called "my-api.zip", for that, we need to create a folder called ".elasticbeanstalk" at the root of our project, and there we'll have a file named "config.yml", there we'll be saying to Elastic Beanstalk that it should be deploying the artifact "my-api.zip":
![Elastic Beanstalk config file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ti8ju4pvdggd8d4ci1vp.png)
# Bonus: Changing the script Elastic Beanstalk will be running 💡
As said in the previous post about Elastic Beanstalk, it runs "npm run start" by default, but we can change that by creating a file called "Procfile" on the root of our project and changing the command that's going to run:
![Procfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5mdmtkg5nc72asfek95.png)
## Run your GitHub actions 👌
Once you've followed all the steps, you can commit all your changes, once the trigger is actioned, your action is going to run, as you can see, my actions ran without any problems:
![Successful GitHub Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ejktv7284hhwassk2ua.png)
Here's my API working:
![API Working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ps6affl8wo1whsax4uq.png)
Hope I was able to help you guys automate the deployment of your NestJS API to AWS Elastic Beanstalk, **you can find my code at:** https://github.com/juanpireslima/nestjs-elastic-beanstalk
If you haven't yet, I highly recommend you read my previous article about Deploying a NestJS API to Elastic Beanstalk, there, I wrote about:
- Creating an AWS Elastic Beanstalk Application - from scratch
Manually deploying a NestJS API to AWS Elastic Beanstalk

You can find it at: https://dev.to/juanpireslima/deploy-your-nestjs-api-on-aws-elastic-beanstalk-1on1