Helper methods for key value stores (original) (raw)

This section applies if you use the CloudFront Key Value Store to include key values in the function that you create. CloudFront Functions has a module that provides three helper methods to read values from the key value store.

To use this module in the function code, make sure that you have associated a key value store with the function.

Next, include the following statements in the first lines of the function code:

import cf from 'cloudfront';
const kvsHandle = cf.kvs();

get() method

Use this method to return the key value for the key name that you specify.

Request

get("key", options);

Request example

const value = await kvsHandle.get("myFunctionKey", { format: "string"});

Response

The response is a promise that resolves to a value in the format requested by using options. By default, the value is returned as a string.

Error handling

The get() method will return an error when the key that you requested doesn't exist in the associated key value store. To manage this use case, you can add a try and catch block to your code.

exists() method

Use this method to identify whether or not the key exists in the key value store.

Request

exists("key");

Request example

const exist = await kvsHandle.exists("myFunctionkey");

Response

The response is a promise that returns a Boolean (true or false). This value specifies whether or not the key exists in the key value store.

Use this method to return metadata about the key value store.

Request

meta();

Request example

const meta = await kvsHandle.meta();

Response

The response is a promise that resolves to an object with the following properties:

Response example

{keyCount:3,creationDateTime:2023-11-30T23:07:55.765Z,lastUpdatedDateTime:2023-12-15T03:57:52.411Z}