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

Terratest Unable to Use Terraform Provider with Assume Role #1500

Open
AnkitGithubSpace opened this issue Jan 8, 2025 · 5 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@AnkitGithubSpace
Copy link

AnkitGithubSpace commented Jan 8, 2025

Describe the bug
I am facing an issue when attempting to use a Terraform provider with an assume_role configuration in Terratest. The same Terraform provider configuration works when running directly from the CLI (e.g., terraform init and terraform plan). However, when executing the same configuration via Terratest, the provider is unable to assume the role and throws an error.

To Reproduce

  1. I am facing an issue when attempting to use a Terraform provider with an assume_role configuration in Terratest. The same Terraform provider configuration works when running directly from the CLI (e.g., terraform init and terraform plan). However, when executing the same configuration via Terratest, the provider is unable to assume the role and throws an error.
  2. Test the Terraform configuration manually:
  • Run terraform init and terraform plan successfully using the AWS provider.
  1. Create a Terratest script to test the same Terraform configuration:
package test

import (
    "os"
    "testing"
    "github.com/gruntwork-io/terratest/modules/terraform"
)

func TestTerraformModule(t *testing.T) {
    t.Parallel()
    terraformOptions := &terraform.Options{
        TerraformDir: "../",
        Vars: map[string]interface{}{
            "some_variable": "some_value",
        },
        NoColor: true,
    }

    terraform.InitAndPlan(t, terraformOptions)
}

Expected behavior
The Terratest script should successfully execute terraform init and terraform plan while using the AWS provider configured with the assume_role block.

Nice to have
image

Versions

  • Terratest version:
  • Environment details (Ubuntu 20.04, Windows 10, etc.):

Additional context
Add any other context about the problem here.

@AnkitGithubSpace AnkitGithubSpace added the bug Something isn't working label Jan 8, 2025
@wakeful
Copy link
Contributor

wakeful commented Jan 8, 2025

Hey, can you share an example of a provider "aws" block? I'm interested in how you are configuring permissions. I tried to replicate your issue, but maybe you have a more complex setup.

Do you have any specific permissions assigned to the role you are using for running terratest?

@AnkitGithubSpace
Copy link
Author

provider "aws" {
region = "ap-southeast-1"
assume_role {
role_arn = "arn:aws:iam::123456790:role/test-STG"
session_name = "RunnerSession"
}
default_tags {
tags = {
"Portfolio" = "abc"
"Application" = "terratest"
}
}
}

@wakeful
Copy link
Contributor

wakeful commented Jan 9, 2025

It looks like we have a matching setup. Can you confirm what permissions are bound to the AWS credentials you are using when running terratest? (before assume)

  • Is it just an assume role?
  • Does the trust policy on the IAM role arn:aws:iam::123456790:role/test-STG allow role assumption from the terratest role?

@AnkitGithubSpace
Copy link
Author

AnkitGithubSpace commented Jan 9, 2025

I have running terratest in runner and runner have permission to assume the role (arn:aws:iam::123456790:role/test-STG) and also I have added the trust relationship on role (arn:aws:iam::123456790:role/test-STG)

@wakeful
Copy link
Contributor

wakeful commented Jan 9, 2025

Can you check if my example test works for you? You just need to update the IAM role that terraform should assume here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants