Building an AWS Auto Scaling Group (ASG) using Terraform Enterprise
Tags: aws, terraform, devops
Published: 2022-02-06 10:21:41

Terraform is an Infrastructure as Code tool that focuses on the automation of infrastructure.

Terraform Cloud is a platform that performs Terraform runs to provision infrastructure, either on-demand or in response to various events. 

This article covers the following sections:
-   Signing up to Terraform Enterprise
-   Creating a Terraform Workspace
    -   Using the Version Control Workflow
    -   Using the CLI driven Workflow

Create a Terraform Cloud account by taking the following steps:
- Sign in to the [Terraform Cloud website](https://cloud.hashicorp.com/products/terraform)

![image](https://user-images.githubusercontent.com/49791498/152152237-f2e3e857-e140-46fe-a2bc-94646eee1cab.png)

- Create an Organization
![image](https://user-images.githubusercontent.com/49791498/152156733-2827c2ad-5e6d-4e76-9af5-4b7b6650696c.png)

- Create a Workspace: here, you are asked to select a method of building the workflow for your build; this can be through:
    Version Control Workflow: A build is triggered by pushing your code to a selected Version Control System such as GitHub GitLab.
    - CLI-driven workflow: Build infrastructure by running commands from your local terminal.
    - API-driven workflow: implement Terraform using the Terraform API.
![image](https://user-images.githubusercontent.com/49791498/152157352-f85b9ae8-412e-47da-b47f-c3fab1f86a52.png)

### Version Control Workflow
To use Terraform code on a Version Control system to build an infrastructure on Terraform enterprise, follow the prompts presented under the **Version Control Workflow**, to connect your VCS of choice to your Terraform Organization.

![image](https://user-images.githubusercontent.com/49791498/152159903-aa32fb95-35d7-4328-8653-b789d1fde71a.png)

Then set up variables such as your access key and secrets. 

![image](https://user-images.githubusercontent.com/49791498/152161158-5e9e862a-c62c-49e1-966b-ff5968fe7da1.png)

To trigger the build of any infrastructure, push the code to the repository to which you connected your Terraform Enterprise under the Version Control Workflow prompt.

### Overview of an AWS ASG Infrastructure built with Terraform
The code for this section is available on [GitHub](https://github.com/Mbaoma/terraform-enterprise).
The codes used to provision this architecture are modularized. A Terraform module is a set of Terraform configuration files in a single directory.

Our file structure is as follows:
- Terraform Enterprise
-- main.tf
-- variables.tf
-- output.tf
-- provider.tf


- The **main.tf** file, contains the configurations with values defined as variables.

![image](https://user-images.githubusercontent.com/49791498/152343836-ab71d459-f47f-4dd7-84ed-8bab86c334bb.png
)

- The ```variables.tf``` file, contains the values used in the main.tf file.

![image](https://user-images.githubusercontent.com/49791498/152344531-73fa2f69-fdfd-4e06-976a-933461f91af9.png
)

- The ```output.tf``` file, contains information that would be displayed publicly after running ```terraform apply```.

![image](https://user-images.githubusercontent.com/49791498/152345610-b4a3095d-9302-4072-ad6a-aea164766b1b.png
)

- The ```provider.tf``` file, contains details of the Terraform configuration for the cloud provider of choice, in this case AWS.

![image](https://user-images.githubusercontent.com/49791498/152347976-fab30f1b-8160-4f56-9f9f-0a38e6b34d7a.png)

### Triggering the Run
- Login to Terraform enterprise
```bash
terraform login
```

- Push your code to the previously created GitHub repository.

```bash
git add .
git commit -m '<commit message>'
git push <remote> <branch>
```

- Then head over to the Terraform enterprise UI, following the prompts.

![image](https://user-images.githubusercontent.com/49791498/152349665-8eb5c522-95e0-4e31-8522-373286ad134d.png)

- The configuration has been planned (equivalent to running ```terraform plan```), click on ```confirm and apply```.

![image](https://user-images.githubusercontent.com/49791498/152490143-074072e0-ccc2-4775-a280-a4645f2bb9b7.png)

- An ```applying``` tag becomes visible - this is equivalent to running ```terraform apply``` in the CLI.

![image](https://user-images.githubusercontent.com/49791498/152491327-76d9e92c-4dbc-4bfa-aff0-8ed84e2ebba6.png)

- Head over to your AWS account, to view the created resources.

![image](https://user-images.githubusercontent.com/49791498/152493157-c4840394-bed4-4314-95f7-9794efd6408a.png)


**N.B: Any new changes made to the Terraform code, should be pushed to GitHub so as to trigger a run to update your infrastructure**

- It is good practice to delete all provisioned services, to avoid incurring costs when using cloud providers.
![image](https://user-images.githubusercontent.com/49791498/152508133-bfb1517a-a48c-4cba-968b-ae4ac6ca2d82.png)

For this use case, we click on the **Queue Destroy Plan**.

![image](https://user-images.githubusercontent.com/49791498/152508610-ed7f9b25-3f3b-4be5-a92e-9dbcd61ac053.png)

The sequel to this article will highlight the CLI Driven Workflow.

Thanks for your time.

Feel free to connect with me via: [LinkedIn](https://linkedin.com/in/mbaoma-chioma-mary), [Twitter](https://twitter.com/mba_oma)

Yoroshko!