diff --git a/pkg/terraform/terraform.go b/pkg/terraform/terraform.go index 8f84f76..1f2363f 100644 --- a/pkg/terraform/terraform.go +++ b/pkg/terraform/terraform.go @@ -7,10 +7,12 @@ import ( "io" "net" "os" + "path/filepath" "regexp" "text/template" "time" + "github.com/Masterminds/sprig" "github.com/takutakahashi/loadbalancer-controller/api/v1beta1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -251,7 +253,7 @@ func (t TerraformClient) genTfvars() (string, error) { func (t TerraformClient) genWithTpl(path string) (string, error) { awsBackend := t.awsBackend if awsBackend != nil { - tmpl, err := template.ParseFiles(path) + tmpl, err := template.New(filepath.Base(path)).Funcs(sprig.TxtFuncMap()).ParseFiles(path) if err != nil { return "", err } diff --git a/src/terraform/AWSBackend/lb.tf.tpl b/src/terraform/AWSBackend/lb.tf.tpl index 5e18d20..91570ed 100644 --- a/src/terraform/AWSBackend/lb.tf.tpl +++ b/src/terraform/AWSBackend/lb.tf.tpl @@ -30,12 +30,14 @@ resource "aws_lb_target_group" "{{ $name }}_tg" { vpc_id = var.vpc_id } -resource "aws_lb_target_group_attachment" "{{ $name }}_tga" { - count = length(var.{{ $name }}_targets) +{{ range $i, $t := $tg.Targets }} + +resource "aws_lb_target_group_attachment" "{{ $name }}_tga_{{ $t.Destination.IP | replace "." "-" }}_{{ $t.Port }}" { target_group_arn = aws_lb_target_group.{{ $name }}_tg.arn - target_id = var.{{ $name }}_targets[count.index].destination + target_id = "{{ $t.Destination.IP }}" availability_zone = "ap-northeast-1a" - port = var.{{ $name }}_targets[count.index].port + port = {{ $t.Port }} } {{- end }} +{{- end }}