Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform S3/Backend ignoring profile parameter #22103

Closed
marcotesch opened this issue Jul 17, 2019 · 11 comments · Fixed by #25134
Closed

Terraform S3/Backend ignoring profile parameter #22103

marcotesch opened this issue Jul 17, 2019 · 11 comments · Fixed by #25134
Assignees
Labels
backend/s3 bug v0.12 Issues (primarily bugs) reported against v0.12 releases
Milestone

Comments

@marcotesch
Copy link

marcotesch commented Jul 17, 2019

Hi there

We are facing issues with Terraform in a Cross-Account Setup. Our Terraform version is shown below:

terraform version
Terraform v0.12.4

Terraform does not use the provided profile = xxx parameter in the backend configuration and therefore we get an access denied error when executing terraform plan | apply.

terraform {
  required_version = ">= 0.12"

  required_providers {
    aws = ">= 2.18.0"
  }

  backend "s3" {
    encrypt        = true
    bucket         = "xxxxxxx"
    key            = "terraform.tfstate"
    region         = "eu-central-1"
    dynamodb_table = "xxxxxxx"
    profile        = "xxxxxxx"
  }
}

Debug Output

It just uses the credentials we provide using the AWS Environment variables, but these are not valid for the AWS S3 Bucket and Backend where we configured different credentials which can be seen in the above mentioned backend configuration.

2019/07/17 10:59:31 [INFO] AWS Auth provider used: "EnvProvider"

Crash Output

Leading to this error:

Error: Error loading state: AccessDenied: Access Denied
        status code: 403

Expected Behavior

Terraform should use the configured backend profile for each backend related API call and the Environment/Provider credentials for each Resource/Data related API call.

Actual Behavior

Terraform uses only the Environment Credentials for each API Call, despite it is related to the Backend or resource creation.

Steps to Reproduce

1. awsume aws-profile-xxx
2. terraform init | plan | apply 

Terraform will always use the assumed environment credentials despite a profile is configured for the backend.

@hashibot hashibot added the v0.12 Issues (primarily bugs) reported against v0.12 releases label Aug 28, 2019
@mateuszmrozewski
Copy link

This is related to the aws-sdk-go-base wrapper that does not pass the profile while creating session. See: hashicorp/aws-sdk-go-base#19

@nomike
Copy link

nomike commented Jan 21, 2020

This is affecting me as well.

@ryanpflynn
Copy link

I am also trying to use a profile for the backend configuration and terraform is not honoring it.

@ryanpflynn
Copy link

I was able to work around this by making sure AWS_SECRET_KEY and AWS_SECRET_ACCESS_KEY environment variables were not set.

@Jishun
Copy link

Jishun commented Feb 13, 2020

this affects me by always picking the default profile thus unable to access the backend,
I don't have AWS_SECRET_KEY and AWS_SECRET_ACCESS_KEY environment variables set either

@Jishun
Copy link

Jishun commented Feb 13, 2020

according to this old thread :
#18402
I worked this around by using
AWS_SDK_LOAD_CONFIG=1 AWS_PROFILE=profile_name terraform init

@marcotesch
Copy link
Author

according to this old thread :
#18402
I worked this around by using
AWS_SDK_LOAD_CONFIG=1 AWS_PROFILE=profile_name terraform init

But this only works if the backend is within the same account the resources are as well.

@matt-brewster
Copy link

according to this old thread :
#18402
I worked this around by using
AWS_SDK_LOAD_CONFIG=1 AWS_PROFILE=profile_name terraform init

I couldn't get this working at all with my config. My backend looks like this:

terraform {
  backend "s3" {
    profile = "profile_with_role_arn"
    region = "<region>"
    bucket = "<bucket name>"
    dynamodb_table = "<dynamodb table>"
    encrypt = "true"
    kms_key_id = "<key arn>"
    key = "<key>"
  }
}

My AWS config file:

[profile profile_with_role_arn]
role_arn = <role arn>
source_profile = profile_with_role_arn
output = json
region = <region>

My AWS credentials file:

[profile_with_role_arn]
aws_access_key_id = XXXXXXXX
aws_secret_access_key = YYYYYYYYY
aws_session_token = ZZZZZZZZZZ

Running terraform init always gives me:

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Error refreshing state: AccessDenied: Access Denied
	status code: 403, request id: ABC123, host id: ...

I have tried with various combinations of export AWS_SDK_LOAD_CONFIG=1, export AWS_PROFILE=profile_with_role_arn but always the same error. Adding profile=profile_with_role_arn into the backend config fixes the issue, but this is not a workable solution for me right now.

@neilferreira
Copy link

according to this old thread :
#18402
I worked this around by using
AWS_SDK_LOAD_CONFIG=1 AWS_PROFILE=profile_name terraform init

But this only works if the backend is within the same account the resources are as well.

Anyone found a proper solution to this problem yet?

I'm storing my backend on a client's "master" account which I have access to via a role_arn. When trying to provision their UAT environment I'm now unable to do so.

This is the setup:

[primary]
aws_access_key_id=
aws_secret_access_key=

[client-primary]
source_profile = primary
role_arn = arn:aws:iam::blah:role/OrganizationAccountAccessRole

[client-uat]
source_profile = primary
role_arn = arn:aws:iam::blah:role/OrganizationAccountAccessRole

When trying to provision resources on client-uat, I need the backend to be on client-primary. As it stands, if we're forced to use environment variables, this will not be possible.

@bflad bflad self-assigned this Jun 2, 2020
@bflad bflad added this to the v0.13.0 milestone Jun 2, 2020
bflad added a commit that referenced this issue Jun 4, 2020
Reference: #13410
Reference: #18774
Reference: #19482
Reference: #20062
Reference: #20599
Reference: #22103
Reference: #22161
Reference: #22601
Reference: #22992
Reference: #24252
Reference: #24253
Reference: #24480
Reference: #25056

Changes:

```
NOTES

* backend/s3: Deprecated `lock_table`, `skip_get_ec2_platforms`, `skip_requesting_account_id` arguments have been removed
* backend/s3: Credential ordering has changed from static, environment, shared credentials, EC2 metadata, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata) to static, environment, shared credentials, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata)
* The `AWS_METADATA_TIMEOUT` environment variable no longer has any effect as we now depend on the default AWS Go SDK EC2 Metadata client timeout of one second with two retries

ENHANCEMENTS

* backend/s3: Always enable shared configuration file support (no longer require `AWS_SDK_LOAD_CONFIG` environment variable)
* backend/s3: Automatically expand `~` prefix for home directories in `shared_credentials_file` argument
* backend/s3: Add `assume_role_duration_seconds`, `assume_role_policy_arns`, `assume_role_tags`, and `assume_role_transitive_tag_keys` arguments

BUG FIXES

* backend/s3: Ensure configured profile is used
* backend/s3: Ensure configured STS endpoint is used during AssumeRole API calls
* backend/s3: Prefer AWS shared configuration over EC2 metadata credentials
* backend/s3: Prefer ECS credentials over EC2 metadata credentials
* backend/s3: Remove hardcoded AWS Provider messaging
```

Output from acceptance testing:

```
--- PASS: TestBackend (16.32s)
--- PASS: TestBackendConfig (0.58s)
--- PASS: TestBackendConfig_AssumeRole (0.02s)
--- PASS: TestBackendConfig_conflictingEncryptionSchema (0.00s)
--- PASS: TestBackendConfig_invalidKey (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyEncoding (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyLength (0.00s)
--- PASS: TestBackendExtraPaths (13.21s)
--- PASS: TestBackendLocked (28.98s)
--- PASS: TestBackendPrefixInWorkspace (5.65s)
--- PASS: TestBackendSSECustomerKey (17.60s)
--- PASS: TestBackend_impl (0.00s)
--- PASS: TestForceUnlock (17.50s)
--- PASS: TestKeyEnv (50.25s)
--- PASS: TestRemoteClient (4.78s)
--- PASS: TestRemoteClientLocks (16.85s)
--- PASS: TestRemoteClient_clientMD5 (12.08s)
--- PASS: TestRemoteClient_impl (0.00s)
--- PASS: TestRemoteClient_stateChecksum (17.92s)
```
bflad added a commit that referenced this issue Jun 5, 2020
* deps: Update github.com/hashicorp/aws-sdk-go-base@v0.5.0

Updated via:

```
$ go get github.com/hashicorp/aws-sdk-go-base@v0.5.0
$ go mod tidy
$ go mod vendor
```

* backend/s3: Updates for Terraform v0.13.0

Reference: #13410
Reference: #18774
Reference: #19482
Reference: #20062
Reference: #20599
Reference: #22103
Reference: #22161
Reference: #22601
Reference: #22992
Reference: #24252
Reference: #24253
Reference: #24480
Reference: #25056

Changes:

```
NOTES

* backend/s3: Deprecated `lock_table`, `skip_get_ec2_platforms`, `skip_requesting_account_id` arguments have been removed
* backend/s3: Credential ordering has changed from static, environment, shared credentials, EC2 metadata, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata) to static, environment, shared credentials, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata)
* The `AWS_METADATA_TIMEOUT` environment variable no longer has any effect as we now depend on the default AWS Go SDK EC2 Metadata client timeout of one second with two retries

ENHANCEMENTS

* backend/s3: Always enable shared configuration file support (no longer require `AWS_SDK_LOAD_CONFIG` environment variable)
* backend/s3: Automatically expand `~` prefix for home directories in `shared_credentials_file` argument
* backend/s3: Add `assume_role_duration_seconds`, `assume_role_policy_arns`, `assume_role_tags`, and `assume_role_transitive_tag_keys` arguments

BUG FIXES

* backend/s3: Ensure configured profile is used
* backend/s3: Ensure configured STS endpoint is used during AssumeRole API calls
* backend/s3: Prefer AWS shared configuration over EC2 metadata credentials
* backend/s3: Prefer ECS credentials over EC2 metadata credentials
* backend/s3: Remove hardcoded AWS Provider messaging
```

Output from acceptance testing:

```
--- PASS: TestBackend (16.32s)
--- PASS: TestBackendConfig (0.58s)
--- PASS: TestBackendConfig_AssumeRole (0.02s)
--- PASS: TestBackendConfig_conflictingEncryptionSchema (0.00s)
--- PASS: TestBackendConfig_invalidKey (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyEncoding (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyLength (0.00s)
--- PASS: TestBackendExtraPaths (13.21s)
--- PASS: TestBackendLocked (28.98s)
--- PASS: TestBackendPrefixInWorkspace (5.65s)
--- PASS: TestBackendSSECustomerKey (17.60s)
--- PASS: TestBackend_impl (0.00s)
--- PASS: TestForceUnlock (17.50s)
--- PASS: TestKeyEnv (50.25s)
--- PASS: TestRemoteClient (4.78s)
--- PASS: TestRemoteClientLocks (16.85s)
--- PASS: TestRemoteClient_clientMD5 (12.08s)
--- PASS: TestRemoteClient_impl (0.00s)
--- PASS: TestRemoteClient_stateChecksum (17.92s)
```
@bflad
Copy link
Member

bflad commented Jun 5, 2020

Multiple fixes for credential ordering, automatically using the AWS shared configuration file if present, and profile configuration handling of the S3 Backend have been merged and will release with version 0.13.0-beta2 of Terraform.

mildwonkey pushed a commit that referenced this issue Jun 12, 2020
* deps: Update github.com/hashicorp/aws-sdk-go-base@v0.5.0

Updated via:

```
$ go get github.com/hashicorp/aws-sdk-go-base@v0.5.0
$ go mod tidy
$ go mod vendor
```

* backend/s3: Updates for Terraform v0.13.0

Reference: #13410
Reference: #18774
Reference: #19482
Reference: #20062
Reference: #20599
Reference: #22103
Reference: #22161
Reference: #22601
Reference: #22992
Reference: #24252
Reference: #24253
Reference: #24480
Reference: #25056

Changes:

```
NOTES

* backend/s3: Deprecated `lock_table`, `skip_get_ec2_platforms`, `skip_requesting_account_id` arguments have been removed
* backend/s3: Credential ordering has changed from static, environment, shared credentials, EC2 metadata, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata) to static, environment, shared credentials, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata)
* The `AWS_METADATA_TIMEOUT` environment variable no longer has any effect as we now depend on the default AWS Go SDK EC2 Metadata client timeout of one second with two retries

ENHANCEMENTS

* backend/s3: Always enable shared configuration file support (no longer require `AWS_SDK_LOAD_CONFIG` environment variable)
* backend/s3: Automatically expand `~` prefix for home directories in `shared_credentials_file` argument
* backend/s3: Add `assume_role_duration_seconds`, `assume_role_policy_arns`, `assume_role_tags`, and `assume_role_transitive_tag_keys` arguments

BUG FIXES

* backend/s3: Ensure configured profile is used
* backend/s3: Ensure configured STS endpoint is used during AssumeRole API calls
* backend/s3: Prefer AWS shared configuration over EC2 metadata credentials
* backend/s3: Prefer ECS credentials over EC2 metadata credentials
* backend/s3: Remove hardcoded AWS Provider messaging
```

Output from acceptance testing:

```
--- PASS: TestBackend (16.32s)
--- PASS: TestBackendConfig (0.58s)
--- PASS: TestBackendConfig_AssumeRole (0.02s)
--- PASS: TestBackendConfig_conflictingEncryptionSchema (0.00s)
--- PASS: TestBackendConfig_invalidKey (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyEncoding (0.00s)
--- PASS: TestBackendConfig_invalidSSECustomerKeyLength (0.00s)
--- PASS: TestBackendExtraPaths (13.21s)
--- PASS: TestBackendLocked (28.98s)
--- PASS: TestBackendPrefixInWorkspace (5.65s)
--- PASS: TestBackendSSECustomerKey (17.60s)
--- PASS: TestBackend_impl (0.00s)
--- PASS: TestForceUnlock (17.50s)
--- PASS: TestKeyEnv (50.25s)
--- PASS: TestRemoteClient (4.78s)
--- PASS: TestRemoteClientLocks (16.85s)
--- PASS: TestRemoteClient_clientMD5 (12.08s)
--- PASS: TestRemoteClient_impl (0.00s)
--- PASS: TestRemoteClient_stateChecksum (17.92s)
```
@ghost
Copy link

ghost commented Jul 6, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backend/s3 bug v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants