Deploy a Function with the Azion API
Create a Function, an Application, and a Workload with curl calls to the Azion API v4, and update the code with a PATCH.
In this tutorial, you will deploy a Function on Azion with nothing but curl and the Azion API v4. You will create the Function, an Application that runs it, and a Workload that serves it, then update the code. If you want, the last stage deletes everything.
Every resource the tutorial creates links to one created before it:
- The Function holds the code.
- The Application has Functions turned on, a function instance that attaches the Function, and a request rule that runs it on every path.
- The Workload holds the domain that receives requests, and its deployment sets the Application that answers them.
Every request goes to the base URL https://api.azion.com/v4.
Prerequisites
- An Azion account. To create one, refer to Create an account.
- A personal token. To create one, refer to Personal tokens.
curl, to call the API and request the Function.jq, to read IDs from the API responses.
1. Prepare the terminal
Create a directory for the tutorial and go to it:
Store your personal token in a shell variable. Replace <your-personal-token> with your token:
Every request in the next stages sends the token in the Authorization: Token header. The variable exists only in this terminal session, so run every command of the tutorial in the same terminal.
2. Create the Function
Create an index.js file with the code of the Function. It answers every request with a JSON message:
Send a POST request to the functions endpoint. jq reads index.js into the code field of the request body:
The response carries the new Function. This excerpt shows the fields the next stages use:
Store the ID of the Function:
The account holds the Function, and FUNCTION_ID holds its ID.
3. Create the Application
The Application needs three calls: one creates it, one attaches the Function, and one adds the rule that runs the Function.
Create the Application with Functions turned on:
Store the ID of the Application:
Attach the Function to the Application with a function instance:
Store the ID of the function instance:
Add a request rule that runs the function instance on every path. The criterion matches every URI that starts with /:
The Application runs the Function on every request it receives.
4. Create the Workload and deploy the Application
Create the Workload:
The response carries the domain that Azion generated for the Workload:
Your domain has the same format, with different letters. Store the ID and the domain:
Create the Workload’s deployment, which sets the Application that answers the Workload’s requests:
The Workload sends its requests to the Application, which runs the Function.
5. Request the Function
Send a request to the domain:
A new Workload can take about ten minutes to propagate across the Azion network. Until then, the domain answers 404 with an HTML error page, and it can alternate between 404 and the Function’s response for a few minutes. Repeat the request every minute until the JSON message returns three times in a row:
The Function answers requests on the Workload’s domain.
6. Update the Function
In index.js, change the message to Hello again from the Azion API:
Send the new code in a PATCH request to the Function:
The response carries the Function with the new code:
Request the domain again:
The new code takes less time to propagate than a new Workload. While it propagates, the domain can answer with the old message or, for a few seconds, with the 404 page. Repeat the request until the response carries the new message:
The Function runs the new code on the same domain.
7. (Optional) Delete the resources
The Function keeps serving your domain after the tutorial. If you want to remove the resources, delete them in the reverse order of their creation. Deleting the Workload also deletes its deployment:
Deleting the Application also deletes its function instance and its rule:
Delete the Function:
Each call answers with the same body:
A deletion completes a few seconds after the call answers. Check that the Workload no longer exists, and repeat the request until it answers 404:
The account no longer holds the Function, the Application, or the Workload. The removal also propagates, so the domain can keep answering for a few minutes.