Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert REST API described in OpenAPI specification to AWS API Gateway REST API described in Terraform template

I have OpenAPI 3.0 specification (YAML) of my REST API and I need to describe the same REST API in Terraform template (HCL) as AWS API Gateway resources.

Are there any tools which could help me to convert it automatically?

like image 445
Gleb Avatar asked Oct 16 '25 15:10

Gleb


1 Answers

AWS' API Gateway directly supports importing an OpenAPI 3.0 spec to define all of the relevant parts of an API Gateway.

With Terraform this can be done with the aws_api_gateway_rest_api resource and the body parameter.

An example is given in the tests for the resource:

resource "aws_api_gateway_rest_api" "test" {
  name = "foo"
  body = <<EOF
{
  "swagger": "2.0",
  "info": {
    "title": "foo",
    "version": "2017-04-20T04:08:08Z"
  },
  "schemes": [
    "https"
  ],
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "x-amazon-apigateway-integration": {
          "type": "HTTP",
          "uri": "https://www.google.de",
          "httpMethod": "GET",
          "responses": {
            "default": {
              "statusCode": 200
            }
          }
        }
      }
    }
  }
}
EOF
}

You can also use the file function to load the OpenAPI spec from your YAML file directly:

resource "aws_api_gateway_rest_api" "test" {
  name = "foo"
  body = file("${path.module}/api.yml")
}
like image 136
ydaetskcoR Avatar answered Oct 19 '25 06:10

ydaetskcoR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!