Create and deploy IBM Cloud function actions
Important!
IBM Cloud® Functions is deprecated. Existing Functions entities such as actions, triggers, or sequences will continue to run, but as of 28 December 2023, you can’t create new Functions entities. Existing Functions entities are supported until October 2024. Any Functions entities that still exist on that date will be deleted. For more information, see Deprecation overview.
If you’re an existing IBM Cloud® Functions user, you can continue to use the service until it is no longer supported in October 2024.
Clients are encouraged to migrate their Functions actions and triggers to IBM Cloud® Code Engine. Alternatively, clients might decide to host their code on VPC virtual instances or in IBM Cloud Kubernetes or OpenShift for IBM Cloud® clusters.
See the following topics for documentation about migrating from IBM Cloud® Functions to Code Engine.
If the above didn’t discourage you and you have a valid reason to create your cloud functions outside of Code Engine then please continue reading.
Prerequisites:
- IBM Cloud account (pay-as-you go will do)
- Working knowledge of the terminal
- Initialize new node.js project
npm init
- Edit
package.json
and add the following:
{
"name": "<function name>",
"version": "1.0.0",
"main": "main.js",
"dependencies": {
"axios": "^1.6.0"
}
}
- Run
npm install
in the root of the project. This will installaxios
dependency. - Create
main.js
file in the root of the project and add the following example code:
function main(params) {
var msg = 'You did not tell me who you are.';
if (params.name) {
msg = `Hello, ${params.name}!`
} else {
msg = `Hello, Functions on CodeEngine!`
}
return {
headers: { 'Content-Type': 'text/html; charset=utf-8' },
body: `<html><body><h3>${msg}</h3></body></html>`
}
}
module.exports.main = main;
- Create
manifest.yml
file in the root of the project and the following code:
packages:
main-package:
version: 1.0
license: Apache-2.0
actions:
main:
function:main.js
- Add the following code to
package.json
(It will be responsible for deploying the function to your IBM Cloud Functions instance).
...
"scripts": {
"deploy": "ibmcloud fn deploy --manifest manifest.yml"
}
You can now use
npm run deploy
to deploy your function but it won’t work as you require to be logged in to IBM Cloud instance in your terminal and point to the right Namespace of IBM Cloud Functions. Let’s create both in the next steps.
- Log in to your IBM Cloud instance in your terminal. You can find the command using The IBM Cloud UI and clicking the top right corner user icon, choosing the “Log in to CLI and API” option from the dropdown, and copying your login command.
- Select the namespace in your terminal. You can find the command by clicking through the menus on the left sidebar from The IBM Cloud UI.
Follow the steps from the screen.
Now you can deploy your function by running
npm run deploy
from the root of your project.If the terminal response is
Success: Deployment completed successfully.
then go to IBM Cloud Function UI and click Actions from the left sidebar to see successfully deployed functions.
I recommend following the documentation at openwhisk-wskdeploy to find out more about how to configure your manifest.yml.