terraform_api_gateway/Taskfile.yml

76 lines
2.3 KiB
YAML

version: '3'
env:
ENV: "{{.ENV}}"
SERVICES_DIRECTORY: "{{.ROOT_DIR}}/services"
TERRAFORM_DIRECTORY: "{{.ROOT_DIR}}/terraform/code"
CONFIG_DIRECTORY: "../configs"
TFVARS: "{{.CONFIG_DIRECTORY}}/{{.ENV}}/terraform.tfvars"
BACKEND: "{{.CONFIG_DIRECTORY}}/{{.ENV}}/backend.conf"
DEFAULT_VARS: "{{.CONFIG_DIRECTORY}}/_default_values/terraform.tfvars"
TF_IN_AUTOMATION: true
PLAN: plan.tfplan
JSON_PLAN_FILE: tfplan.json
tasks:
check_env:
preconditions:
- sh: "[ $ENV != '' ]"
msg: "Variable ENV is not set"
cmds:
- cmd: 'echo "Using environment: $ENV"'
silent: true
check_jq:
preconditions:
- sh: "command -v jq >/dev/null 2>&1 || { echo >&2 \"jq is not installed. Aborting.\"; exit 1; }"
msg: "jq is not installed"
terraform_init:
deps: [check_env]
dir: "{{.TERRAFORM_DIRECTORY}}"
cmds:
- terraform init -backend-config=${BACKEND} -input=false
- terraform workspace new $ENV || terraform workspace select $ENV
build_services:
dir: "{{.SERVICES_DIRECTORY}}"
cmds:
- zip hello_world.zip hello_world.py
sources:
- hello_world.py
generates:
- hello_world.zip
terraform_plan:
deps: [check_jq, terraform_init, build_services]
dir: "{{.TERRAFORM_DIRECTORY}}"
sources:
- '*.tf'
- '../../doc/*'
- '{{.CONFIG_DIRECTORY}}/_default_values/terraform.tfvars'
- '{{.CONFIG_DIRECTORY}}/{{.ENV}}/terraform.tfvars'
generates:
- '{{.PLAN}}'
- '{{.JSON_PLAN_FILE}}'
cmds:
- terraform fmt -recursive
- terraform validate
- terraform plan --var-file=${DEFAULT_VARS} --var-file=${TFVARS} -out=${PLAN} -input=false
- terraform show --json ${PLAN} | jq -r '([.resource_changes[]?.change.actions?]|flatten)|{"create":(map(select(.=="create"))|length),"update":(map(select(.=="update"))|length),"delete":(map(select(.=="delete"))|length)}' > ${JSON_PLAN_FILE}
terraform_apply:
deps: [terraform_plan]
dir: "{{.TERRAFORM_DIRECTORY}}"
sources:
- '{{.PLAN}}'
cmds:
- terraform apply --var-file=${DEFAULT_VARS} --var-file=${TFVARS} -input=false -auto-approve
terraform_destroy:
deps: [terraform_init]
dir: "{{.TERRAFORM_DIRECTORY}}"
cmds:
- terraform destroy --var-file=${DEFAULT_VARS} --var-file=${TFVARS} -input=false -auto-approve
- rm ${PLAN}