diff --git a/pkg/iac/terraform/state/backend/s3_reader.go b/pkg/iac/terraform/state/backend/s3_reader.go index e71276fb..e4c657e3 100644 --- a/pkg/iac/terraform/state/backend/s3_reader.go +++ b/pkg/iac/terraform/state/backend/s3_reader.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws/session" + "github.com/cloudskiff/driftctl/pkg/envproxy" "github.com/pkg/errors" "github.com/aws/aws-sdk-go/service/s3" @@ -33,9 +34,12 @@ func NewS3Reader(path string) (*S3Backend, error) { Key: &key, Bucket: &bucket, } + envProxy := envproxy.NewEnvProxy("DCTL_S3_", "AWS_") + envProxy.Apply() sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) + envProxy.Restore() backend.S3Client = s3.New(sess) return &backend, nil } diff --git a/pkg/iac/terraform/state/backend/s3_reader_test.go b/pkg/iac/terraform/state/backend/s3_reader_test.go index 064fdea2..503f209a 100644 --- a/pkg/iac/terraform/state/backend/s3_reader_test.go +++ b/pkg/iac/terraform/state/backend/s3_reader_test.go @@ -67,6 +67,31 @@ func TestNewS3Reader(t *testing.T) { ) } +func TestNewS3ReaderWithEnvProxy(t *testing.T) { + assert := assert.New(t) + os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + os.Setenv("DCTL_S3_DEFAULT_REGION", "eu-west-3") + reader, err := NewS3Reader("sample_bucket/path/to/state.tfstate") + + got := reader.S3Client.(*s3.S3).Config.Region + if aws.StringValue(got) != "eu-west-3" { + t.Errorf("NewS3Reader().S3Client.Config.Region got = %v, want %v", aws.StringValue(got), "eu-west-3") + } + + if err != nil { + t.Error(err) + } + + assert.Equal( + "path/to/state.tfstate", + *reader.input.Key, + ) + assert.Equal( + "sample_bucket", + *reader.input.Bucket, + ) +} + func TestS3Backend_ReadWithError(t *testing.T) { assert := assert.New(t) fakeS3 := &awstest.MockFakeS3{}