Tuesday, December 3, 2024

What is Amazon CloudFront KeyValueStore Optimization

- Advertisement -

You may safely distribute both static and dynamic content with fast transfer speeds and minimal latency by using Amazon CloudFront. You can handle millions of requests per second and latency-sensitive customizations with CloudFront Functions. CloudFront Functions, for example, can be used to rewrite URLs, authorize requests, normalize cache keys, and change headers.

AWS are pleased to present CloudFront KeyValueStore, a safe, low-latency global key value datastore that can be accessed readly from within CloudFront Functions. This feature enables highly customized logic to be implemented at CloudFront edge locations.

- Advertisement -

In the past, configuration data had to be included directly into the function code. For instance, information for choosing which URL to send the viewer to when a URL needs to be redirected. Every tiny change in configuration when embedding it with the function code necessitates a code change and a redeployment of the function code. There is a chance that code will be accidentally altered when new lookup additions need updating and deploying code. Additionally, since the maximum function size is 10 KB, many use cases will struggle to fit all of the data within the code.

You can now update the function code and the data associated with it separately using CloudFront KeyValueStore. As a result, function code is made simpler and data updates are made simple without requiring code modifications to be deployed.

Let’s examine how this functions in real life.

Building a key value store for CloudFront

You select Functions from the navigation pane in the CloudFront dashboard. Then now you select Create KeyValueStore under the KeyValueStores menu.

- Advertisement -

This allows you to import key-value pairs into an Amazon Simple Storage Service (Amazon S3) bucket from a JSON file. If you want to start with no keys, therefore you not doing that right now. You finish the key value store creation by entering a name and description.

You select Edit under the Key value pairs area and then Add pair once the key value store has been established. You enter Hello World for the value and hello for the key before saving the adjustments. For now, one key is sufficient, but you can add more keys and values.

Changes made to a key value store propagate quickly to all CloudFront edge locations, allowing functions connected with the key value store to use it with little latency.

Utilizing CloudFront Functions’ CloudFront KeyValueStore

You select Functions from the navigation pane in the CloudFront console, followed by Create function. You give the function a name, choose the cloudfront-js-2.0 runtime, and finish the function’s creation. Then you correlate this function with the key value store using the newly available option.

You can use the console’s key value store ID, which you copy, in the function code that follows:

This function answers with the name of the key and its value, using the first segment of the request path as the key.

Now you publish the function and save the modifications. You can link the function to a CloudFront distribution which you previously made in the Publish tab of the function. You can intercept all requests to the distribution using the Default (*) cache action and the Viewer Request event type.

You return to the functions list in the console and watch for the function to be deployed. Next, you download content from the distribution using curl from the command line and verify the function’s outcome.

Initially, you can test a few pathways that call the method and checkup the previous key you made (hello): Success! Next, you experiment with a different approach to observe that, in the event that the key cannot be retrieved, the code returns the default value.

Now that we have this basic example working, let’s try a more sophisticated and practical one.

Using CloudFront KeyValueStore configuration data, rewrite the URL

Let’s create a function that looks up the custom path that CloudFront should use to send the actual request in a key-value store using the content of the URL in the HTTP request. This feature can assist in managing the various services that make up a website.

Things to consider

Today, CloudFront KeyValueStore is accessible in every edge location across the world. Pay just for the read/write operations from the public API and the read operations from within CloudFront Functions when using CloudFront KeyValueStore. View the CloudFront pricing page for additional details.

The AWS Management Console, AWS Command Line Interface (AWS CLI), and AWS SDKs can all be used to manage a key value store. Support for AWS CloudFormation is on the horizon. You can link a single key value store to every function, and key value stores have a maximum capacity of 5 MB. A key can have a maximum size of 512 bytes. Values may have a maximum value of 1KB. Using a source file on Amazon S3, you can import key/value data while building a key-value store. This file has the following JSON structure:

Key/value data imports during creation provide easy configuration replication between environments (e.g., preproduction and production) and can automate the setup of a new environment (e.g., test or dev).

- Advertisement -
RELATED ARTICLES

Recent Posts

Popular Post

Govindhtech.com Would you like to receive notifications on latest updates? No Yes