From d4e8971861fa87070994a7ebcecfbc1c526240e4 Mon Sep 17 00:00:00 2001 From: Asyraf Norafandi Date: Wed, 11 Sep 2024 14:16:49 +0800 Subject: [PATCH] Initial commit --- .editorconfig | 32 ++++++++ .github/CODEOWNERS | 8 ++ .github/FUNDING.yml | 3 + .github/ISSUE_TEMPLATE/bug_report.yml | 48 ++++++++++++ .github/pr-labeler.yml | 9 +++ .github/workflows/lint.yml | 81 ++++++++++++++++++++ .github/workflows/pr-labeler.yml | 20 +++++ .github/workflows/semantic-title.yml | 38 +++++++++ .github/workflows/stale.yml | 31 ++++++++ .github/workflows/trivy.yml | 35 +++++++++ .gitignore | 36 +++++++++ .pre-commit-config.yaml | 10 +++ .terraform-docs.yml | 8 ++ LICENSE | 21 +++++ README.md | 106 ++++++++++++++++++++++++++ data.tf | 0 examples/complete/main.tf | 4 + examples/complete/versions.tf | 10 +++ main.tf | 1 + outputs.tf | 0 variables.tf | 0 versions.tf | 10 +++ 22 files changed, 511 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/CODEOWNERS create mode 100644 .github/FUNDING.yml create mode 100644 .github/ISSUE_TEMPLATE/bug_report.yml create mode 100644 .github/pr-labeler.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/pr-labeler.yml create mode 100644 .github/workflows/semantic-title.yml create mode 100644 .github/workflows/stale.yml create mode 100644 .github/workflows/trivy.yml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 .terraform-docs.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 data.tf create mode 100644 examples/complete/main.tf create mode 100644 examples/complete/versions.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e282a8e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,32 @@ +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +# Override for Makefile +[{Makefile,makefile,GNUmakefile}] +indent_style = tab +indent_size = 4 + +[Makefile.*] +indent_style = tab +indent_size = 4 + +[{*.yaml,*.yml,*.md}] +indent_style = space +indent_size = 2 + +[*.sh] +indent_style = tab +indent_size = 2 + +[*.{tf,tfvars,tpl,variant}] +indent_style = space +indent_size = 2 + +[*.json] +insert_final_newline = false diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..71814d6 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,8 @@ +# This is a comment. +# Each line is a file pattern followed by one or more owners. + +# These owners will be the default owners for everything in +# the repo. Unless a later match takes precedence, +# @global-owner1 and @global-owner2 will be requested for +# review when someone opens a pull request. +* asyrafnorafandi@gmail.com diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..e0971a6 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms +--- +github: asyrafnorafandi diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml new file mode 100644 index 0000000..be73c66 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -0,0 +1,48 @@ +name: Bug Report +description: File a bug report +labels: ["triage"] +body: + - type: markdown + attributes: + value: | + Thanks for reporting a bug 🐛! + + Please search open/closed issues before submitting. Someone might have had the similar problem before 😉! + + - type: textarea + id: description + attributes: + label: Description + description: A brief description of the issue. + validations: + required: true + + - type: textarea + id: steps + attributes: + label: Steps to reproduce + description: | + Explain how to reproduce the issue in the development environment. + + - type: input + id: version + attributes: + label: Version + description: The release version of the backend or branch/commit. + placeholder: v6.1.0 + validations: + required: true + + - type: input + id: os-version + attributes: + label: Operating system + description: The operating system this issue occurred with. + placeholder: Linux/macOS/Windows + + - type: textarea + id: additional-information + attributes: + label: Additional information + description: | + Use this section to provide any additional information you might have (e.g screenshots or screencasts). diff --git a/.github/pr-labeler.yml b/.github/pr-labeler.yml new file mode 100644 index 0000000..072e086 --- /dev/null +++ b/.github/pr-labeler.yml @@ -0,0 +1,9 @@ +"kind/ci": ci/* +"kind/bug": ["fix/*", "bug/*"] +"kind/chore": chore/* +"kind/refactor": refactor/* +"kind/release": release/* +"kind/revert": revert/* +"kind/security": security/* +"kind/documentation": ["documentation/*", "doc/*"] +"kind/enhancement": ["feat/*", "feature/*"] diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..8d0167f --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,81 @@ +name: Lint & Formatting + +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + format: + runs-on: ubuntu-latest + name: Format HCL + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Terraform format + uses: dflook/terraform-fmt@v1 + + - name: Reviewdog suggester + uses: reviewdog/action-suggester@v1 + with: + tool_name: "terraform fmt -recursive" + cleanup: false + + - name: Status check + shell: bash + run: git diff --exit-code + + lint-find-dirs: + runs-on: ubuntu-latest + name: Find modules + steps: + - uses: actions/checkout@v4 + + - id: set-matrix + # Ignore directories whose names start with .terraform or obsolete + run: | + matrix=$(find . -type d \( -name '.terraform*' -o -name 'obsolete*' \) -prune -o \ + -name '*.tf' \ + -not -path '*/.terraform/*' \ + -exec dirname {} \; \ + | sort \ + | uniq \ + | jq --raw-input --slurp 'split("\n")| map(select(. != ""))') + echo "matrix=$(echo $matrix)" >> $GITHUB_OUTPUT + outputs: + tfdirs_matrix: ${{ steps.set-matrix.outputs.matrix }} + + lint: + runs-on: ubuntu-latest + name: Lint (${{ matrix.tfdir }}) + needs: lint-find-dirs + strategy: + fail-fast: false + matrix: + tfdir: ${{ fromJson(needs.lint-find-dirs.outputs.tfdirs_matrix) }} + env: + TFLINT_PLUGIN_DIR: ${{ github.workspace }}/.tflint.d/plugins + TFLINT_CACHE_VER: 1 # Increment this to force a cache refresh + steps: + - uses: actions/checkout@v4 + + - uses: hashicorp/setup-terraform@v3 + + - run: terraform init + working-directory: ${{ matrix.tfdir }} + + - name: tflint + uses: reviewdog/action-tflint@v1 + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-check + fail_on_error: true + tflint_init: true + working_directory: ${{ matrix.tfdir }} + filter_mode: added + tflint_version: "v0.49.0" + # tflint_rulesets: Optional diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml new file mode 100644 index 0000000..7a410cb --- /dev/null +++ b/.github/workflows/pr-labeler.yml @@ -0,0 +1,20 @@ +name: PR Labeler + +on: + pull_request: + types: [opened] + +permissions: + contents: read + +jobs: + pr-labeler: + permissions: + contents: read # for TimonVS/pr-labeler-action to read config file + pull-requests: write # for TimonVS/pr-labeler-action to add labels in PR + runs-on: ubuntu-latest + steps: + - uses: TimonVS/pr-labeler-action@v5 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + configuration-path: .github/pr-labeler.yml # optional, .github/pr-labeler.yml is the default value diff --git a/.github/workflows/semantic-title.yml b/.github/workflows/semantic-title.yml new file mode 100644 index 0000000..82bd585 --- /dev/null +++ b/.github/workflows/semantic-title.yml @@ -0,0 +1,38 @@ +name: "Semantic Title Check" + +on: + pull_request: + types: + - opened + - edited + - synchronize + - reopened + +permissions: + pull-requests: read + +jobs: + main: + name: Validate PR title + runs-on: ubuntu-latest + steps: + # https://github.com/commitizen/conventional-commit-types/blob/master/index.json + - uses: amannn/action-semantic-pull-request@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + types: | + bug + fix + hotfix + chore + ci + feat + doc + epic + perf + refactor + release + revert + test + security diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 0000000..38e047b --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,31 @@ +--- +name: Close inactive issues + +on: + schedule: + - cron: "0 0 * * 1" + +jobs: + close-issues: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + steps: + - uses: actions/stale@v8 + with: + days-before-stale: 120 + days-before-close: 60 + exempt-issue-labels: bug,pinned,security,planned + exempt-pr-labels: bug,pinned,security,planned + stale-issue-label: "stale" + stale-pr-label: "stale" + stale-issue-message: | + This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! + close-issue-message: | + This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. + stale-pr-message: | + This pr has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! + close-pr-message: | + This pr has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/trivy.yml b/.github/workflows/trivy.yml new file mode 100644 index 0000000..5c2418b --- /dev/null +++ b/.github/workflows/trivy.yml @@ -0,0 +1,35 @@ +name: Trivy + +on: + push: + branches: + - main + pull_request: + +jobs: + build: + permissions: + contents: read # for actions/checkout to fetch code + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status + name: Vulnerability scan + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Run Trivy vulnerability scanner in IaC mode + uses: aquasecurity/trivy-action@0.24.0 + with: + scan-type: "config" + hide-progress: true + format: "sarif" + output: "trivy-results.sarif" + exit-code: "1" + ignore-unfixed: true + severity: "CRITICAL,HIGH" + + - name: Upload Trivy scan results to GitHub Security tab + uses: github/codeql-action/upload-sarif@v3 + with: + sarif_file: "trivy-results.sarif" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ad70b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +################################################################# +# Default .gitignore content for all terraform-aws-modules below +################################################################# + +.DS_Store + +# Local .terraform directories +**/.terraform/* + +# Terraform lockfile +.terraform.lock.hcl + +# .tfstate files +*.tfstate +*.tfstate.* +*.tfplan + +# Crash log files +crash.log + +# Exclude all .tfvars files, which are likely to contain sentitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Ignore CLI configuration files +.terraformrc +terraform.rc diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..38b9c32 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/antonbabenko/pre-commit-terraform + rev: "v1.94.1" # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases + hooks: + - id: terraform_fmt + - id: terraform_tflint + - id: terraform_trivy + - id: terraform_docs + args: + - --args=--config=.terraform-docs.yml diff --git a/.terraform-docs.yml b/.terraform-docs.yml new file mode 100644 index 0000000..852bf21 --- /dev/null +++ b/.terraform-docs.yml @@ -0,0 +1,8 @@ +formatter: "markdown" + +output: + file: README.md + mode: inject + +recursive: + enabled: false diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..18d15d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Asyraf Norafandi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..350cc50 --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# Terraform Module Template + +This repository is a boilerplate to start writing your own Terraform Module + +## Features: + +### Terraform Linting & Formatting + +terraform + +### IAC Security Scanning + +Trivy + +### Automatic Terraform Doc Generation + +Terraform Docs + +## Prerequisites + +Install the follow tools to your local machine: + +```bash +# Terraform +$ brew install terraform + +# Terraform lint to enforce best practices in writing Terraform code +# Reference: https://github.com/terraform-linters/tflint +$ brew install tflint + +# Trivy for IAC Security scanning +# Reference: https://github.com/aquasecurity/trivy +$ brew install trivy + +# Terraform Docs to autogenerate Terraform Module documentation +# Reference: https://terraform-docs.io/user-guide/installation/ +$ brew install terraform-docs + +# Pre Commit hooks to enforce Terraform Docs generation on every commit +# Reference: https://pre-commit.com/#install +$ brew install pre-commit +``` + +## Getting started + +On the top right of this repo, click on `Use this template` -> `Create new repository`. Then fill in the name of the repository with the following format, `terraform--`. Eg. `terraform-aws-private-eks`. + +```bash +$ git clone + +$ cd + +# Install pre-commit hooks +$ pre-commit install-hooks + +# Make code changes and try to commit. You should see the following output +Terraform fmt............................................................Passed +Terraform validate with tflint...........................................Passed +Terraform validate with trivy............................................Passed +Terraform docs...........................................................Passed +``` + +You are all set to go to write your custom Terraform Module! 🚀 + +--- + +### Note: Everything below this line is generated via terraform-docs + +--- + + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.0 | + +## Providers + +No providers. + +## Modules + +No modules. + +## Resources + +No resources. + +## Inputs + +No inputs. + +## Outputs + +No outputs. + diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..e69de29 diff --git a/examples/complete/main.tf b/examples/complete/main.tf new file mode 100644 index 0000000..8f5bca0 --- /dev/null +++ b/examples/complete/main.tf @@ -0,0 +1,4 @@ +# Example usage of the terraform module +# module "main" { +# source = "../../" +# } diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf new file mode 100644 index 0000000..734df5e --- /dev/null +++ b/examples/complete/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0" + + # required_providers { + # aws = { + # source = "hashicorp/aws" + # version = ">= 5.65.0" + # } + # } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/main.tf @@ -0,0 +1 @@ + diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e69de29 diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..734df5e --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0" + + # required_providers { + # aws = { + # source = "hashicorp/aws" + # version = ">= 5.65.0" + # } + # } +}