Data send between modules

I have two modules which Main.tf calls these two modules one after another.
first module is for creating IAM roles and second one to create aws glue jobs. I need to pass ARN number of a specific role created in first module to second module to create a glue job.

this is main.tf

# DataLake IAM Roles/Policies
module "iam_Policies"{
source = "./modules/iam/policies"
}

# DataLake Glue Jobs 
module "glue_jobs"{
source = "./modules/glue/jobs"
}

This is main.tf for iam_policies module

resource "aws_iam_role" "AWSServiceRoleForRedshift" {

 name = "AWSServiceRoleForRedshift"
  path = "/"
  assume_role_policy = jsonencode({

    Version = "2012-10-17"

    Statement = [

      {

        Action = "sts:AssumeRole"
        Effect = "Allow"
        Sid    = ""
     Principal = {

          Service = "redshift.amazonaws.com"
        }
      },
    ]
  })
}

I need to pass ARN number of role named “AWSServiceRoleForRedshift” to module glue_jobs.

I tried:

  ARN_number  =  module.iam_Policies.aws_iam_role.AWSServiceRoleForRedshift.arn

but it doesn’t work.

any help apprecited if you know how to do this.

Thanks.

Hi @hboostani,

In your policies module, declare an output value to export the role ARN:

output "redshift_service_role_arn" {
  value = aws_iam_role.AWSServiceRoleForRedshift.arn
}

Then in your root module you can refer to that output when you want to pass that ARN into the other module:

  ARN_number = module.iam_Policies.redshift_service_role_arn

Please note that the typical Terraform language style is for resource names, module names, and output names to be written all in lowercase with underscores separating words. Although you can choose to use other schemes if you wish, I used an output named redshift_service_role_arn above in order to be consistent with the usual idiom.

Thank you very much! It works. :+1:

I’d also suggest using the IAM policy document data source for your assume role policy. See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document