Sexiest way to manage your AWS resources
Tags: terraform, aws, codepipeline, iac
Published: 2023-02-14 23:49:43

A friend has been editing a SaaS solution for a few years on AWS Cloud. Step by step, his SaaS solution is starting to take hold, his client portfolio is growing, he has to hire more developers. The code that used to be modified by a single person will now be modified by several, so far its git repository is only composed of two branches “develop” for its developments and “main” for what goes into production.

![Suitable git flow for one single developer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cqeutwkvrmq5cwe28pl.png)

This situation terribly anguishes him, he wonders if he will be able to scale up. So he asks me :
- **How can I manage the development of features in parallel?**
- **How can I minimize the risks of regression?**
- **How to carry out production releases with zero downtime?**


![The Gitflow like for bigger developers teams](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4umuvsyvj62k0z7jy2ef.png)

Before answering those questions, I wanted to reassure him because being on AWS cloud is a great starting point. For sure AWS certainly has the most complete cloud ecosystem, and any SaaS application should feel safe there.
So I told him to close his eyes and imagine a solution able to dynamically :
- _**Creates a "feature" branch from "develop"**_
- _**Creates a "feature" dedicated environment on the AWS cloud**_
- _**Create a deployment pipeline on the "feature" environment and which is triggered at each commit on the "feature" branch**_
- _**Create a DNS record that allows easy testing of the "feature" branch**_
- _**Create a load balancer and deploy an SSL certificate for the "feature" environment**_
- _**Deploy in "Staging" with each validation of "merge request" carried out on the "develop" branch**_
- _**Create an image of the "staging" environment for a deployment in production**_


![Automating environment management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9govxsu0exx27myay2wi.png)


Then I told him to open his eyes, because it can easily be implemented on the AWS cloud, let’s deal with it.

## How to Create a “feature” branch from “develop”?

Terraform is your friend, the code below allows you to have a Terraform module that allows you to manage any git repository.

![Terrform code snippet to create or delete a git branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qimzb5nij6wf9gs0ioe9.png)

## How do I create a feature-dedicated environment on the AWS Cloud?

Terraform is once again your friend if you need to interact with AWS. All deployment models can be automated with Terraform:
- EC2 servers
- Docker containers on ECS
- Docker containers on Kubernetes
- Lambda functions
- RDS databases
- A combination of all these workloads

![Terrform code snippet to create an EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du474imwhaq02jq938c1.png)


![Terrform code snippet to create an AWS Fargate service for Docker containers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufwb2izkfo0zs38htngj.png)

## How do I create a deployment pipeline on the “feature” environment and which is triggered on commit on the “feature” branch?

Here again Terraform will allow you to automate the creation of a pipeline with the tool of your choice. But since you're on AWS, you can use Terraform to build a pipeline with AWS CodePipeline.

![Terrform code snippet to create an AWS CodePipeline pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvwxpxpqwvk0k0acsn11.png)

## How do I create a DNS record that makes it easy to test the "feature" branch?

We can definitely do everything with Terraform, because creating a DNS record on Amazon Route 53 is child's play.

![Terraform code snippet to create a DNS record in a Route 53 hosted zone](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8wl7qkamrlgpbyumrnb.png)

## How to create a Load Balancer and deploy an SSL certificate to it on the “feature” environment?

Terraform is once again the solution. All you need is to provide the ARN of your certificate.

![Terraform code snippet to create an AWS ALB with an SSL certificate attached on it](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ii9kcw1vl7u0wf36p3eq.png)

## How to automatically deploy each time a merge request is validated?

The validation of a "merge request" corresponds to a commit on the destination branch. With AWS CodePipeline and Terraform, automatic triggering of a pipeline execution is done by configuring a connection at the Source stage of your pipeline.

![Using a codestar connection to trigger pipeline execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umbpr1kov2ycv90lfl9d.png)

## How do I create an image of the staging environment for a production deployment?

With Terraform creating an AMI Image of an instance is easy, creating a Docker image is even easier.

![Terraform code snippet to create an AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilboojaro47ay2s9qbm8.png)

## So what's the sexiest way to manage your AWS resources ?

All this code allows you to have scripts that will allow you to manage your resources, create new environments, streamline and enhance the work of your developers.
To go further, you can even develop a small “user friendly” application to control the execution of your scripts and thus control the management of a complex infrastructure with a click.

![Managing IaC scripts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dhvhkz56v34hvdd6op1.png)

Then you entire architecture could look like this.

![Solution architecture including a IaC tool to manage AWS cloud resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kpar1sag4jsu51dl8s5.png)


## Terraform is great

IaC tools are the perfect answer to manage Cloud resources. You can still have fun using the AWS Web Console, but tools like Terraform will makes you speed, go fast like Usain Bolt, and improve the maintainability, of your AWS Cloud infrastructure.