-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.hcl
85 lines (74 loc) · 2.63 KB
/
common.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
locals {
# Read project config file
config = jsondecode(file("${get_parent_terragrunt_dir()}/config.json"))
# Extract values from folder namespacing
// "environments/<account>/<region>/<instance>"
path = path_relative_to_include()
path_split = split("/", local.path)
component = "github-aws-ci-roles"
account = local.path_split[1]
aws_region = local.path_split[2]
aws_account_id = local.config.aws.accounts[local.account]
backend_filename = local.config.terragrunt.backend_filename
tags = merge(
{
Location = "${local.config.base.git_url}/${path_relative_to_include()}"
}
)
}
# DRY terragrunt actions
# https://terragrunt.gruntwork.io/docs/features/keep-your-cli-flags-dry/
terraform {
extra_arguments "plan" {
commands = ["plan"]
arguments = ["-out=${get_terragrunt_dir()}/tgplan.out"]
}
extra_arguments "apply" {
commands = ["apply"]
arguments = ["${get_terragrunt_dir()}/tgplan.out"]
}
source = "git::https://github.com/kloud-cnf/terraform-aws-ci-iam-roles//?ref=v0.3.1"
}
# Generate an AWS provider block
# https://terragrunt.gruntwork.io/docs/getting-started/quick-start/#example
generate "aws_provider" {
path = "_provider.tf"
if_exists = "overwrite_terragrunt"
contents = file("${get_parent_terragrunt_dir()}/templates/aws_provider.tf.tmpl")
}
generate "terragrunt_local_vars" {
path = "_locals.tf"
if_exists = "overwrite"
contents = <<-EOF
locals {
terragrunt_dir = "${get_terragrunt_dir()}"
parent_terragrunt_dir = "${get_parent_terragrunt_dir()}"
template_dir = "${get_parent_terragrunt_dir()}/templates"
backend_filename = "${local.backend_filename}"
aws_region = "${local.aws_region}"
}
EOF
}
# Configure root level variables that all resources can inherit.
inputs = merge(
{
aws_region = local.aws_region == "global" ? "${local.config.aws.home_region}" : local.aws_region
aws_account_id = local.aws_account_id
}
)
# Configure Terragrunt to automatically store tfstate files in an S3 bucket
remote_state {
backend = "s3"
config = {
encrypt = true
bucket = "terraform-state-${local.aws_account_id}"
key = "${join("/", compact([local.component, local.aws_region]))}/terraform.tfstate"
region = "eu-west-1" # one state bucket per account, multi region support via file path
dynamodb_table = "terraform-state-lock-${local.aws_account_id}"
disable_bucket_update = true
}
generate = {
path = local.backend_filename
if_exists = "overwrite_terragrunt"
}
}