hashicorp / hashicorp/terraform

terraform backend s3 not working with mfa profile and assume role

Open
#17,530 8 comments 6 reactions 0 assignees View on GitHub
backend/s3
Dominant language
Go
Stars
49.7k
Forks
10.6k
Avg merge
21h 30m
Merged PRs (30d)
100

Description

Hi,
We are trying to use backend s3 to store terraform state file. We have 3 different accounts(each used as prod, dev and staging) .
We have enabled MFA for IAM users. IAM user(s) can login using MFA in one of these accounts and then from their they use switch role to access resources in any of these accounts.
For eg:, we have a s3 bucket (for eg: s3-tfstate-bucket) in dev account, we are trying to store the tf state into the same bucket under different key(path) for each account. we can only use access key and secret key for dev as we do not have IAM users for other 2 accounts, we use switch role to access resources for other accounts.
We use assume role features for the resources that we would like to launch in these accounts. Also for s3, we thought to use assume role but it is not working, hence we thought to use access key and secret access key. We get below error when trying to initialize the backend. Could you please advise how should we fix this?

Initializing the backend...

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

**Here is the terraform code that we are using:**

terraform {
backend "s3" {
bucket = "s3-tfstate-bucket"
key = "dev/bastion/terraform.tfstate"
dynamodb_table = "dynamodbtable-east-lock"
region = "us-east-1"
encrypt = "true"
access_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}

provider "aws" {
region = "us-east-1"
shared_credentials_file = "~/.aws/credentials"
profile = "dev-mfa"
assume_role {
role_arn = "arn:aws:iam::xxxxxxxxxxxxxxxxx:role/abcd"
}
}

module "bastion" {
source = "../../.../../../modules/core/services/bastion"
vpc_id = "${var.vpc_id}"
asg_subnets = ["${var.asg_subnets}"]
}

Regards,
Ravi

### Terraform Version

```
...
```

### Terraform Configuration Files

```hcl
...
```

### Debug Output

### Crash Output

### Expected Behavior

### Actual Behavior

### Steps to Reproduce

### Additional Context

### References

Contributor guide

Open the contributing guide

Research direction

Start with the S3 backend configuration and the AWS provider profile and assume_role settings shown in the issue. Reproduce the `terraform init` failure with the supplied configuration and compare backend authentication behavior with provider authentication; done means the state bucket and DynamoDB lock table can be accessed using the intended MFA and role setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, terraform
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.