Creating functions - 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).

Creating functions

You create a function in two stages. First you create the function code as Java Script, outside of CloudFront. Then you use CloudFront to create the function and include the code. The code exists inside the function (not as a reference).

The new function is added to the DEVELOPMENT stage. You must publish the function to copy it over to the LIVE stage (Published in the console).

Console
To create a function (console)
  1. Sign in to the Amazon Web Services Management Console and open the Functions page in the CloudFront console at https://console.amazonaws.cn/cloudfront/v4/home#/functions.

  2. Choose Create function.

  3. Enter a function name that is unique within the Amazon account, then choose the Java Script version, and then choose Continue. The function now exists. The details page for the new function appears.

    Note

    If you want to use key value pairs in the function, you must choose Java Script 2.0.

  4. In the Function code section, select the Build tab and enter your function code. The sample code that is included in the Build tab illustrates the basic syntax for the function code. You can complete the code as follows:

    • Use the default function just so you can get started.

    • Replace it with code that you copy from example code on GitHub.

    • Replace it with your own code.

    For more information about writing function code, see the following:

  5. Choose Save changes as often as you want, to save the function code.

  6. If the function code uses key value pairs, you must associate a key value store.

    You can associate the key value store during the initial creation of the function. Or you can associate it later, by updating the function.

    To associate a key value store now, follow these steps:

    • Go to the Associate KeyValueStore section and choose Associate existing KeyValueStore.

    • Select the key value store that contains the key value pairs in the function, then choose Associate KeyValueStore.

    CloudFront immediately associates the store with the function. You don't need to save the function.

CLI

If you use the CLI, you typically first create the function code in a file, and then create the function with the Amazon CLI.

  1. Create the function code in a file, and store it in a directory that your computer can connect to. For more information about writing function code, see the following:

  2. Run the command as shown in the example. This example uses the fileb:// notation to pass in the file. It also includes line breaks to make the command more readable.

aws cloudfront create-function \ --name MaxAge \ --function-config '{"Comment":"Max Age 2 years","Runtime":"cloudfront-js-2.0","KeyValueStoreAssociations":{"Quantity":1,"Items":[{"KeyValueStoreARN":"arn:aws:cloudfront::111122223333:key-value-store/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"}]}}' \ --function-code fileb://function-max-age-v1.js

Notes:

  • Runtime: The version of Java Script. If you want to use key value pairs in the function, you must specify version 2.0.

  • KeyValueStoreAssociations: If your function uses key value pairs, you can associate the key value store during the initial creation of the function. Or you can associate it later, by using update-function. The Quantity is always 1 because each function can have only one key value store associated with it.

When the command is successful, you see output like the following.

ETag: ETVABCEXAMPLE FunctionSummary: FunctionConfig: Comment: Max Age 2 years Runtime: cloudfront-js-2.0 KeyValueStoreAssociations= \ {Quantity=1, \ Items=[{KeyValueStoreARN='arn:aws:cloudfront::111122223333:key-value-store/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111'}]} \ FunctionMetadata: CreatedTime: '2021-04-18T20:38:56.915000+00:00' FunctionARN: arn:aws:cloudfront::111122223333:function/MaxAge LastModifiedTime: '2023-11-19T20:38:56.915000+00:00' Stage: DEVELOPMENT Name: MaxAge Status: UNPUBLISHED Location: https://cloudfront.amazonaws.com/2020-05-31/function/arn:aws:cloudfront:::function/MaxAge

Most of the information is repeated from the request. Other information is added by CloudFront.

Notes
  • ETag: This value changes each time you modify the key value store. You use this value and the function name to reference the function in the future. Make sure that you always use the current ETag.

  • FunctionARN

  • Stage

  • 111122223333

  • Status