Skip to content

Commit f288ad6

Browse files
authored
Merge pull request #6 from rhythmictech/example
Examples and fancy tools
2 parents 5c3289c + 0531d74 commit f288ad6

File tree

15 files changed

+327
-66
lines changed

15 files changed

+327
-66
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @rhythmictech/engineering

.github/workflows/check.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
name: pre-commit-check
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- develop
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: macOS-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
with:
16+
ref: ${{ github.head_ref }}
17+
- name: Install prerequisites
18+
run: ./bin/install-macos.sh
19+
- name: initialize Terraform
20+
run: terraform init --backend=false
21+
- uses: actions/cache@v1
22+
with:
23+
path: ~/.cache/pre-commit
24+
key: pre-commit|${{ hashFiles('.pre-commit-config.yaml') }}
25+
restore-keys: |
26+
pre-commit
27+
- name: pre-commit run all
28+
run: |
29+
pre-commit run -a
30+
env:
31+
AWS_DEFAULT_REGION: us-east-1
32+
SKIP: terraform_tflint_deep,no-commit-to-branch
33+
- uses: stefanzweifel/git-auto-commit-action@v4
34+
if: ${{ failure() }}
35+
with:
36+
commit_message: Apply automatic changes
37+
commit_options: "--no-verify"
38+
# Optional commit user and author settings
39+
commit_user_name: Linter Bot
40+
commit_user_email: noreply@rhythmictech.com
41+
commit_author: Linter Bot <actions@github.com>

.pre-commit-config.yaml

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
1-
---
21
repos:
3-
- repo: git://github.com/antonbabenko/pre-commit-terraform
4-
rev: v1.24.0
5-
hooks:
6-
- id: terraform_fmt
7-
- id: terraform_docs
8-
- repo: https://github.com/pre-commit/pre-commit-hooks
9-
rev: v2.4.0
10-
hooks:
11-
- id: end-of-file-fixer
12-
- id: trailing-whitespace
13-
- id: no-commit-to-branch
2+
- repo: https://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.31.0
4+
hooks:
5+
- id: terraform_docs
6+
always_run: true
7+
args:
8+
- --args=--sort-by-required
9+
- id: terraform_fmt
10+
- id: terraform_tflint
11+
alias: terraform_tflint_deep
12+
name: terraform_tflint_deep
13+
args:
14+
- --args=--deep
15+
- id: terraform_tflint
16+
alias: terraform_tflint_nocreds
17+
name: terraform_tflint_nocreds
18+
- id: terraform_tfsec
19+
- repo: local
20+
hooks:
21+
- id: terraform_validate
22+
name: terraform_validate
23+
entry: |
24+
bash -c '
25+
AWS_DEFAULT_REGION=us-east-1
26+
declare -a DIRS
27+
for FILE in "$@"
28+
do
29+
DIRS+=($(dirname "$FILE"))
30+
done
31+
for DIR in $(printf "%s\n" "${DIRS[@]}" | sort -u)
32+
do
33+
cd $(dirname "$FILE")
34+
terraform init --backend=false
35+
terraform validate .
36+
done
37+
'
38+
language: system
39+
verbose: true
40+
files: \.tf(vars)?$
41+
exclude: examples
42+
- repo: https://github.com/pre-commit/pre-commit-hooks
43+
rev: v3.0.0
44+
hooks:
45+
- id: check-case-conflict
46+
- id: check-json
47+
- id: check-merge-conflict
48+
- id: check-symlinks
49+
- id: check-yaml
50+
args:
51+
- --unsafe
52+
- id: end-of-file-fixer
53+
- id: trailing-whitespace
54+
- id: mixed-line-ending
55+
args:
56+
- --fix=lf
57+
- id: no-commit-to-branch
58+
- id: pretty-format-json
59+
args:
60+
- --autofix
61+
- --top-keys=name,Name
62+
- id: trailing-whitespace
63+
args:
64+
- --markdown-linebreak-ext=md
65+
exclude: README.md

.tflint.hcl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
config {
2+
module = true
3+
deep_check = false
4+
}
5+
6+
rule "terraform_deprecated_interpolation" {
7+
enabled = true
8+
}
9+
10+
rule "terraform_unused_declarations" {
11+
enabled = true
12+
}
13+
14+
rule "terraform_comment_syntax" {
15+
enabled = true
16+
}
17+
18+
rule "terraform_documented_outputs" {
19+
enabled = true
20+
}
21+
22+
rule "terraform_documented_variables" {
23+
enabled = true
24+
}
25+
26+
rule "terraform_typed_variables" {
27+
enabled = true
28+
}
29+
30+
rule "terraform_module_pinned_source" {
31+
enabled = true
32+
}
33+
34+
rule "terraform_naming_convention" {
35+
enabled = true
36+
format = "snake_case"
37+
}
38+
39+
rule "terraform_required_version" {
40+
enabled = true
41+
}
42+
43+
rule "terraform_required_providers" {
44+
enabled = true
45+
}

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# terraform-aws-backend
2-
3-
[![](https://github.com/rhythmictech/terraform-aws-backend/workflows/check/badge.svg)](https://github.com/rhythmictech/terraform-aws-backend/actions)
2+
[![](https://github.com/rhythmictech/terraform-aws-backend/workflows/pre-commit-check/badge.svg)](https://github.com/rhythmictech/terraform-aws-backend/actions) <a href="https://twitter.com/intent/follow?screen_name=RhythmicTech"><img src="https://img.shields.io/twitter/follow/RhythmicTech?style=social&logo=RhythmicTech" alt="follow on Twitter"></a>
43

54
Creates a backend S3 bucket and DynamoDB table for managing Terraform state. Useful for bootstrapping a new
65
environment. This module supports cross-account state management, using a centralized account that holds the S3 bucket and KMS key.
@@ -55,19 +54,31 @@ region = "us-east-1"
5554
```
5655

5756
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
57+
## Requirements
58+
59+
| Name | Version |
60+
|------|---------|
61+
| terraform | >= 0.12.19 |
62+
| aws | ~> 2.37 |
63+
64+
## Providers
65+
66+
| Name | Version |
67+
|------|---------|
68+
| aws | ~> 2.37 |
69+
5870
## Inputs
5971

6072
| Name | Description | Type | Default | Required |
61-
|------|-------------|:----:|:-----:|:-----:|
62-
| allowed\_account\_ids | Account IDs that are allowed to access the bucket/KMS key | list(string) | `[]` | no |
63-
| bucket | Name of bucket to create \(do not provide if using `remote_bucket`\) | string | `""` | no |
64-
| kms\_key\_id | ARN for KMS key for all encryption operations. | string | `""` | no |
65-
| logging\_target\_bucket | The name of the bucket that will receive the log objects | string | `var.bucket` | no |
66-
| logging\_target\_prefix | A key prefix for log objects | string | `"TFStateLogs/"` | no |
67-
| region | Region bucket will be created in | string | n/a | yes |
68-
| remote\_bucket | If specified, the remote bucket will be used for the backend. A new bucket will not be created | string | `""` | no |
69-
| table | Name of Dynamo Table to create | string | `"tf-locktable"` | no |
70-
| tags | Mapping of any extra tags you want added to resources | map(string) | `{}` | no |
73+
|------|-------------|------|---------|:--------:|
74+
| allowed\_account\_ids | Account IDs that are allowed to access the bucket/KMS key | `list(string)` | `[]` | no |
75+
| bucket | Name of bucket to create (do not provide if using `remote_bucket`) | `string` | `""` | no |
76+
| kms\_key\_id | ARN for KMS key for all encryption operations. | `string` | `""` | no |
77+
| logging\_target\_bucket | The name of the bucket that will receive the log objects | `string` | `null` | no |
78+
| logging\_target\_prefix | A key prefix for log objects | `string` | `"TFStateLogs/"` | no |
79+
| remote\_bucket | If specified, the remote bucket will be used for the backend. A new bucket will not be created | `string` | `""` | no |
80+
| table | Name of Dynamo Table to create | `string` | `"tf-locktable"` | no |
81+
| tags | Mapping of any extra tags you want added to resources | `map(string)` | `{}` | no |
7182

7283
## Outputs
7384

bin/install-macos.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
echo 'installing brew packages'
4+
brew update
5+
brew tap liamg/tfsec
6+
brew install tfenv tflint terraform-docs pre-commit liamg/tfsec/tfsec coreutils
7+
brew upgrade tfenv tflint terraform-docs pre-commit liamg/tfsec/tfsec coreutils
8+
9+
echo 'installing pre-commit hooks'
10+
pre-commit install
11+
12+
echo 'setting pre-commit hooks to auto-install on clone in the future'
13+
git config --global init.templateDir ~/.git-template
14+
pre-commit init-templatedir ~/.git-template
15+
16+
echo 'installing terraform with tfenv'
17+
tfenv install min-required
18+
tfenv use min-required

examples/basic/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Basic Backend Example
2+
Creates resources for a secure backend in AWS
3+
4+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
5+
## Requirements
6+
7+
No requirements.
8+
9+
## Providers
10+
11+
| Name | Version |
12+
|------|---------|
13+
| aws | n/a |
14+
15+
## Inputs
16+
17+
No input.
18+
19+
## Outputs
20+
21+
No output.
22+
23+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/basic/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
data "aws_caller_identity" "current" {}
2+
data "aws_region" "current" {}
3+
4+
locals {
5+
env = "sandbox"
6+
name = "example"
7+
namespace = "aws-rhythmic-sandbox"
8+
owner = "Rhythmictech Engineering"
9+
10+
extra_tags = {
11+
delete_me = "please"
12+
purpose = "testing"
13+
}
14+
}
15+
16+
module "tags" {
17+
source = "rhythmictech/tags/terraform"
18+
version = "1.0.0"
19+
20+
names = [local.name, local.env, local.namespace]
21+
22+
tags = merge({
23+
"Env" = local.env,
24+
"Namespace" = local.namespace,
25+
"Owner" = local.owner
26+
}, local.extra_tags)
27+
}
28+
29+
module "backend" {
30+
source = "../.."
31+
32+
bucket = "${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}-${module.tags.name}"
33+
region = data.aws_region.current.name
34+
tags = module.tags.tags
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Backend Example with external Logging
2+
Creates resources for a secure backend in AWS
3+
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Requirements
7+
8+
No requirements.
9+
10+
## Providers
11+
12+
No provider.
13+
14+
## Inputs
15+
16+
No input.
17+
18+
## Outputs
19+
20+
No output.
21+
22+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

0 commit comments

Comments
 (0)