In this sample, you will create an API with API Gateway where each path is handled by a different Cloud Function.
We'll deploy two simple functions defined in index.js:
exports.helloWorld = (req, res) => {
res.send('Hello, World');
};
exports.byeWorld = (req, res) => {
res.send('Bye, World');
};
Deploy both functions:
./1_deploy_functions.sh
Enable required services for API Gateway and create a greeter-api
API:
./2_create_api.sh
Create an API config using the OpenAPI spec to define which path goes to which function.
We'll use openapi2-functions.yaml as a starter but replace REGION
and
PROJECT_ID
with real values. This definition routes /hello
to the
hello function and /bye
to the bye function.
./3_create_api_config.sh
The final step is to deploy the API config to a gateway:
./4_create_gateway.sh
To test the API, first fetch the default host name of the gateway and then make
a call with /hello
and /bye
paths:
./5_test_gateway.sh
You should see something similar:
# Test hello
curl https://greeter-gateway-5dvbajef.ew.gateway.dev/hello
Hello, World
# Test bye
curl https://greeter-gateway-5dvbajef.ew.gateway.dev/bye
Bye, World%