Create functions - Amazon CloudFront (original) (raw)

You create a function in two stages:

  1. Create the function code as JavaScript. You can use the default example from the CloudFront console or write your own. For more information, see the following topics:
  2. Use CloudFront to create the function and include your code. The code exists inside the function (not as a reference).

Console

To create a function
  1. Sign in to the CloudFront console at https://console.aws.amazon.com/cloudfront/v4/home#/functions and choose theFunctions page.
  2. Choose Create function.
  3. Enter a function name that is unique within the AWS account, choose the JavaScript version, and then choose Continue. The details page for the new function appears.
Note

To use key-value pairs in the function, you must choose JavaScript runtime 2.0. 4. In the Function code section, choose theBuild tab and enter your function code. The sample code that is included in the Build tab illustrates the basic syntax for the function code. 5. Choose Save changes. 6. If the function code uses key-value pairs, you must associate a key value store.
You can associate the key value store when you first create the function. Or, you can associate it later, by updating the function.
To associate a key value store now, follow these steps:

CLI

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

To create a function
  1. Create the function code in a file, and store it in a directory that your computer can connect to.
  2. Run the command as shown in the example. This example uses thefileb:// 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
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

After you create the function, it's added to the DEVELOPMENT stage. We recommend that you test your function before you publish it. After you publish your function, the function changes to the LIVE stage.

Use async and await

Test functions

Did this page help you? - Yes

Thanks for letting us know we're doing a good job!

If you've got a moment, please tell us what we did right so we can do more of it.

Did this page help you? - No

Thanks for letting us know this page needs work. We're sorry we let you down.

If you've got a moment, please tell us how we can make the documentation better.