Skip to content

Commit

Permalink
Merge pull request #25 from takutakahashi/add_aws_backend
Browse files Browse the repository at this point in the history
avoid to recreate all resource when adding targets
  • Loading branch information
takutakahashi committed May 7, 2020
2 parents 55551f8 + 6fdfcba commit 75ac438
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkg/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions src/terraform/AWSBackend/lb.tf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit 75ac438

Please sign in to comment.