Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions providers/aws/aws_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package aws

import (
"context"
"fmt"
"os"
"regexp"

"github.com/aws/aws-sdk-go-v2/service/sts"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)
Expand All @@ -34,12 +35,20 @@ type AWSService struct { //nolint

var awsVariable = regexp.MustCompile(`(\${[0-9A-Za-z:]+})`)

var configCache *aws.Config
var (
configCache = map[string]aws.Config{}
configCacheMu sync.RWMutex
)

func (s *AWSService) generateConfig() (aws.Config, error) {
if configCache != nil {
return *configCache, nil
cacheKey := s.configCacheKey()

configCacheMu.RLock()
if cachedConfig, ok := configCache[cacheKey]; ok {
configCacheMu.RUnlock()
return cachedConfig, nil
}
configCacheMu.RUnlock()

baseConfig, e := s.buildBaseConfig()

Expand All @@ -66,10 +75,29 @@ func (s *AWSService) generateConfig() (aws.Config, error) {
os.Setenv("AWS_SESSION_TOKEN", creds.SessionToken)
}
}
configCache = &baseConfig
configCacheMu.Lock()
configCache[cacheKey] = baseConfig
configCacheMu.Unlock()
return baseConfig, nil
}

func (s *AWSService) configCacheKey() string {
var region, profile string
var skipRegionValidation bool

if value, ok := s.GetArgs()["region"].(string); ok {
region = value
}
if value, ok := s.GetArgs()["profile"].(string); ok {
profile = value
}
if value, ok := s.GetArgs()["skip_region_validation"].(bool); ok {
skipRegionValidation = value
}

return fmt.Sprintf("region:%s|profile:%s|skip_region_validation:%t", region, profile, skipRegionValidation)
}

func (s *AWSService) buildBaseConfig() (aws.Config, error) {
var loadOptions []func(*config.LoadOptions) error
if s.GetArgs()["profile"].(string) != "" {
Expand Down