Tutorial: Creating a function that includes key values - Amazon CloudFront
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Tutorial: Creating a function that includes key values

This tutorial shows you how to include key values with CloudFront function. Key values are part of a key value pair. You include the name (from the key value pair) in the function code. When the function runs, CloudFront replaces the name with the value.

key value pairs are variables that are stored in a key value store. When you use a key in your function (instead of hard-coded values), your function is more flexible. You can change the value of the key without having to deploy code changes. Key value pairs can also reduce the size of your function. For more information about key value pairs and key value stores, see Amazon CloudFront KeyValueStore.

Prerequisites

We assume that you are familiar with CloudFront Functions. If you are new to both functions and the key value store, you should first follow the tutorial in Tutorial: Creating a simple function with CloudFront Functions.

Set up the key value store

Step 1: Create the key value store

  1. Plan the key value pairs you want to include in the function. Make a note of key names.

    Keep in mind that all the key value pairs you want to use in a function must be in a single key value store.

  2. Decide about the order of work. There are two ways to proceed:

    • Create a key value store, and add key value pairs to the store. Then create (or modify) the function and incorporate the key names.

    • Or, create (or modify) the function and incorporate the key names you want to use. Then create a key value store, and add the key value pairs.

    This tutorial assumes that you are extending the function from the functions tutorial. It also assumes that you want to create the key value store first.

  3. Sign in to the Amazon Web Services Management Console and open the CloudFront console at https://console.amazonaws.cn/cloudfront/v4/home.

  4. In the left navigation bar, choose Functions. On the Functions page, choose the KeyValueStores tab.

  5. Choose Create KeyValueStore and complete the fields as follows:

    • Enter a name and optional description for the store.

    • Leave S3 URI blank because this tutorial demonstrates how to enter the key value pairs manually.

  6. Choose the Create button. The details page for the new key value store appears. This page includes a Key value pairs section that is currently empty.

Step 2: Add key value pairs to the store

  1. In the Key value pairs section, choose the Add key value pairs button. Choose Add pair and enter a name and value.

  2. Choose the Add pair button to add another pair.

  3. When you have finished, choose Save changes to save all the pairs in the store. On the confirmation dialog that appears, choose Done.

You now have a store that contains a group of key value pairs.

Set up in the function

Step 3: Associate the key value store with the function

You have now created the key value store. And you have created or modified a function that includes the key names from the key value store. You can now associate the key value store and the function. You create that association from within the function.

  1. In the left navigation bar, choose Functions. The Functions tab appears on top, by default.

  2. In the Associated KeyValueStore section, choose Associate Existing KeyValueStore. Select the key value store and choose the Associate KeyValueStore button. Note that you can associate only one key value store with each function.

Step 4: Test and publish the function code

  1. You should always test the function code every time you modify it, including when you do the following:

    • Associate a key value store with the function.

    • Modify the function and its key value store to include a new key value pair.

    • Change the value of a key value pair.

    For information about how to test a function, see Testing functions. Make sure that you choose to test the function in the DEVELOPMENT stage.

  2. Publish the function when you are ready to use the function (with the new or revised key value pairs) in a LIVE environment.

    When you publish, CloudFront copies the version of the function from the DEVELOPMENT stage over to the live stage. The function has the new code and is associated with the key value store. (There is no need to perform the association again, in the live stage.)

    For information about how to publish the function, see Publishing functions.