Jatin Mehrotra
Jatin Mehrotra

Reputation: 11604

How to solve cycle error ( circular dependency error ) of terraform during assume role in providers?

I am trying to use terraform programming for infrastructure software development. The data resource to retrieve account id in order to avoid hardcoding account id/mask account id before checking into GitHub

Basically I am trying to assume role in target account using account A.

provider "aws" {
  region = "us-east-1"
  assume_role {
    role_arn     = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/abc"
    session_name = "jatin-test"
  }
}


resource "aws_s3_bucket" "s3_bucket" {
  bucket = var.bucket_name
  # bucket = "ck-jatin-test-123456"
}

data.tf

data "aws_caller_identity" "current" {}

Error message

│ Error: Cycle: data.aws_caller_identity.current, provider["registry.terraform.io/hashicorp/aws"]

Upvotes: -1

Views: 422

Answers (1)

0xn0b174
0xn0b174

Reputation: 1022

data.tf

provider "aws" {
  alias  = "no_assume_role"
  region = "us-east-1"
}

data "aws_caller_identity" "current" {
  provider = aws.no_assume_role
}

aws.no_assume_role is configured to retrive the account ID using data.aws_caller_identity.current

separating the data source from the provider that assumes the role will avoid creating a cycle, allowing terraform to correctly resolve dependencies

Upvotes: 0

Related Questions