Multi developer environments on AWS using Gitlab + Terraform + Serverless + React

Multi developer environments on AWS using Gitlab

We are using Gitlab to deploy our applications directly to AWS through pipelines. If you never heard about Gitlab (https://about.gitlab.com/stages-devops-lifecycle/), please feel free to read the Gitlab’s white papers and understand more about the tools. Maybe you are wondering why we don’t use Github/Actions to deploy our applications, and I’ll answer you with a simple reason: Runners (https://docs.gitlab.com/ee/ci/runners/).

In this project, we used a front-end application based on React and deployed on CloudFront + S3. Also, a backend application based on the Serverless framework deployed on API Gateway. Both were simple applications that were consuming some 3rd party API and a database in Dynamo DB.

 

DNX 1

Sometimes we would like to test some new feature that is being developed using our workspace the Development on AWS. It is very simple when we have a small team, and it is easy to do internal management, but sometimes we start to face some problems because another developer would like to test his new feature as well and this would cause confusion, as the second developer would need to merge/rebase the code and that would mean more time and nowadays time is money. ?

We can fix that problem creating a new workspace on AWS for each developer and we can share the resources between them or have one resource per developer, in this case, we will share the resources between the developer since we’re looking for cost optimisation as well.

Let’s look at some code. We’re using Terraform and a concept called workspaces if you never heard about that you may check our blog post talking about Terraform Workspaces.

Terraform — Deploy Infrastructure as Code (IaC)

JTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRjFiZDVlNjZiOTI1N2UyMzc2MDJhYjJmMjgxNDMxNzk2LmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNFBackend — API GatewayJTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRmU1YmI5YTVlODhhYWMwYmM0YWEwZGMyOTQxZjQwOTkyLmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNF

Variables

JTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRjE2MzYzOWJlOTNlMjA5YjE1OTczM2FhMjQ0MzA0ZjY2LmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNF

We’re using our Terraform modules to create most of the resources. All the code and tools that we’re using is Open Source so if you want to check this out, you will find all the terraform modules and tools that we’ve on this link: https://modules.dnx.one/.

 

Look’s nice but how we know each developer and deploy the code in the right workspace? Gitlab provides us with many environment variables that we can use (https://docs.gitlab.com/ee/ci/variables/predefined_variables.html) in this case we’ll use on the pipeline. The variable we can use to separate the environments could be the GITLAB_USER_ID.

Gitlab CI

JTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRjgxODMzNWIxYjAwMTdmYWQzYThkZmYxNDZkZDY2MjcyLmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNF

In this case, we set the pipeline to run the stage developer just when we deploy a variable called DEVELOPER_ID. This variable is the GITLAB_USER_ID that we found before and you can grab this on your Gitlab’s profile account (https://gitlab.com/profile) and get the User ID field. Now that we have our developer’s user id, we can run our pipeline to create the user workspace using Terraform:

DNX 2
Run Pipeline — Gitlab CI

The result will be:

DNX 4Pipeline Running – Gitlab CI

We can use the Gitlab CI to apply our code using terraform and creating the new developer just when we set the variable USER_ID.

Now that we have our workspace deployed, let’s move on and create our pipeline for our applications.

JTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRjMyNWE0ODIxODhkODc0YjU5MzEyMDBjMTM1ZDg2OWY0LmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNF

As we’re creating the API Gateway by developer we want each developer to have their own subdomain so we will use an API Gateway Custom Domain Name to have an endpoint pointing to the right API Gateway:

https://api-GITLAB_USER_ID.dev.cloud.example.com.au/

We also will change a bit our Serverless pipeline to grab the GITLAB_USER_ID and use inside for our code as an environment variable.

DNX 3

JTNDc2NyaXB0JTIwc3JjJTNEJTIyaHR0cHMlM0ElMkYlMkZnaXN0LmdpdGh1Yi5jb20lMkZicnVub2Rhc2lsdmFsZW5nYSUyRjk0NzEyNTUxM2MwNTAyZWYyNjc3NmRkNWRiN2M0NTE4LmpzJTIyJTNFJTNDJTJGc2NyaXB0JTNF

Now when the developer does a commit into Gitlab the pipeline will trigger and grab the code that he is developing and deploy into his workspace on AWS.

I hope that this article could be useful for you. Let me know in the comments if you have some doubts about that.

[html_block id=”1164″]

[html_block id=”1336″]

Plan Your Next Move with Confidence
Ready to align your technology with your business growth strategy? Talk to DNX about modernising your platform for scalability, resilience, and faster time-to-market.
Related Insights

Insights to help you navigate an evolving digital landscape.