terraform-aws-api-gateway icon indicating copy to clipboard operation
terraform-aws-api-gateway copied to clipboard

Support for yaml api body

Open tibbing opened this issue 3 years ago • 2 comments

It would be nice if we could also support yaml format API body. Currently doing the jsonencode(..) here restricts us to only providing a OpenAPI map: https://github.com/cloudposse/terraform-aws-api-gateway/blob/d11aabaf9358626ea51a98a9a9854a9595da1339/main.tf#L13

But aws_api_gateway_rest_api actually accepts a yaml string just as well. I.e. the following works:

data "template_file" "swagger_api" {
  template = file("./swagger.yaml")
  vars = {
    ...
  }
}

resource "aws_api_gateway_rest_api" "this" {
  count = local.enabled ? 1 : 0

  name = module.this.id
  body = data.template_file.swagger_api.rendered
  tags = module.this.tags

  endpoint_configuration {
    types = [var.endpoint_type]
  }
}

The easiest solution would probably be to just remove the jsonencode() part and let the caller do that if needed. Optionally we could add another variable instead for backwards compatibility.

Another upside to getting rid of the jsonencode is supporting the case where the caller already has the body as json, for example when reading a json file.

tibbing avatar Aug 25 '22 14:08 tibbing

I'm able to do... openapi_config = yamldecode(file("${get_terragrunt_dir()}/vars/infra1-io.yaml")) I export from aws as a yaml formatted file.

Not your request, but could help in the meantime?

susie-idw avatar Aug 14 '24 22:08 susie-idw

I'm able to do... openapi_config = yamldecode(file("${get_terragrunt_dir()}/vars/infra1-io.yaml")) I export from aws as a yaml formatted file.

Not your request, but could help in the meantime?

If I do this:

resource "aws_api_gateway_rest_api" "api" {
  name                          = var.product
  description                   = var.description
  put_rest_api_mode             = "merge"

  endpoint_configuration {
    types = ["PRIVATE"]
    vpc_endpoint_ids = [aws_vpc_endpoint.stg.id]
  }

  body = yamldecode(file("./openapi_resources.yaml"))
}

I get this TF error:

Error: Incorrect attribute value type
on main.tf line 12, in resource "aws_api_gateway_rest_api" "api":
  body = yamldecode(file("./openapi_resources.yaml"))
Inappropriate value for attribute "body": string required.

@susie-idw is that different from what you do? Do you anything wrong with my code please? Thx

pajel avatar May 12 '25 16:05 pajel