Skip to content

Commit

Permalink
feat(ci): Add terraform validation (#4)
Browse files Browse the repository at this point in the history
Introduced a `.yml` file for a CI pipeline that validates the tf config.

fixes issue #2
  • Loading branch information
sydrawat01 authored Sep 25, 2023
1 parent e5befc7 commit 4ab0ffa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/tf-validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Terraform IaC template validation for Jenkins

on:
workflow_dispatch:
pull_request:
branches: [master]
push:
branches: [master]

jobs:
tf_validate:
runs-on: ubuntu-latest
name: Configure AWS `ghactions` IAM user
steps:
- uses: actions/checkout@v3
- name: Create Terraform variables
run: |
cd root && touch prod.tfvars
echo REGION=${{ secrets.AWS_REGION }} >> prod.tfvars
echo ENV=${{ secrets.ENV }} >> prod.tfvars
echo VPC_CIDR_BLOCK=${{ secrets.VPC_CIDR_BLOCK }} >> prod.tfvars
echo PUBLIC_ROUTE_TABLE_CIDR_BLOCK=${{ secrets.PUBLIC_ROUTE_TABLE_CIDR_BLOCK }} >> prod.tfvars
echo PUBLIC_SUBNETS=${{ secrets.PUBLIC_SUBNETS }} >> prod.tfvars
echo PRIVATE_SUBNETS=${{ secrets.PRIVATE_SUBNETS }} >> prod.tfvars
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Setup `Terraform`
uses: hashicorp/setup-terraform@v2

- name: Terraform fmt
id: fmt
run: terraform fmt -check
continue-on-error: true

- name: Terraform Init
id: init
run: terraform init

- name: Terraform Validate
id: validate
run: terraform validate
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ terraform -install-autocomplete
terraform init
```

2. Create a `.tfvars` from the `example.tfvars` template.
2. Create a `<filename>.tfvars` using the `example.tfvars` template.

3. Plan the cloud infrastructure
3. Validate the terraform configuration

```bash
terraform validate
```

4. Plan the cloud infrastructure
This command shows how many resources will be created, deleted or modified when we run `terraform apply`.

> NOTE: Remember to set your aws profile in the terminal to run the commands going forward
Expand All @@ -90,15 +96,15 @@ terraform -install-autocomplete
terraform plan -var-file="<filename>.tfvars"
```

4. Apply the changes/updates to the infrastructure to create it
5. Apply the changes/updates to the infrastructure to create it

```bash
# execute the tf plan
# `--auto-approve` is to prevent tf from prompting you to say y/n to apply the plan
terraform apply --auto-approve -var-file="<filename>.tfvars"
```

5. To destroy your infrastructure, use the command:
6. To destroy your infrastructure, use the command:

```bash
terraform destroy --auto-approve -var-file="<filename>.tfvars"
Expand Down

0 comments on commit 4ab0ffa

Please sign in to comment.