KV Store quickstart
Create your first namespace with the Azion API, then store and read a key from a function running on an application.
This guide instructs you through storing and reading your first key in KV Store. By the end you will have:
- Your first namespace, created through the Azion API.
- A function that opens the namespace, writes one key, and reads it back.
- A function instance and a Rules Engine rule that run that function.
- A URI on your own application that answers with the stored value.
The namespace is the container KV Store keeps keys and values in, and it is permanent: it cannot be renamed, emptied, or deleted afterwards, so the name is worth choosing before the first request. The function holds the code that opens the namespace and calls the client. The function instance binds the function to an application, and the Rules Engine rule selects that instance and sets which requests run it. Creating a function does not execute it, and an instance without a rule never runs.
Select the interface you will use. The prerequisites and every stage below follow that choice.
Prerequisites
- An Azion account. To create one, refer to How to create an account on Azion.
- KV Store Preview access on the account. The product is in Preview, and access is requested through the technical support team. To request it, refer to Technical Support.
- The Edit Functions and Edit Applications permissions on the account. They grant permission to create the function and to change the application that runs it. For more information, refer to Teams Permissions.
- An application. To create one, refer to Applications quickstart.
- A personal token and
curl. You create the namespace through the Azion API whichever interface you pick, and the request sends the token in theAuthorizationheader.
- Access to Azion Console. To sign in, refer to Access Azion Console.
Create a namespace
A namespace is created through the Azion API, because KV Store has no screen in Azion Console and no Azion CLI command. Every reader follows this one procedure, whichever interface the later stages use.
The name runs 3 to 63 characters, and uses letters, numbers, the hyphen, and the underscore. Names are case-sensitive, and nothing removes a namespace once it exists, so a name created in the wrong case is permanent.
Replace [TOKEN VALUE] with your personal token, and my-namespace with a name of your own:
A 201 carries the whole namespace:
A 400 with the message Namespace already exists means the account already holds that name. Choose a different name and send the request again.
The request is synchronous, and there is no provisioning state to poll. The name the response carries is the handle every later call uses, including the function you write next. For every field, operation, and error on this endpoint, refer to Namespaces.
Create the function
Azion.KV is a global of Azion Runtime, so a function reaches it with no import line and no credential. The function below opens the namespace, writes one key, reads it back, and returns the value.
To create the function in Azion Console:
Access Azion Console > Products Menu > Libraries > Functions.
Enter a name for the function. For example: kv-quickstart.
In the Code tab, paste the following code, with the name of the namespace you created:
The function is saved and holds the code that reaches the namespace.
The function does not run yet: the next stage binds it to an application and adds the rule that triggers it. For every method the client carries, refer to KV client.
Instantiate the function and add the rule
An instance binds the function to an application, and a rule decides which requests run the instance. This rule runs it on requests to /kv-quickstart.
To set both up in Azion Console:
Still in Azion Console, go to Products Menu > Build > Applications, then select your application.
Enter a name for the instance. For example: kv-quickstart instance.
Enter a name for the rule. For example: Run kv-quickstart.
In the Criteria section, select the ${uri} variable and the is equal operator, and enter /kv-quickstart as the argument.
The rule runs the instance on every request whose URI is /kv-quickstart.
New rules can take a few minutes to propagate. Wait before you request the URI. For each instance and rule field this guide passes over, refer to Functions quickstart.
Request the URI
Your application answers on its own domain. To reach the function, request the URI the rule matches:
A successful response carries the body the function returns, which is the value it stored under user-42 and read back: the string active. A response carrying No value for that key means the read returned null, so the key was not found under that name.
Your first namespace holds one key, and a function writes and reads it on every request the rule matches.