Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure AWS MFA for Terraform?

I want to perform MFA for Terraform so it's expected to ask the 6-digit token from my virtual MFA device for every terraform [command]. After reading the documentation: cli-roles terraform mfa I created a role:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::[ACCOUNT_ID]:user/testuser"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "Bool": {
          "aws:MultiFactorAuthPresent": "true"
        }
      }
    }
  ]
}

This user is forced to use MFA by default and I have a configured virtual MFA device for him.

~/.aws/credentials:

[default]
...

[terraform_role]
role_arn = arn:aws:iam::[ACCOUNT_ID]:role/terraform-test-role
source_profile = default
mfa_serial = arn:aws:iam::[ACCOUNT_ID]:mfa/testuser

in my Terraform environment I placed the following:

provider "aws" {
  profile = "terraform_role"
}

But when i run terraform plan it throws me an error:

Error refreshing state: 1 error(s) occurred:

* provider.aws: No valid credential sources found for AWS Provider.
  Please see https://terraform.io/docs/providers/aws/index.html for more information on
  providing credentials for the AWS Provider
like image 572
Alexander Nekrasov Avatar asked Aug 15 '17 09:08

Alexander Nekrasov


People also ask

Is it possible to configure AWS with Terraform?

You can provide Terraform with an AWS access key directly through the provider, but we recommend that you use a credential profile already configured by one of the AWS Software Developer Kits (SDKs).

How do I manually set up an MFA?

Go to the Microsoft 365 admin center at https://admin.microsoft.com. Select Show All, then choose the Azure Active Directory Admin Center. Select Azure Active Directory, Properties, Manage Security defaults. Under Enable Security defaults, select Yes and then Save.


1 Answers

The solution is to specify an assume_role statement:

provider "aws" {
  profile = "default"
  assume_role {
    role_arn = "arn:aws:iam::[ACCOUNT_ID]:role/terraform-test-role"
  }
}
like image 105
Alexander Nekrasov Avatar answered Nov 01 '22 08:11

Alexander Nekrasov