Setting up variables
API Gateway supports variables in the OpenAPI specification.
Example of a specification file with a variable
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
x-yc-apigateway:
variables:
environment:
default: "prod"
enum:
- "prod"
- "testing"
- "dev"
description: "API Gateway installation."
paths:
/env/info:
get:
x-yc-apigateway-integration:
type: dummy
content:
'*': It is ${var.environment} environment!
http_code: 200
http_headers:
Content-Type: text/plain
By default, variables take the values specified in the default field of the OpenAPI specification
- In the management console
, go to the folder where you want to update an API gateway. - In the list of services, select API Gateway.
- In the API gateway row, click
and select Edit. - Under Variable settings, list all the variables declared in the specification and their values.
- Click Save.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To set up your variables:
-
Edit the OpenAPI specification file or create a new one, if required.
-
View the description of the CLI command for updating an API gateway:
yc serverless api-gateway update --help -
Specify the API gateway ID or name in the command and list all the variables declared in the specification as
key=value:yc serverless api-gateway update \ --id <API_gateway_ID> \ --variables <list_of_variables>
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To provide the values of the variables declared in the API gateway specification:
-
Open the Terraform configuration file and add the
variablessection to it:resource "yandex_api_gateway" "<API_gateway_name>" { ... variables = { <list_of_variables> } ... }List the variables in
key = valueformat, such as:resource "yandex_api_gateway" "<API_gateway_name>" { ... variables = { environment = "testing" } ... } -
Check the configuration using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planThe terminal will display a list of resources with their properties. No changes will be made at this step. If the configuration contains any errors, Terraform will point them out.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.You can check the API gateway update using the management console
or this CLI command:yc serverless api-gateway get <API_gateway_name>
To set up variables for an API gateway specification, use the update REST API method for the ApiGateway resource or the ApiGatewayService/Update gRPC API call.