Deploy a function with Azion CLI
Create a function project from the terminal with Azion CLI, run it on your machine, and deploy it to a live domain.
In this tutorial, you will build and deploy a function from the terminal with Azion CLI. You will save a personal token and create a project. Then you will write the handler, run it on your machine, and deploy it.
The Functions quickstart reaches the same result through Azion Console. This page is the terminal path, and it ends with the same three objects: a function, an instance, and the application that serves it.
Prerequisites
- An Azion account. To create one, refer to Create an account.
- Azion CLI installed on your machine.
- A personal token.
- Node.js and a package manager, which the CLI uses to build the project.
1. Save your personal token
The CLI authorizes every command with a personal token stored on your machine. To save it:
The token is stored locally, and the commands that follow authorize with it. You do not pass it again.
2. Create the project
azion init creates the project and its build configuration. To start it:
The command asks for a name, suggesting one you can accept:
Then it asks which preset to use. Select Javascript, the preset for a function written in plain JavaScript rather than a framework:
Then it asks which template to start from. Select Hello World:
It asks whether to install the project dependencies. Answer Y:
It asks whether to start a local development server. Answer N, because step 4 starts it:
The project directory now holds the build configuration and the entrypoint the build reads.
3. Write the handler
Open index.js, the entrypoint the build reads by default, and replace its contents with an ES Modules handler:
The fetch method receives the request, the environment variables, and the execution context, and returns the response the client receives. This is the pattern Azion recommends.
4. Run the function locally
Before deploying, run the project on your machine and send it a request. To start the local server with azion dev:
The command builds the project, then prints the address it serves on:
From another terminal, request that address:
The handler answers with its response body:
Stop the server with Ctrl+C once the response is correct.
5. Deploy the project
To deploy the project to Azion with azion deploy:
The command builds the project and creates the application and the function on your account. It instantiates the function and prints the domain that serves it. The domain has the format xxxxxxxxxx.map.azionedge.net.
Requesting that domain returns the same response you read locally:
The first deploy can take several minutes to answer from every location. An immediate request can fail before the configuration is in place. Wait and retry before diagnosing.