Skip create api gateway if it already exists

Hi,

I’m using a multi branch pipeline, where i’m creating a API gateway and lambda using terraform. The terraform script creates mutliple apis for each stage (dev,uat,prod). Can you suggest how to stop creating resources if they are already available in the aws cloud.

Also, if i try this way

data “aws_api_gateway_rest_api” “my_rest_api” {
name = “API_name”
}

locals {
api_present = anytrue([
data.aws_api_gateway_rest_api.my_rest_api.id == “glwvtrnxta”
])
}

output “api_exists” {
value = local.api_present
}

resource “aws_api_gateway_rest_api” “api” {
count = local.api_present ? 0 : 1
name = “API_name”
}

it is throwing this error

│ Error: no REST APIs with name “API_name” found in this region

│ with data.aws_api_gateway_rest_api.my_rest_api,
│ on main.tf line 6, in data “aws_api_gateway_rest_api” “my_rest_api”:
│ 6: data “aws_api_gateway_rest_api” “my_rest_api” {