Skip to content

Commit

Permalink
Improve how function.json is initialized
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Ken Bannister <[email protected]>
  • Loading branch information
kb2ma committed Mar 7, 2022
1 parent 7d0a63c commit bab4b15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Microsoft Azure Function for IoT Device Provisioning
This Azure Function allows you to provision and synchronize a balena device with Azure IoT in a secure and automated way via an HTTP endpoint. The Azure Function may be called by a balena device, as seen in the [cloud-relay](https://github.com/balena-io-examples/cloud-relay) example.

| Command | Actions |
| Method | Actions |
|---------|---------|
| POST | Provisions a balena device with Azure IoT. First the function verifies the device UUID with balenaCloud. Then it creates a public key certificate and registers the device with IoT Hub. Finally the function pushes the certificate and private key to balena device environment variables. |
| DELETE | Removes a balena device from the IoT Hub and removes the balena device environment variable. Essentially reverses the actions from provisioning with POST. |
Expand All @@ -24,6 +24,7 @@ func init provision-func --javascript \
# copy provisioning code into workspace
cp ../provision-repo/src/index.js provision \
&& cp ../provision-repo/src/function.json provision \
&& cp ../provision-repo/src/package* . \
&& npm install
Expand All @@ -32,8 +33,6 @@ cp ../provision-repo/tools/*.sh . \
&& echo -e '*.sh\n*.pem' >.funcignore
```

You must edit `provision-func/provision/function.json`, which was created by the `func init` command above. Ensure the `methods` array attribute includes `delete` and `post`.

Finally, you must edit the `run-local-server.sh` and `create-func.sh` scripts copied above to provide these environment variables:

| Key | Value |
Expand All @@ -48,7 +47,7 @@ The HTTP endpoint expects a request containing a JSON body with the attributes b
| Attribute | Value |
|-----------|-------|
| uuid | UUID of device |
| balena_service | (optional) Name of service container on balena device. If defined, creates service level variables; otherwise creates device level variables. Service level variables are more secure. |
| balena_service | (optional) Name of service container on balena device that uses provisioned key and certificate, for example `cloud-relay`. If defined, creates service level variables; otherwise creates device level variables. Service level variables are more secure. |

### Test locally
Start the local server by running `run-local-server.sh`.
Expand Down
19 changes: 19 additions & 0 deletions src/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "Anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"delete",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}

0 comments on commit bab4b15

Please sign in to comment.