From 79ba11ef6e31b1c3705c74ccd1c8c4b085aafb19 Mon Sep 17 00:00:00 2001 From: Erik van Dam Date: Wed, 13 Jan 2021 22:46:55 +0100 Subject: [PATCH] Initial commit --- .github/workflows/check.yml | 31 +++++++++++++++++++++++++++++++ .gitignore | 4 ++++ .pre-commit-config.yaml | 23 +++++++++++++++++++++++ LICENSE | 11 +++++++++++ README.md | 20 ++++++++++++++++++++ examples/simple/main.tf | 6 ++++++ main.tf | 26 ++++++++++++++++++++++++++ outputs.tf | 4 ++++ variables.tf | 27 +++++++++++++++++++++++++++ versions.tf | 10 ++++++++++ 10 files changed, 162 insertions(+) create mode 100644 .github/workflows/check.yml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 examples/simple/main.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..e42cd0b --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,31 @@ +name: Check +on: [ push, pull_request ] + +jobs: + validate: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: hashicorp/setup-terraform@v1 + with: + terraform_version: 0.14.0 + + - name: Validate Module + env: + AWS_REGION: 'eu-west-1' + run: | + terraform init -backend=false + terraform validate + terraform fmt -check + - name: Validate Examples + env: + AWS_REGION: 'eu-west-1' + run: | + for example in $(find examples -maxdepth 1 -mindepth 1 -type d); do + cd $example + terraform init -backend=false + terraform validate + terraform fmt -check + cd - + done diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..284ab27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.tfvars +*.tfstate* +*.lock.hcl* +.terraform/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..03b6fc8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,23 @@ +repos: + - repo: git://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: check-json + - id: check-merge-conflict + - id: check-yaml + - id: detect-private-key + - id: pretty-format-json + args: + - --autofix + - id: trailing-whitespace + + - repo: git://github.com/igorshubovych/markdownlint-cli + rev: v0.26.0 + hooks: + - id: markdownlint + + - repo: git://github.com/antonbabenko/pre-commit-terraform + rev: v1.45.0 + hooks: + - id: terraform_docs + - id: terraform_fmt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..51fca54 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..dc0c929 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# AWS S3 Bootstrap State Module + +A Terraform module to create and manage Terraform state on AWS. +Available through the [Terraform registry](https://registry.terraform.io/modules/eriktisme/bootstrap/aws/latest). + +## Terraform versions + +Terraform 0.13 and above are supported. + +## Usage example + +- A simple example is contained in the [examples directory](./examples/simple). + +## Authors + +- [Erik van Dam](https://github.com/eriktisme) + +## License + +The Apache License, Version 2.0. Please see [License File](LICENSE) for more information. diff --git a/examples/simple/main.tf b/examples/simple/main.tf new file mode 100644 index 0000000..d094310 --- /dev/null +++ b/examples/simple/main.tf @@ -0,0 +1,6 @@ +module "bootstrap" { + source = "../../" + + project_alias = "simple" + region = "eu-west-1" +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..5c29da8 --- /dev/null +++ b/main.tf @@ -0,0 +1,26 @@ +module "terraform_state_bucket" { + source = "eriktisme/s3-bucket/aws" + version = "0.1.0" + + bucket = "${var.project_alias}-${var.bucket_purpose}-${var.region}" + + tags = merge(var.tags) +} + +resource "aws_dynamodb_table" "terraform_state_lock" { + hash_key = "LockID" + name = var.table_name + read_capacity = 2 + write_capacity = 2 + + server_side_encryption { + enabled = true + } + + attribute { + name = "LockID" + type = "S" + } + + tags = merge(var.tags) +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..938b12b --- /dev/null +++ b/outputs.tf @@ -0,0 +1,4 @@ +output "dynamodb_table" { + description = "The name of the DynamoDB table created for the Terraform state." + value = aws_dynamodb_table.terraform_state_lock.id +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..fd85e9c --- /dev/null +++ b/variables.tf @@ -0,0 +1,27 @@ +variable "table_name" { + description = "The table name for the Terraform state lock." + default = "terraform-state-lock" + type = string +} + +variable "project_alias" { + description = "The AWS account alias." + type = string +} + +variable "bucket_purpose" { + description = "The identification of the bucket's purpose." + default = "terraform-state" + type = string +} + +variable "region" { + description = "The AWS region the account will be bootstrapped in." + type = string +} + +variable "tags" { + description = "A mapping of tags to assign to the table." + type = map(string) + default = {} +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..6d94e8e --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 0.13" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.23.0" + } + } +}