Manage an Application with Terraform
Deploy a Function on Azion with Terraform and change its code with a second apply, with an optional destroy at the end.
In this tutorial, you will deploy a Function on Azion with Terraform and manage it as code. You will declare the resources in code, create them with one apply, and change the Function. If you want to remove everything at the end, one destroy does it.
The configuration declares six resources. Each one links to a resource above it:
azion_functionholds the code of the Function.azion_application_main_settingcreates the Application, with Functions turned on.azion_application_function_instanceattaches the Function to the Application.azion_application_rule_engineadds a request rule that runs the Function on every path.azion_workloadcreates the Workload, which holds the domain that receives requests.azion_workload_deploymentsets the Application that the Workload sends its requests to.
Prerequisites
- Terraform 1.0 or later.
- An Azion account. To create one, refer to Create an account.
- A personal token. To create one, refer to Personal tokens.
curl, to send requests to the Application.
1. Create the project
Create a directory for the project and go to it:
Create a versions.tf file. It requires the Azion Terraform Provider at version 2.10.0:
The provider "azion" block stays empty, because the provider reads your personal token from the environment.
Download the provider:
The output names the installed provider and ends with a success line:
The directory holds the provider and a .terraform.lock.hcl file that records its version.
2. Provide your personal token
The provider reads the token from the AZION_API_TOKEN environment variable. In the terminal that runs Terraform, set the variable. Replace <your-personal-token> with your token:
The variable exists only in this terminal session, and no file in the project holds the token.
3. Write the Function
Create an index.js file. The Function answers every request with a JSON message and a version number:
Terraform reads this file and sends its content as the code of the Function.
4. Declare the resources
Create a main.tf file with the six resources and an output that prints the Workload’s domain:
Four lines of the file need a reason:
chomp(file(...))removes the final newline ofindex.js. Azion stores the code without it, and withoutchompthe apply stops withProvider produced inconsistent result after apply.$${uri}writes the literal text${uri}, the rule variable that holds the request path. The doubled$stops Terraform from reading it as an interpolation.- The
protocolsandtlsblocks repeat the values that Azion gives a new Workload. Declared in the file, they keep later plans free of differences. depends_oncreates the deployment last, after the rule that runs the Function exists.
Check the configuration:
5. Create the resources
Create the six resources:
Terraform prints the plan and asks for confirmation:
Enter yes. Terraform creates the resources in dependency order and prints the domain of the Workload:
Your domain has the same format, with different letters. Compare the configuration with what Azion now holds:
Terraform tracks the six resources in its state, and the account holds the Function, the Application, and the Workload.
6. Request the Application
Store the domain in a shell variable:
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 Workload sends each request to the Application, whose rule runs the Function.
7. Change the Function
In index.js, change version: 1 to version: 2:
Preview the change:
The plan shows the changed line of code in the Function:
The plan also lists the function instance and the rule, because the provider cannot read the Function’s ID until the apply ends. Their values stay the same.
Apply the change, and enter yes at the prompt:
The apply changes only the Function:
Request the domain again:
The new code takes less time to propagate than a new Workload. While it propagates, the domain can answer with version 1 or, for a few seconds, with the 404 page. Repeat the request until the response carries version 2:
The Function runs the new code, and the domain and the other resources did not change.
8. (Optional) Destroy the resources
The Function keeps serving your domain after the tutorial. If you want to remove every resource that the configuration created, run:
Terraform lists the six resources and asks for confirmation:
Enter yes. Terraform removes the resources in the reverse order of their dependencies:
Check that the state is empty:
The command prints nothing, because Terraform no longer tracks any resource. The removal also propagates, so the domain can keep answering for a few minutes.