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.
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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data.aws_iam_role.iam_role_check: Refreshing state...
Error: error reading IAM Role (some_role): NoSuchEntity: The role with name some_role cannot be found. status code: 404, request id: