aURI Functions ビューワーリクエストイベントの KeyValueStore 設定に基づいてリクエスト CloudFront を書き換える - AWS SDKコードの例

Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

aURI Functions ビューワーリクエストイベントの KeyValueStore 設定に基づいてリクエスト CloudFront を書き換える

次のコード例は、URI Functions ビューワーリクエストイベントの KeyValueStore 設定に基づいてリクエスト CloudFront を書き換える方法を示しています。

JavaScript
JavaScript Functions の CloudFront ランタイム 2.0
注記

GitHub には他にもあります。完全な例を見つけて、CloudFront Functions の例リポジトリでセットアップして実行する方法について説明します。

import cf from 'cloudfront'; // (Optional) Replace KVS_ID with actual KVS ID const kvsId = "KVS_ID"; // enable stickiness by setting a cookie from origin or using another edge function const stickinessCookieName = "appversion"; // set to true to enable console logging const loggingEnabled = false; // function rewrites the request uri based on configuration in KVS // example config in KVS in key:value format // "latest": {"a_weightage": .8, "a_url": "v1", "b_url": "v2"} // given above key and value in KVS the request uri will be rewritten // for example http(s)://domain/latest/something/else will be rewritten as http(s)://domain/v1/something/else or http(s)://domain/v2/something/else depending on weightage // if no configuration is found, then the request is returned as is async function handler(event) { // NOTE: This example function is for a viewer request event trigger. // Choose viewer request for event trigger when you associate this function with a distribution. const request = event.request; const pathSegments = request.uri.split('/'); const key = pathSegments[1]; // if empty path segment or if there is valid stickiness cookie // then skip call to KVS and let the request continue. if (!key || hasValidSticknessCookie(request.cookies[stickinessCookieName], key)) { return event.request; } try { // get the prefix replacement from KVS const replacement = await getPathPrefixByWeightage(key); if (!replacement) { return event.request; } //Replace the first path with the replacement pathSegments[1] = replacement; log(`using prefix ${pathSegments[1]}`) const newUri = pathSegments.join('/'); log(`${request.uri} -> ${newUri}`); request.uri = newUri; return request; } catch (err) { // No change to the path if the key is not found or any other error log(`request uri: ${request.uri}, error: ${err}`); } // no change to path - return request return event.request; } // function to get the prefix from KVS async function getPathPrefixByWeightage(key) { const kvsHandle = cf.kvs(kvsId); // get the weightage config from KVS const kvsResponse = await kvsHandle.get(key); const weightageConfig = JSON.parse(kvsResponse); // no configuration - return null if (!weightageConfig || !isFinite(weightageConfig.a_weightage)) { return null; } // return the url based on weightage // return null if no url is configured if (Math.random() <= weightageConfig.a_weightage) { return weightageConfig.a_url ? weightageConfig.a_url: null; } else { return weightageConfig.b_url ? weightageConfig.b_url : null; } } // function to check if the stickiness cookie is valid function hasValidSticknessCookie(stickinessCookie, pathSegment) { // if the value exists and it matches pathSegment return (stickinessCookie && stickinessCookie.value === pathSegment) } function log(message) { if (loggingEnabled) { console.log(message); } }