Call a registered external service from Kyma
This guide shows how to call a registered external service from Kyma using a simple Function.
Prerequisites
- A registered external service
- Your service display name exported as an environment variable
- Your Application name exported as an environment variable
- Your desired Namespace, cluster domain, and the names for your Function and APIRule exported as environment variablesClick to copyexport NAMESPACE=defaultexport CLUSTER_DOMAIN=local.kyma.devexport FUNCTION_NAME=my-functionexport APIRULE_NAME=$FUNCTION_NAME-ar
- Istio sidecar injection enabled in your NamespaceClick to copykubectl label namespace $NAMESPACE istio-injection=enabled
CAUTION: On a local Kyma deployment, skip SSL certificate verification when making a
curl
call, by adding the-k
flag to it. Alternatively, add the Kyma certificates to your local certificate storage on your machine using thekyma import certs
command.
Steps
Build a path to access your registered service:
Click to copyexport GATEWAY_URL=http://central-application-gateway.kyma-system:8080/$APP_NAME/$SERVICE_DISPLAY_NAMECAUTION:
SERVICE_DISPLAY_NAME
in the GATEWAY_URL path must be in its normalized form. This means that, for example, if you usedtest-basic-auth
as the service displayName, you're good to go, but if you used"Test Basic Auth"
, you must replace it withtest-basic-auth
in the path.Create a Function that sends a request to the registered service with the additional path of
/uuid
. A successful response returns a UUID generated byhttpbin.org
. To create and register the Function in the desired Namespace, run:Click to copycat <<EOF | kubectl apply -f -apiVersion: serverless.kyma-project.io/v1alpha2kind: Functionmetadata:name: $FUNCTION_NAMEnamespace: $NAMESPACElabels:app: $APP_NAMEspec:runtime: nodejs18source:inline:source: |-const request = require('request');module.exports = { main: function (event, context) {return new Promise((resolve, reject) => {const url = process.env.GATEWAY_URL + "/uuid";const options = {url: url,};sendReq(url, resolve, reject)})} }function sendReq(url, resolve, reject) {request.get(url, { json: true }, (error, response, body) => {if(error){resolve(error);}resolve(response.body);})}dependencies: |-{"name": "example-1","version": "0.0.1","dependencies": {"request": "^2.85.0"}}env:- name: GATEWAY_URLvalue: $GATEWAY_URLEOFTo expose the Function outside the cluster, create an APIRule custom resource.
Click to copycat <<EOF | kubectl apply -f -apiVersion: gateway.kyma-project.io/v1beta1kind: APIRulemetadata:name: $APIRULE_NAMEnamespace: $NAMESPACElabels:function: $FUNCTION_NAMEspec:gateway: kyma-system/kyma-gatewayhost: $APIRULE_NAME.$CLUSTER_DOMAINrules:- path: /.*accessStrategies:- config: {}handler: noopmethods:- GETservice:name: $FUNCTION_NAMEport: 80EOFTo verify that everything was set up correctly, you can now call the Function through HTTPS:
Click to copycurl https://$APIRULE_NAME.$CLUSTER_DOMAIN/NOTE: If you get nothing or
no healthy upstream
as a response, wait for a few moments for the Function to get ready and call it again.A successful response returns a UUID generated by
httpbin.org
:Click to copy{"uuid": "d44cc373-b26e-4a36-9890-6418d131a285"}