title: Terraform - Resource not found for variable despite having it declared in the same file
body: <p>Terraform can't find a resource which is declared in the same file where the reference is.</p>
  
  <p>It seems that this line is causing trouble: <code>role_arn = "${aws_iam_role.newsapi_lambda_codepipeline.arn}"</code>. It can't find <code>newsapi_lambda_codepipeline</code> which is declared as <code>resource "aws_iam_role" "newsapi_lambda_codepipeline" { ... }</code>.</p>
  
  <p>This is my main.tf:</p>
  
  <pre><code>resource "aws_s3_bucket" "newsapi_lambda_builds" {
    bucket = "newsapi-lambda-builds"
    acl    = "private"
  }
  
  resource "aws_iam_role" "newsapi_lambda_codebuild" {
    name = "newsapi-lambda-codebuild"
  
    assume_role_policy = &lt;&lt;EOF
  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": [
          "s3:GetObject",
          "s3:GetObjectVersion",
          "s3:GetBucketVersioning"
        ],
        "Resource": "arn:aws:s3:::newsapi_lambda_builds",
        "Effect": "Allow"
      },
      {
        "Action": [
          "s3:PutObject"
        ],
        "Resource": [
          "arn:aws:s3:::newsapi_lambda_builds"
        ],
        "Effect": "Allow"
      },
      {
        "Action": [
          "lambda:invokefunction",
          "lambda:listfunctions"
        ],
        "Resource": "*",
        "Effect": "Allow"
      },
      {
        "Effect": "Allow",
        "Resource": [
          "*"
        ],
        "Action": [
          "logs:CreateLogGroup",
          "logs:CreateLogStream",
          "logs:PutLogEvents"
        ]
      }
    ]
  }
  EOF
  }
  resource "aws_iam_role" "newsapi_lambda_codepipeline" {
    name = "newsapi-lambda-codepipeline"
  
    assume_role_policy = &lt;&lt;EOF
  {
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": {
          "Service": "codepipeline.amazonaws.com"
        },
        "Action": "sts:AssumeRole"
      },
      {
        "Action": [
          "s3:GetObject",
          "s3:GetObjectVersion",
          "s3:GetBucketVersioning"
        ],
        "Resource": "${aws_s3_bucket.newsapi_lambda_builds.arn}",
        "Resource": "${aws_s3_bucket.newsapi_lambda_builds.arn}/*"
        "Effect": "Allow"
      },
      {
        "Action": [
          "s3:PutObject"
        ],
        "Resource": [
          "arn:aws:s3:::newsapi_lambda_builds"
        ],
        "Effect": "Allow"
      },
      {
        "Effect": "Allow",
        "Action": [
          "codebuild:BatchGetBuilds",
          "codebuild:StartBuild"
        ],
        "Resource": "*"
      }
    ],
    "Version": "2012-10-17"
  }
  EOF
  }
  
  
  resource "aws_codepipeline" "newsapi_lambda" {
    name     = "newsapi-lambda"
    role_arn = "${aws_iam_role.newsapi_lambda_codepipeline.arn}"
  
    artifact_store {
      location = "${aws_s3_bucket.newsapi_lambda_builds.bucket}"
      type     = "S3"
    }
  
    stage {
      name = "Source"
  
      action {
        name             = "Source"
        category         = "Source"
        owner            = "ThirdParty"
        provider         = "GitHub"
        version          = "1"
        output_artifacts = ["newsapi_lambda"]
  
        configuration {
          Owner      = "Defozo"
          Repo       = "traceitfor.me_newsapi_lambda"
          Branch     = "master"
        }
      }
    }
  
    stage {
      name = "Build"
  
      action {
        name            = "Build"
        category        = "Build"
        owner           = "AWS"
        provider        = "CodeBuild"
        input_artifacts = ["newsapi_lambda"]
        version         = "1"
        role_arn = "${aws_iam_role.newsapi_lambda_codebuild.arn}"
  
        configuration {
          ProjectName = "newsapi-lambda"
        }
      }
    }
  }
  </code></pre>
  
  <p>After executing <code>terraform apply</code> I get:</p>
  
  <pre><code>Error: Error running plan: 1 error(s) occurred:
  
  * aws_codepipeline.newsapi_lambda: 1 error(s) occurred:
  
  * aws_codepipeline.newsapi_lambda: Resource 'aws_iam_role.newsapi_lambda_codepipeline' not found for variable 'aws_iam_role.newsapi_lambda_codepipeline.arn'
  </code></pre>
  
  <p>I don't understand why that happens. I have <code>aws_iam_role.newsapi_lambda_codepipeline</code> declared, haven't I?</p>