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
86
87
88
89
90
91
92
93
|
# File 'lib/tfctl/generator.rb', line 17
def make(
account_id:,
account_name:,
execution_role:,
profiles:,
config:,
region: 'eu-west-1',
tf_version: '>= 0.12.0',
aws_provider_version: '~> 2.14',
target_dir: "#{PROJECT_ROOT}/.tfctl/#{config[:config_name]}/#{account_name}"
)
FileUtils.mkdir_p target_dir
terraform_block = {
'terraform' => {
'required_version' => tf_version,
'backend' => {
's3' => {
'bucket' => config[:tf_state_bucket],
'key' => "#{account_name}/tfstate",
'region' => config[:tf_state_region],
'role_arn' => config[:tf_state_role_arn],
'dynamodb_table' => config[:tf_state_dynamodb_table],
'encrypt' => 'true',
},
},
},
}
write_json_block("#{target_dir}/terraform.tf.json", terraform_block)
provider_block = {
'provider' => {
'aws' => {
'version' => aws_provider_version,
'region' => region,
'assume_role' => {
'role_arn' => "arn:aws:iam::#{account_id}:role/#{execution_role}",
},
},
},
}
write_json_block("#{target_dir}/provider.tf.json", provider_block)
vars_block = {
'variable' => {
'config' => {
'type' => 'string',
},
},
}
write_json_block("#{target_dir}/vars.tf.json", vars_block)
config_block = { 'config' => config.to_json }
write_json_block("#{target_dir}/config.auto.tfvars.json", config_block)
FileUtils.rm Dir.glob("#{target_dir}/profile_*.tf.json")
profiles.each do |profile|
profile_block = {
'module' => {
profile => {
'source' => "../../../profiles/#{profile}",
'config' => '${var.config}',
'providers' => {
'aws' => 'aws',
},
},
},
}
write_json_block("#{target_dir}/profile_#{profile}.tf.json", profile_block)
end
end
|