title: How can I use a Terraform's modules output as an input to another Terraform module?
body: <p>I am writing Terraform to deploy an S3 bucket and a lambda. The S3 bucket is needed as this is where the zip object will be stored ready to deploy to lambda.</p>
  <p>I have two modules in my top-level main.tf:</p>
  <pre><code>module &quot;s3&quot; {
    count  = var.enable_s3 ? 1 : 0
    source = &quot;./modules/s3&quot;
  }
  
  module &quot;lambdas&quot; {
    count     = var.enable_lambdas ? 1 : 0
    source    = &quot;./modules/lambdas&quot;
    bucket_id = module.s3.lambda_bucket_id
  }
  </code></pre>
  <p>s3 module:</p>
  <p>main.tf</p>
  <pre><code>resource &quot;aws_s3_bucket&quot; &quot;lambda_bucket&quot; {
    bucket = &quot;lunchboxd-lambdas&quot;
  
    tags = {
      Owner       = &quot;Terraform&quot;,
      Description = &quot;A bucket to hold the lambdas zip&quot;
    }
  }
  </code></pre>
  <p>outputs.tf</p>
  <pre><code>output &quot;lambda_bucket_id&quot; {
    value       = aws_s3_bucket.lambda_bucket.id
    description = &quot;ID of the bucket holding the lambda functions.&quot;
  }
  </code></pre>
  <p>lambdas module:</p>
  <p>variables.tf</p>
  <pre><code>variable &quot;bucket_id&quot; {}
  </code></pre>
  <p>main.tf</p>
  <pre><code>data &quot;archive_file&quot; &quot;lambda_hello_world&quot; {
    type = &quot;zip&quot;
  
    source_dir  = &quot;${path.module}/hello-world&quot;
    output_path = &quot;${path.module}/hello-world.zip&quot;
  }
  
  resource &quot;aws_s3_object&quot; &quot;lambda_hello_world&quot; {
    bucket = var.bucket_id
  
    key    = &quot;hello-world.zip&quot;
    source = data.archive_file.lambda_hello_world.output_path
  
    etag = filemd5(data.archive_file.lambda_hello_world.output_path)
  }
  
  
  resource &quot;aws_lambda_function&quot; &quot;hello_world&quot; {
    function_name = &quot;HelloWorld&quot;
  
    s3_bucket = var.bucket_id
    s3_key    = aws_s3_object.lambda_hello_world.key
  
    runtime = &quot;nodejs12.x&quot;
    handler = &quot;hello.handler&quot;
  
    source_code_hash = data.archive_file.lambda_hello_world.output_base64sha256
  
    role = aws_iam_role.lambda_exec.arn
  }
  
  resource &quot;aws_cloudwatch_log_group&quot; &quot;hello_world&quot; {
    name = &quot;/aws/lambda/${aws_lambda_function.hello_world.function_name}&quot;
  
    retention_in_days = 30
  }
  
  resource &quot;aws_iam_role&quot; &quot;lambda_exec&quot; {
    name = &quot;serverless_lambda&quot;
  
    assume_role_policy = jsonencode({
      Version = &quot;2012-10-17&quot;
      Statement = [{
        Action = &quot;sts:AssumeRole&quot;
        Effect = &quot;Allow&quot;
        Sid    = &quot;&quot;
        Principal = {
          Service = &quot;lambda.amazonaws.com&quot;
        }
        }
      ]
    })
  }
  
  resource &quot;aws_iam_role_policy_attachment&quot; &quot;lambda_policy&quot; {
    role       = aws_iam_role.lambda_exec.name
    policy_arn = &quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;
  }
  
  </code></pre>
  <p>When I try to validate my terraform I am getting the following error:</p>
  <pre><code>╷
  │ Error: Unsupported attribute
  │ 
  │   on main.tf line 36, in module &quot;lambdas&quot;:
  │   36:   bucket_id = module.s3.aws_s3_bucket.lambda_bucket.id
  │     ├────────────────
  │     │ module.s3 is a list of object, known only after apply
  │ 
  │ Can't access attributes on a list of objects. Did you mean to access an attribute for a specific element of the list, or across all elements of the list?
  ╵
  </code></pre>
  <p>I have followed the documentation on Outputs and module composition but I am unsure what I am doing wrong. All help appreciated.</p>