Develop locally¶
Install the func CLI, scaffold a function project, run it on your machine,
and test it with HTTP or CloudEvents requests — no Kubernetes cluster or
container registry required.
Prepare¶
- A running Docker engine
for containerized builds (
packors2i). For--builder host, Docker is optional, but the target language toolchain (Go or Python) must be installed on your host. - Install the
funcCLI (orkn funcplugin):
brew install knative-extensions/kn-plugins/func
If kn is also installed via Homebrew, kn func is available automatically.
Download the binary for your OS and architecture from the
func release page,
make it executable, and place it on your PATH as func (or as kn-func
to use it as a kn plugin).
Requires Go and make:
git clone https://github.com/knative/func.git func
cd func
make
Place the resulting func binary on your PATH.
Verify the installation using the command matching how you installed it:
func version
# or, if installed as a kn plugin:
kn func version
Create a project¶
Create a new function directory using -l (language) and -t (template:
http or cloudevents, default http). See
Templates and runtimes for the full list of supported
languages.
func create -l go -t http my-function
cd my-function
kn func create -l go -t http my-function
cd my-function
func create writes a new my-function/ directory containing func.yaml,
handler source, and starter tests. Pass -t cloudevents if your function will
consume CloudEvents. For all flags, see the
func create reference.
Run locally¶
Inside the project directory, build and run the function locally. Pass an
explicit localhost/... image name so the build stays local without needing a
container registry:
func run --builder pack \
--image localhost/my-function:latest
kn func run --builder pack \
--image localhost/my-function:latest
Wait until the function prints its local address, for example:
Function running on 127.0.0.1:8080
Useful flags:
--builder host— run Go or Python directly on your host without Docker.--build=true— force a rebuild even if no source changes were detected.--build=false— reuse an already-built image without rebuilding (fails if no image has been built yet).--address 127.0.0.1:8081— bind to a different local port.
Keep this terminal open while testing.
Send test requests¶
In a second terminal inside the same project directory, use func invoke
with --target local so you are guaranteed to hit the local instance:
func invoke --target local \
--content-type application/json \
--data '{"message":"Hello local!"}'
func invoke --target local \
--format cloudevent \
--type dev.knative.example \
--source /local-test \
--data '{"message":"Hello event!"}'
(kn func invoke accepts the same flags.) Confirm the response body matches
your handler's output. You can also target a deployed instance with
--target remote or an arbitrary endpoint with --target https://.... See the
func invoke reference
for all options.
Iterate and stop¶
- Press Ctrl+C in the
func runterminal to stop the local function. - Edit your handler code and save.
- Re-run
func runand verify the updated output withfunc invoke --target local.
When you are ready to package or ship the function, continue to Build an image or Deploy a function.