Reputation: 2491
I wanted to pass some variables while rendering the json file of OpenAPI spec through the template file but it shows the below changes.
Code: resource.tf
resource "aws_api_gateway_rest_api" "default" {
body = jsonencode(var.routes)
name = "dev-api-gateway-fargate"
endpoint_configuration {
types = ["REGIONAL"]
}
}
main.tf
module "api_gateway_new" {
source = "../../../modules/api-gateway/new/"
routes = templatefile("${path.module}/routes.tpl",{
url = "https://ip-ranges.amazonaws.com/ip-ranges.json"
}
)
}
Terraform Plan
~ resource "aws_api_gateway_rest_api" "default" {
~ body = jsonencode(
~ {
- info = {
- title = "example"
- version = "1.0"
}
- openapi = "3.0.1"
- paths = {
- "/path1" = {
- get = {
- x-amazon-apigateway-integration = {
- httpMethod = "GET"
- payloadFormatVersion = "1.0"
- type = "HTTP_PROXY"
- uri = "https://ip-ranges.amazonaws.com/ip-ranges.json"
}
}
}
}
} -> <<-EOT
{
openapi = "3.0.1"
info = {
title = var.rest_api_name
version = "1.0"
}
paths = {
"/v1/alerts" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "HTTP_PROXY"
uri = "https://ip-ranges.amazonaws.com/ip-ranges.json"
}
}
}
}
}
EOT
)
id = "mxktrgeypg"
name = "dev-api-gateway-fargate"
tags = {}
# (10 unchanged attributes hidden)
# (1 unchanged block hidden)
}
routes.tpl
{
openapi = "3.0.1"
info = {
title = var.rest_api_name
version = "1.0"
}
paths = {
"/v1/alerts" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "HTTP_PROXY"
uri = "${url}"
}
}
}
}
}
Upvotes: 0
Views: 932
Reputation: 18103
There is an error in the template file as it seems you were trying to add a name for the API but you provided var.rest_api_name
which is treated as a literal. In order to fix this, there are two changes that need to take place, one in the module call and one in the template file itself.
module "api_gateway_new" {
source = "../../../modules/api-gateway/new/"
routes = templatefile("${path.module}/routes.tpl",{
url = "https://ip-ranges.amazonaws.com/ip-ranges.json",
name = var.rest_api_name
}
)
}
Then, in the template file:
{
openapi = "3.0.1"
info = {
title = "${name}" # this has changed
version = "1.0"
}
paths = {
"/v1/alerts" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "HTTP_PROXY"
uri = "${url}"
}
}
}
}
}
Since the template file is almost JSON formatted already, I would suggest changing the file to look like this:
{
"openapi": "3.0.1",
"info": {
"title": "${name}",
"version": "1.0"
},
"paths": {
"/v1/alerts": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "GET",
"payloadFormatVersion": "1.0",
"type": "HTTP_PROXY",
"uri": "${url}"
}
}
}
}
}
And then you can drop the jsonencode
form the module code:
resource "aws_api_gateway_rest_api" "default" {
body = var.routes
name = "dev-api-gateway-fargate"
endpoint_configuration {
types = ["REGIONAL"]
}
}
Also, this should work for creating an OpenAPI spec, but the validation of this code says it is missing a responses
section. So for the sake of testing if it works, you could add the responses
:
{
"openapi": "3.0.1",
"info": {
"title": "${name}",
"version": "1.0"
},
"paths": {
"/v1/alerts": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref" : "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"httpMethod": "GET",
"payloadFormatVersion": "1.0",
"type": "HTTP_PROXY",
"uri": "${url}"
}
}
}
},
"components": {
"schemas": {
"Empty": {
"title": "Empty Schema",
"type": "object"
}
}
}
}
I have used [1] to validate the OpenAPI spec, so if it is still complaining, the tool should give you a pretty good idea of what is missing/wrong.
[1] https://apitools.dev/swagger-parser/online/
Upvotes: 1