Skip to content

Commit

Permalink
Merge pull request #125 from helmfile/remove-unused-interface
Browse files Browse the repository at this point in the history
Experimental support for specifying aws_profile on tfstate backend
  • Loading branch information
yxxhero authored Feb 4, 2023
2 parents e7f263f + 0afc6ea commit 87322cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,12 @@ Examples:
### Terraform (tfstate)
- `ref+tfstate://relative/path/to/some.tfstate/RESOURCE_NAME`
- `ref+tfstate:///absolute/path/to/some.tfstate/RESOURCE_NAME`
- `ref+tfstate://relative/path/to/some.tfstate/RESOURCE_NAME[?aws_profile=AWS_POFILE]`
- `ref+tfstate:///absolute/path/to/some.tfstate/RESOURCE_NAME[?aws_profile=AWS_POFILE]`
Options:
`aws_profile`: If non-empty, `vals` tries to let tfstate-lookup to use the specified AWS profile defined in the well-known `~/.credentials` file.
Examples:
Expand Down
23 changes: 22 additions & 1 deletion pkg/providers/tfstate/tfstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import (
"fmt"
"os"
"strings"
"sync"

"github.com/helmfile/vals/pkg/api"

"github.com/fujiwara/tfstate-lookup/tfstate"
)

type provider struct {
backend string
backend string
awsProfile string
}

func New(cfg api.StaticConfig, backend string) *provider {
p := &provider{}
p.backend = backend
p.awsProfile = cfg.String("aws_profile")
return p
}

Expand Down Expand Up @@ -44,8 +47,26 @@ func (p *provider) GetString(key string) (string, error) {
return attrs.String(), nil
}

var (
// tfstate-lookup does not support explicitly setting some settings like
// the AWS profile to be used.
// We use temporary envvar override around calling tfstate's Read function,
// so that hopefully the aws-go-sdk v2 session can be initialized using those temporary
// envvars, respecting things like the AWS profile to use.
tfstateMu sync.Mutex
)

// Read state either from file or from backend
func (p *provider) ReadTFState(f, k string) (*tfstate.TFState, error) {
tfstateMu.Lock()
defer tfstateMu.Unlock()

if p.awsProfile != "" {
v := os.Getenv("AWS_PROFILE")
os.Setenv("AWS_PROFILE", p.awsProfile)
defer os.Setenv("AWS_PROFILE", v)
}

switch p.backend {
case "":
state, err := tfstate.ReadFile(f)
Expand Down

0 comments on commit 87322cd

Please sign in to comment.