From 9e8e1c32a00abc59936a1d38829235a4faa90a2e Mon Sep 17 00:00:00 2001 From: Lena Sadowska Date: Thu, 19 Oct 2023 16:35:30 +0100 Subject: [PATCH] Adding new workflow --- .github/workflows/test.yml | 35 ---------------------- README.md | 1 + docs/terraform-plan-aws.md | 59 ++++++++++++++++++++++++++++++++++++++ main.tf | 1 - test-folder-2/main.tf | 1 - test-folder/main.tf | 1 - 6 files changed, 60 insertions(+), 38 deletions(-) delete mode 100644 .github/workflows/test.yml create mode 100644 docs/terraform-plan-aws.md delete mode 100644 main.tf delete mode 100644 test-folder-2/main.tf delete mode 100644 test-folder/main.tf diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index 177ab48..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Terraform Plan Generic Modules -on: - push: - branches: - - monorepo - pull_request: - branches: - - monorepo - -jobs: - modules: - runs-on: ubuntu-latest - steps: - - uses: theappnest/terraform-monorepo-action@master - id: modules - with: - monitored: 'main.tf' - outputs: - modules: ${{ steps.modules.outputs.modules }} - - terraform: - needs: modules - strategy: - matrix: - module: ${{ fromJson(needs.modules.outputs.modules) }} - uses: appvia/appvia-cicd-workflows/.github/workflows/terraform-plan-aws.yml@monorepo - name: Terraform Run - secrets: - infracost-api-key: ${{ secrets.ORG_INFRACOST_API_KEY }} - with: - aws-account-id: 536471746696 - enable-infracost: true - terraform-version: 1.5.7 - terraform-values-file: '${{ fromJson(needs.modules.outputs.modules) }}/values/production.tfvars' - working-directory: ${{ matrix.module }} \ No newline at end of file diff --git a/README.md b/README.md index 397d037..e1b7a33 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ This repository contains a collection of GitHub Actions workflow templates that ## Workflows - [Terraform Plan & Apply (AWS)](./docs/terraform-plan-and-apply-aws.md) +- [Terraform Plan Only (AWS)](./docs/terraform-plan.md) - [Terraform Module Validation](./docs/terraform-module-validation.md) - [Terraform Module Release](./docs/terraform-module-release.md) diff --git a/docs/terraform-plan-aws.md b/docs/terraform-plan-aws.md new file mode 100644 index 0000000..ea07408 --- /dev/null +++ b/docs/terraform-plan-aws.md @@ -0,0 +1,59 @@ +# Terraform Workflow for AWS Infrastructure + +This GitHub Actions workflow template ([terraform-plan-aws.yml](../.github/workflows/terraform-plan-aws.yml)) can be used with Terraform repositories to automate the testing and planning of AWS infrastructure. The workflow performs various steps such as authentication with AWS, Terraform formatting, initialization, validatio and planning. It also adds the Terraform plan output as a comment to the associated pull request and triggers an apply action for pushes to the main branch. + +## Workflow Steps + +1. **Setup Terraform:** Terraform is fetched at the specified version (overridable via inputs). +2. **Terraform Format:** This step runs the terraform fmt command to check that all Terraform files are formatted correctly. +3. **Terraform Lint:** This step runs terraform lint to check for deprecated syntax, unused declarations, invalid types, and enforcing best practices. +4. **AWS Authentication:** The workflow uses Web Identity Federation to authenticate with AWS. The required AWS Role ARN must be provided as an input for successful authentication. + * A Web Identity Token File is also generated and stored in `/tmp/web_identity_token_file`, which can be referenced in Terraform Provider configuration blocks if required. +5. **Terraform Init:** The Terraform backend is initialised and any necessary provider plugins are downloaded. The required inputs for AWS S3 bucket name and DynamoDB table name must be provided for storing the Terraform state. +6. **Terraform Validate:** The workflow validates the Terraform configuration files using the terraform validate command to check for syntax errors and other issues. +7. **Terraform Plan:** A Terraform plan is generated with a specified values file (overridable via inputs) using the terraform plan command. +8. **Get Cost Estimate:** The infracost utility is run to get a cost estimate on the Terraform Plan output. A comment will be added to the pull request with the cost estimate. +9. **Add PR Comment:** If the workflow is triggered via a Pull Request, a comment will be added to the ticket containing the results of the previous steps. + +## Inputs + +| Input | Required? | Default Value | Description | +|-------|-------------|-----------|---------------| +| aws-role-arn | Yes | | The ARN of the AWS role to assume for authentication | +| aws-s3-bucket-name | Yes | | The name of the AWS S3 bucket to store the Terraform state | +| aws-dynamodb-table-name | Yes | | The name of the AWS DynamoDB table to use for locking | +| aws-region | No | eu-west-2 | The AWS region to deploy the infrastructure to | +| terraform-log-level | No | INFO | The log level of Terraform | +| terraform-state-key | No | ${{ github.event.repository.name }}.tfstate | The name of the Terraform state file to store in S3 | +| terraform-values-file | No | values/production.tfvars | The path to the values file to use | +| terraform-version | No | 1.5.2 | The version of Terraform to use | +| working-directory | No | . | Which working directory to navigate to | + +## Usage + +Create a new workflow file in your Terraform repository (e.g. `.github/workflows/terraform.yml`) with the below contents: +```yml +name: Terraform Plan Generic Modules +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + terraform: + uses: appvia/appvia-cicd-workflows/.github/workflows/terraform-plan-aws.yml@main + name: Terraform Run + secrets: + infracost-api-key: ${{ secrets.ORG_INFRACOST_API_KEY }} + with: + aws-account-id: 536471746696 + enable-infracost: true + terraform-version: 1.5.7 +``` + +The `aws-role-name` inputs are optional and will default to the repository name (with the respective `-ro` or `-rw` suffixes) if not provided. + +**Note:** This template may change over time, so it is recommended that you point to a tagged version rather than the main branch. \ No newline at end of file diff --git a/main.tf b/main.tf deleted file mode 100644 index b6fc4c6..0000000 --- a/main.tf +++ /dev/null @@ -1 +0,0 @@ -hello \ No newline at end of file diff --git a/test-folder-2/main.tf b/test-folder-2/main.tf deleted file mode 100644 index 23294b0..0000000 --- a/test-folder-2/main.tf +++ /dev/null @@ -1 +0,0 @@ -hello2 \ No newline at end of file diff --git a/test-folder/main.tf b/test-folder/main.tf deleted file mode 100644 index 30d74d2..0000000 --- a/test-folder/main.tf +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file