Creating a List of API Gateway resources using Terraform

For some reason when you utilize Terraform with AWS, specifically when you want to get a list of API Gateway resources, that data element simply does not exist. Below is a relatively quick solution that will create a comma separated list of API Gateways so that you can iterate through them.

In order to execute this element you need to have the AWS CLI setup within your preferred deployment method. Personally, I love GitHub Actions so I needed to add another stage in my deployment to install the CLI.

The way this work is to create a data element that you can trigger as needed to execute a simple shell script.

data "external" "apis" {
   program = ["sh", "-c", "aws apigateway get-rest-apis    --query 'items[?starts_with(name,`${var.prefix}`)].name' --output json | jq -r '{\"names\": (. | join(\",\"))}'"]
}

We also are creating a variable called “prefix” so that you can filter as required by your project. Personally, I used this to create Cloudwatch Dashboards so I can easily monitor my resources.

If this is helpful for you, please share it on your social media!

Comments

Leave a Reply