Getting started with API Gateway
Follow this guide to create and test different types of extensions. First, you will set up an API gateway to get a static response and then add an integration for invoking a function. You will need curl
Getting started
To get started in Yandex Cloud:
- Log in to the management console
. If not signed up yet, navigate to the management console and follow the instructions. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account yet, create one. - If you do not have a folder yet, create one.
Create an API gateway
-
In the management console
, select the folder where you want to create an API gateway. -
In the list of services, select API Gateway.
-
Click Create API gateway.
-
In the Name field, enter
numbers
. -
(Optional) In the Description field, enter a description.
-
In the Specification section, add the specification:
openapi: "3.0.0" info: version: 1.0.0 title: Test API paths: /hello: get: summary: Say hello operationId: hello parameters: - name: user in: query description: User name to appear in greetings required: false schema: type: string default: 'world' responses: '200': description: Greeting content: 'text/plain': schema: type: "string" x-yc-apigateway-integration: type: dummy http_code: 200 http_headers: 'Content-Type': "text/plain" content: 'text/plain': "Hello, {user}!\n"
-
Click Create.
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 create an API gateway:
-
Describe the parameters of the
yandex_api_gateway
resource in the configuration file:-
name
: API gateway name. The name format is as follows:- The name must be from 3 to 63 characters long.
- It may contain lowercase Latin letters, numbers, and hyphens.
- The first character must be a letter and the last character cannot be a hyphen.
-
description
: API gateway description. -
labels
: Labels for the API gateway. Specify a key-value pair. -
spec
: API gateway specification.
Here is an example of the configuration file structure:
resource "yandex_api_gateway" "test-api-gateway" { name = "<API_gateway_name>" description = "<API_gateway_description>" labels = { label = "label" empty-label = "" } spec = <<-EOT openapi: "3.0.0" info: version: 1.0.0 title: Test API paths: /hello: get: summary: Say hello operationId: hello parameters: - name: user in: query description: User name to appear in greetings required: false schema: type: string default: 'world' responses: '200': description: Greeting content: 'text/plain': schema: type: "string" x-yc-apigateway-integration: type: dummy http_code: 200 http_headers: 'Content-Type': "text/plain" content: 'text/plain': "Hello, {user}!\n" EOT }
For more information about resource parameters in Terraform, see the provider documentation
. -
-
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configuration using the management console
or these CLI commands:yc serverless api-gateway get <API_gateway_name>
-
Access the API gateway
-
In the management console
, select the folder containing the API gateway. -
In the list of services, select API Gateway and click the name of the created API gateway.
-
Save the value of the Default domain field.
-
Install curl
. -
Access the API gateway using curl with one of the commands:
-
curl <service_domain>/hello?user=API
-
curl <service_domain>/hello
Where
<service_domain>
is the value of the Default domain field you saved earlier.For example:
curl https://d5dm1lba80md********.apigw.yandexcloud.net/hello?user=API
Result:
-
Hello, API!
-
Hello, world!
-
Add an integration with the function
Create a function
Create a function to get a list of numbers. Read more about functions in the Yandex Cloud Functions documentation.
To create a function:
- Create a function:
- In the management console
, select the folder to create your function in. - Click Create resource.
- Select Function.
- In the Name field, specify
list
. - Click Create.
- In the management console
- Create a function version:
-
Select the
nodejs18
runtime environment. -
Enable Add files with code examples.
-
Click Continue.
-
In the Method field, select
Code editor
. -
Click Create file in the editor below.
- In the window that opens, enter the
index.js
file name. - Click Create.
- In the window that opens, enter the
-
Paste the following code in the
index.js
file:module.exports.handler = async (event) => { return { "statusCode": 200, "headers": {"content-type": "application/json"}, "body": "[0, 1, 2]" }; };
-
In the Entry point field, specify
index.handler
. -
Click Save changes.
-
- Make your function public.
To create a function:
-
Prepare a ZIP archive with the function code:
-
Save the following code to a file named
index.js
:module.exports.handler = async (event) => { return { "statusCode": 200, "headers": {"content-type": "application/json"}, "body": "[0, 1, 2]" }; };
-
Add
index.js
to thehello-js.zip
archive.
-
-
Describe the
yandex_function
resource parameters in the configuration file:resource "yandex_function" "test-function" { name = "test-function" description = "Test function" user_hash = "first-function" runtime = "nodejs18" entrypoint = "index.handler" memory = "128" execution_timeout = "10" service_account_id = "<service_account_ID>" tags = ["my_tag"] content { zip_filename = "<path_to_ZIP_archive>" } }
Where:
name
: Function name.description
: Text description of the function.user_hash
: Any string to identify the function version. When the function changes, update this string, too. The function will update when this string is updated.runtime
: Function runtime environment.entrypoint
: Function name in the source code that will serve as an entry point to the applications.memory
: Amount of memory allocated for the function, in MB.execution_timeout
: Function execution timeout.service_account_id
: ID of the service account to invoke the function under.tags
: Function tags.content
: Function source code.content.0.zip_filename
: Path to the ZIP archive containing the function source code.
For more information about the
yandex_function
resource parameters, see the provider documentation . -
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configuration using the management console
or these CLI commands:yc serverless function list
-
Extend the API gateway specification
Add function information to the API gateway specification.
To update an API gateway specification:
-
In the management console
, select the folder where you want to update an API gateway. -
In the window that opens, select the API gateway and click
. -
In the menu that opens, click Edit.
-
Under Specification, add an extended version of the specification.
The
/numbers
method, which uses thecloud_functions
typex-yc-apigateway-integration
extension, invokes a function by ID.To ensure that the API gateway works properly, in the
function_id
parameter, specify the ID of the function to invoke. To enable the API gateway to access a private function, in theservice_account_id
parameter, specify a service account that has permissions to invoke the function.openapi: "3.0.0" info: version: 1.0.0 title: Test API paths: /hello: get: summary: Say hello operationId: hello parameters: - name: user in: query description: User name to appear in greetings required: false schema: type: string default: 'world' responses: '200': description: Greeting content: 'text/plain': schema: type: "string" x-yc-apigateway-integration: type: dummy http_code: 200 http_headers: 'Content-Type': "text/plain" content: 'text/plain': "Hello, {user}!\n" /numbers: get: summary: List some numbers operationId: listNumbers responses: '200': description: Another example content: 'application/json': schema: type: "array" items: type: "integer" x-yc-apigateway-integration: type: cloud_functions function_id: <function_ID > service_account_id: <service_account_ID>
To add function information to the API gateway specification:
-
Open the Terraform configuration file and add the
/numbers
method, which uses thecloud_functions
typex-yc-apigateway-integration
extension to invoke a function by ID. Underspec
, change the API gateway specification by specifying the following parameters:function_id
: Function ID.service_account_id
: ID of the service account with permissions to invoke the function.
Extended API gateway specification:
... spec = <<-EOT openapi: "3.0.0" info: version: 1.0.0 title: Test API paths: /hello: get: summary: Say hello operationId: hello parameters: - name: user in: query description: User name to appear in greetings. required: false schema: type: string default: 'world' responses: '200': description: Greeting content: 'text/plain': schema: type: "string" x-yc-apigateway-integration: type: dummy http_code: 200 http_headers: 'Content-Type': "text/plain" content: 'text/plain': "Hello again, {user}!\n" /numbers: get: summary: List some numbers operationId: listNumbers responses: '200': description: Another example. content: 'application/json': schema: type: "array" items: type: "integer" x-yc-apigateway-integration: type: cloud_functions function_id: <function_ID> service_account_id: <service_account_ID> EOT }
For more information about resource parameters in Terraform, see the provider documentation
. -
Make sure the configuration files are correct.
-
In the command line, go to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply
-
Confirm creating the resources: type
yes
in the terminal and press Enter.All the resources you need will then be created in the specified folder. You can check the new resources and their configurations using the management console
or these CLI commands:yc serverless api-gateway get <API_gateway_name>
-
Access the function via the API gateway
Note
To allow the API gateway to access the function, make it public or specify in the specification a service account that has permissions to invoke the function.
Access the API gateway:
curl <service_domain>/numbers
Where <service_domain>
is the value of the Default domain field you saved earlier.
For example:
curl https://d5dm1lba80md********.apigw.yandexcloud.net/numbers
Result:
[0, 1, 2]