OpenFaas with Golang
This is a short and simple blog to get you started with OpenFaas with Golang on windows machine(with WSL2).
Prerequisites
- Windows 10 OS with WSL 2 Enabled
- Docker Desktop Installed on machine(for running docker container)
- Basic Understanding for Docker and Golang
If you have all the prerequisites, then you can follow below steps to get started
- install go and faas-cli – You can go the offcial golang website to know how to install go on you local machine and same can be done for faas-cli as well.
- Clone/Copy OpenFaas environment on your local machine for testing
git clone https://github.com/openfaas/faas
- Once cloned, go inside that directory and execute following power shell script
deploy_stack.ps1
which will install the required services to run and manage you FaaS function
- Initialize docker swarm(or you can also use kubernates as your orchestrator)
docker swarm init
- Go to inside this directory “C:\Users\USER_NAME\go\src\github.com\YOUR_GITHUB_USERNAME\openfaas-golang\” . Create directory whichever is missing from that path.
After that create new directory “user-module”, then executed below command to allow faas-cli to create new function with default deployment file(i.e. YML file)
faas-cli new --lang go login-function
- Once Project is created and you have written your logic inside handler.go file, you can start the building process with following command.
fass-cli build -f login-function.yml --build-arg GO111MODULE=on
- You might get an error if you don’t have templates not on your local machine, you can resolve that issue by downloading the templates with following command
faas-cli template pull https://github.com/openfaas-incubator/golang-http-template
- Once it gets built successfully, you can start the deployment. Just make sure that you have logged into the cli before deploying, Otherwise it give you an error.
fass-cli deploy-f login-function.yml
- Open Faas environment that we have deployed in step 2, comes with an UI interface to access the deployed function for testing. You can access the User Interface with on following URL.
Bonus Tips
If you lost your password, Run Following command in WSL2 to get it
docker run -ti -v /var/run/docker.sock:/var/run/docker.sock \ alexellis2/jaas:1.1.0 \ run --secret basic-auth-password \ --image alpine:3.10 \ --command "cat /run/secrets/basic-auth-password"
Github Link for sample project with golang : Github