Setting up variables
API Gateway supports variables in an OpenAPI specification.
Sample 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 command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To set up your variables:
-
If necessary, edit the OpenAPI specification file or create a new one.
-
View a 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 <gateway_ID> \ --variables <list_of_variables>
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
If you don't have Terraform, 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
variables
section to it:resource "yandex_api_gateway" "<API_gateway_name>" { ... variables = { <list_of_variables> } ... }
List the variables in
key = value
format, such as:resource "yandex_api_gateway" "<API_gateway_name>" { ... variables = { environment = "testing" } ... }
-
Check the configuration using this command:
terraform validate
If the configuration is correct, you will get this message:
Success! The configuration is valid.
-
Run this command:
terraform plan
The terminal will display a list of resources with parameters. No changes will be made at this step. If the configuration contains any errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
into the terminal and press Enter.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.