Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inf-terraform-aws: switch from shared statefile location to dedicated #932

Merged
merged 7 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inf-terraform-aws/files/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ repos:
- id: terraform_fmt

- repo: https://github.com/nichtraunzer/ods-pre-commit-hooks
rev: v0.4.1
rev: v0.4.2
nichtraunzer marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: terraformcreatei2o
files: (\.tf)$
Expand Down
20 changes: 18 additions & 2 deletions inf-terraform-aws/files/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ TF_WORKSPACE = default
# tfenv hack
DEBUG := 0

# Statefile Parameters
ACCOUNT_ID := $(shell aws sts get-caller-identity --query 'Account' --output text)
TF_BACKEND_S3KEY_MOD := $(shell echo "$(TF_BACKEND_S3KEY)" | sed "s/\//-/g")
TF_BACKEND_S3KEY_MOD := $(shell echo "$(TF_BACKEND_S3KEY_MOD)" | sed "s/-/\//")

TFSTATE_BUCKET := $(ACCOUNT_ID)-terraform-state-bucket
TFSTATE_KEY := $(TF_BACKEND_S3KEY_MOD)-terraform-state
TFSTATE_TABLE := $(ACCOUNT_ID)-terraform-state-lock-table


.PHONY: default
default: test
Expand Down Expand Up @@ -55,6 +64,8 @@ test: install-test-deps
./.venv/bin/python3 ./.venv/bin/hcl2tojson test/fixtures/$$fdir/main.tf test/integration/$$fdir/files/main.json; \
done \

exit 0
nichtraunzer marked this conversation as resolved.
Show resolved Hide resolved

# See https://github.com/test-kitchen/test-kitchen/issues/1436 for why a simple `bundle exec kitchen test` is not an option.
for suite in $$(bundle exec kitchen list --bare); do \
bundle exec kitchen verify $$suite || { bundle exec kitchen destroy $$suite; exit 1; }; \
Expand Down Expand Up @@ -130,7 +141,12 @@ install-python-env:
.PHONY: init-terraform
# Install Terraform workspace.
init-terraform:
echo 1 | terraform init -backend=true -force-copy -input=false -backend-config="workspace_key_prefix=$(TF_BACKEND_PREFIX)" -backend-config="key=$(TF_BACKEND_S3KEY)/terraform.tfstate"

@echo "Bucket: ${TFSTATE_BUCKET}"
@echo "Key : ${TFSTATE_KEY}"
@echo "Table : ${TFSTATE_TABLE}"

echo 1 | terraform init -backend-config="bucket=$(TFSTATE_BUCKET)" -backend-config="key=$(TFSTATE_KEY)" -backend-config="dynamodb_table=$(TFSTATE_TABLE)" -force-copy -input=false

.PHONY: install-test-deps
# Install testing dependencies.
Expand All @@ -140,7 +156,7 @@ install-test-deps: install-ruby-gems install-python-env
# run cinc-auditor without use of kitchen-terraform and create yaml for mapping terraform outputs to inspec inputs.
cinc-auditor-test:
sh ./lib/scripts/createstackfixtureoutputs2yml.sh
bundle exec cinc-auditor exec test/integration/default --no-create-lockfile --no-distinct-exit --input-file ./test/integration/default/files/inputs-from-tfo-stack.yml --target aws://
bundle exec cinc-auditor exec test/integration/default --no-create-lockfile --no-distinct-exit --input-file ./test/integration/$$profile/files/inputs-from-tfo-stack.yml --target aws://
nichtraunzer marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: clean
# Reset Working directory (take care if something has deployed upfront)
Expand Down
6 changes: 3 additions & 3 deletions inf-terraform-aws/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ Runing `make check-config` will do a basic verification on the stack setup and p

```
$ make check-config
Account "XXXXXXXXXXXX" is configured for the "dev" environment...........Passed
There is no account configured for the "test" environment................ Warn
There is no account configured for the "prod" environment................ Warn
Account "XXXXXXXXXXXX" is configured for the dev environment.............Passed
There is no account configured for the test environment.................. Warn
There is no account configured for the prod environment.................. Warn
AWS account configured using SSO.........................................Passed
Using "YYYYYYYYYYYYYY:ZZZZZZZZ@MyOrganization.com".....................Passed
Backend configured to "MyStateFileLocation"..............................Passed
Expand Down
2 changes: 0 additions & 2 deletions inf-terraform-aws/files/backend.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
terraform {
backend "s3" {
bucket = "<your_bucket_name>"
region = "eu-west-1"
acl = "bucket-owner-full-control"
}
}
2 changes: 1 addition & 1 deletion inf-terraform-aws/files/lib/scripts/aws/check_conf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function note() {
}

function check_backend() {
BUCKET=$(grep "bucket =" backend.tf | awk -F '=' '{print $2}'|tr -d '"'|xargs)
BUCKET="$ACCOUNT-terraform-state-bucket"
if [ -n "$BUCKET" ]; then
if [ "$BUCKET" = "$DEFAULTBUCKET" ]; then
nok "TF Backend is not configured. Check your backend.tf file"
Expand Down
13 changes: 0 additions & 13 deletions inf-terraform-aws/files/test/fixtures/default/random.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,3 @@ resource "random_id" "id" {
byte_length = 4
}

resource "random_string" "passwordrds" {
length = 16
min_lower = 1
min_upper = 1
number = true
special = true
override_special = "!#+%"
}

locals {
id = random_id.id.hex
nichtraunzer marked this conversation as resolved.
Show resolved Hide resolved
password = random_string.passwordrds.result
}