Amazon CloudFront enables you to securely deliver static and dynamic content with fast transfer speeds and minimal latency. With CloudFront Functions, you can customize millions of requests per second by performing latency-sensitive actions such as modifying headers, rewriting URLs, normalizing cache keys, and authorizing requests. We are excited to introduce CloudFront KeyValueStore, a global, secure, and low-latency key-value datastore that allows read access from within CloudFront Functions. This enables advanced customization at the CloudFront edge locations.
Previously, you had to embed configuration data within the function code, which meant that even small changes in configuration required code redeployment. This approach introduced the risk of unintentional code changes and had a maximum function size limitation of 10 KB, making it challenging to include all necessary data within the code.
With CloudFront KeyValueStore, you can now update the data associated with a function independently from the function code. This simplifies the code and allows for easy data updates without the need for code deployment. Here’s how it works:
1. Creating a CloudFront key-value store:
– Go to the CloudFront console and select “Functions” from the navigation pane.
– In the KeyValueStores tab, click on “Create KeyValueStore”.
– Optionally, you can import key-value pairs from a JSON file in an Amazon S3 bucket.
– Enter a name and description for the key-value store and complete the creation process.
– Once created, click on “Edit” in the Key value pairs section and then “Add pair”.
– Enter a key and its corresponding value, and save the changes.
2. Using CloudFront KeyValueStore from CloudFront Functions:
– In the CloudFront console, select “Functions” from the navigation pane and click on “Create function”.
– Provide a name for the function, select the cloudfront-js-2.0 runtime, and complete the creation process.
– Associate the key-value store with the function using the provided option.
– Copy the key-value store ID from the console and use it in the function code.
– Save the changes and publish the function.
– In the Publish tab, associate the function with a CloudFront distribution.
– Choose the Viewer Request event type and Default (*) cache behavior to intercept all requests to the distribution.
– Wait for the function to be deployed.
– Use curl or similar tools to test the function by making requests to the CloudFront distribution.
By separating configuration data from code, you gain flexibility in managing and updating your functions. CloudFront KeyValueStore is available in all global edge locations, and you only pay for the read/write operations from the public API and read operations from within CloudFront Functions. The maximum size of a key-value store is 5 MB, and you can associate a single key-value store with each function. Importing key-value data during creation is supported and can be done using a JSON structure in an Amazon S3 source file. AWS CloudFormation support for CloudFront KeyValueStore is coming soon.
Source link