{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rickymoorhouse.uk/blog/2022/ibm-cloud-functions/index",
  "description": "Originally posted on the IBM Integration Community Blog",
  "path": "/blog/2022/ibm-cloud-functions/index",
  "publishedAt": "2022-10-25T00:00:00.000Z",
  "site": "at://did:plc:r53zv4vpzeihop3aliwyejlu/site.standard.publication/3mos5q3a7jf2w",
  "tags": [
    "apiconnect",
    "serverless"
  ],
  "textContent": "Cloud functions offer an easy way to build functionality and only pay for the time they are actually running  without having to worry about how it will be hosted - you just right the code, save it and it is ready to be invoked. The function can be invoked through an event trigger or as a REST API - in this case I'm going to show how you can trigger a function as part of your API Connect assembly flow - providing an easy way to manage and socialise the functionality you have built.\n\nTo call an IBM Cloud function you first need to exchange your IBM Cloud API Key for an IAM access key using the IAM Authentication API.  \n\nThis is a case of setting up and sending a post request to IBM Cloud IAM, to do this I used a set-variable policy to configure the headers and body followed by an invoke to https://iam.cloud.ibm.com/identity/token. The set-variable looks like this:\n\nOnce you have this access key you can use this to call your function using the endpoint provided in the UI:\n\n{{{<diagram example-function.png >}}\n\nFor the authentication to the function URL we need to set the Authorization header to Bearer {access_key} where access_key is from the body of the previous request, so to obtain this we can use a Parse policy to parse the returned json, and then another set-variable policy to configure the headers for the Function invoke. This time the set-variable looks like this:\n\nAs you can see we're setting the authorization and accept headers, and passing the original request body from the inbound request.  We follow this with an invoke of the function URL and a map, because the JSON returned from the function API includes details on invocation as well as the defined response:\n\n{{{<diagram \"map.png\" >}}\n\nThe end to end flow looks like this:\n\n{{{<diagram \"end-to-end-flow.png\" >}}\n\nHere is the complete sample swagger file  which can be used in any API Connect deployment, including our new API Connect service on AWS",
  "title": "Invoke IBM Cloud Functions from API Connect"
}