title: How to create a terraform resource only if doesn't exists
body: <p>I am creating a role in AWS using terraform. Since IAM is non-region service, i just want to create the role once. So whenever I run the terraform it should check if the role already exists or not, if not it should create one.</p>
  
  <pre><code>data "aws_iam_role" "iam_role_check" {
    name = "some_role"
  }
  
  resource "aws_iam_role" "iam_role" {
    count= "${data.aws_iam_role.iam_role_check != "null" ? 0 : 1}"
    name = "some_role"
  
    assume_role_policy = &lt;&lt;EOF
  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "sts:AssumeRole",
        "Principal": {
          "Service": "lambda.amazonaws.com"
        },
        "Effect": "Allow",
        "Sid": ""
      }
    ]
  }
  EOF
  }
  </code></pre>
  
  <p>data.aws_iam_role.iam_role_check: Refreshing state...</p>
  
  <blockquote>
    <p>Error: error reading IAM Role (some_role): NoSuchEntity: The role with
    name some_role cannot be found.   status code: 404, request id:</p>
  </blockquote>