Creating function workloads from existing code bundles
Create your Code Engine function with a code bundle in IBM Cloud® Container Registry. You can create a function from the console or with the CLI.
A code bundle is a collection of files that represents your function code. This code bundle is injected into the runtime container. Your code bundle is created by Code Engine and is stored in container registry or inline with the function. A code bundle is not a Open Container Initiative (OCI) standard container image.
You cannot pull code bundles from a location other than Container Registry.
Before you begin
-
You must have a code bundle in IBM Cloud® Container Registry. For more information, see Getting started with Container Registry. Or, you can build an image from repository source or from local source.
-
Verify that you can access the registry. See Setting up authorities for container registries.
Interested in configuring your project such that all users of the project can store and access images in Container Registry without having to manually create registry secrets? With sufficient permissions, you can configure this default registry access on a per location (region) basis. If you don't have sufficient permissions to perform these actions, you can use this page to help you understand the required permissions. See Configuring project-wide settings.
Creating a function that references a code bundle in Container Registry with the console
Create a function that uses an existing code bundle by using the Code Engine console.
Code Engine can automatically pull code bundles from Container Registry.
- Open the Code Engine console.
- Select Let's go.
- Select Function.
- Enter a name for the function; for example,
myfunction
. Use a name for your function that is unique within the project. - Select a project from the list of available projects. You can also create a new one. You must select a project to create a function.
- Select a Runtime image for your function code. For more information, see Runtimes.
- Select to Use an existing code bundle.
- Specify the location of your code bundle in the format
registry/namespace/repository:tag
. - If your code bundle is in your account or in a public registry, you do not need to specify a registry secret. If the code bundle is in a private registry, you must add the registry secret. For additional help, click Help me specify the code bundle. For more information, see Accessing container registries.
- Specify your resource information, including CPU and memory combinations and Scale down delay.
- Optionally, specify a custom domain or environment variables.
- Click Create.
- After the function status changes to Ready, you can test the function. Click Test function and then click Send request. To open the function in a web page, click Function URL.
Deploying a function with an existing code bundle with the CLI
Create a function that uses a code bundle that is stored in IBM Cloud® Container Registry with the CLI with the ibmcloud ce fn create
command. For a complete listing of options, see the ibmcloud ce fn create
command.
Before you begin
- Set up your Code Engine CLI environment.
- Create and work with a project.
- Before you can work with a Code Engine function that references a code bundle in Container Registry, you must first add access to the registry so Code Engine can pull the code bundle when the function is created. For information about the required permissions for accessing registries, see Setting up authorities for image registries.
Deploying a function from a public registry with the CLI
If your existing code bundle is in your account or a public registry, you can use the ibmcloud ce fn create
command.
For example, run the following command to pull an existing code bundle from the public registry and create a function.
ibmcloud ce fn create --name myhellofun --code-bundle icr.io/codeengine/samples/function-nodejs-codebundle --runtime nodejs
Run the provided fn get
command to find the URL. You can then use the curl
command to call that URL.
Example output from the existing code bundle.
<html><body><h3>Hello, Functions on CodeEngine!</h3></body></html>
Deploying a function from a private registry with the CLI
To access an existing code bundle from a private registry, you must set up access to the registry, create a registry secret, and then provide that secret when you run the ibmcloud ce fn create
command.
-
To add access to Container Registry, create an IAM API key. To create an IBM Cloud IAM API key with the CLI, run the
iam api-key-create
command. For example, to create an API key calledcliapikey
with a description ofMy CLI API key
and save it to a file calledkey_file
, run the following command:ibmcloud iam api-key-create cliapikey -d "My CLI API key" --file key_file
If you choose to not save your key to a file, you must record the API key that is displayed when you create it. You cannot retrieve it later.
-
After you create your API key, create a registry secret to Code Engine. To create a registry secret, use the
ibmcloud ce secret create --format registry
command. For example, the following command creates registry access to a Container Registry instance calledmyregistry
. Note, even though the--server
and--username
options are specified in the example command, the default value for the--server
option isus.icr.io
and the--username
option defaults toiamapikey
when the server isus.icr.io
.ibmcloud ce secret create --format registry --name myregistry --server us.icr.io --username iamapikey --password APIKEY
Example output
Creating registry secret 'myregistry'... OK
-
Create your function and reference a code bundle in a private registry in Container Registry. For example, use the
ibmcloud ce fn create
command to create themyhellofun
function to reference theicr.io/codeengine/samples/function-nodejs-codebundle
by using themyregistry
access information.ibmcloud ce fn create --name myhellofun --code-bundle icr.io/NAMESPACE/REGISTRY:TAG --registry-secret myregistry
The format of the name of the code bundle for this function is
REGISTRY/NAMESPACE/REGISTRY:TAG
whereREGISTRY
andTAG
are optional. IfREGISTRY
is not specified, the default isicr.io
. IfTAG
is not specified, the default islatest
. -
After your function creates, you can invoke it. To find the URL of your function, run the provided
ibmcloud ce fn get
command. You can then use thecurl
command to call that URL.
Next steps
-
After your function is created, you can access your function by clicking Test function in the console or finding the URL for your function with the
function get
command. -
You can create a custom domain mapping and assign it to your function.
-
After your function is created and deployed, you can update the function to meet your needs from the console or by using the
ibmcloud ce function update
command. If you want to update your source to use with your function, you must provide the--build-source
option on thefunction update
command.
After your function is created, you can update your function and its referenced code by using any of the following ways, independent of how you created or previously updated your function:
-
If you have an existing code bundle, then you need to provide only a reference to the image, which points to the location of your container registry when you deploy your app. For more information, see Creating function workloads from existing code bundles.
If you created your function by using the
function create
command and you specified the--build-source
option to build the code bundle from local or repository source, and you want to change your function to point to a different code bundle, you must first remove the association of the build from your function. For example, runibmcloud ce function update -n FUN_NAME --build-clear
. After you remove the association of the build from your function, you can update the function to reference a different image. -
If you are starting with source code that resides in a Git repository, you can choose to let Code Engine to build the code bundle from your source and create your function with a single operation. In this scenario, Code Engine uploads your code bundle to IBM Cloud® Container Registry. To learn more, see Creating your function from repository source code.
-
If you are starting with source code that resides on a local workstation, you can choose for Code Engine to build the code bundle from your source and create your function with a single CLI command. In this scenario, Code Engine uploads your source code and code bundle to IBM Cloud® Container Registry.
For example, you might choose for Code Engine to build your local source while you evolve the development of your source for the function. Then, after the code bundle is matured, you can update your function to reference the specific code bundle that you want. You can repeat this process as needed.
Looking for more code examples? Check out the Samples for IBM Cloud Code Engine GitHub repo.