Slide 8
Slide 8 text
© 2024, Amazon Web Services, Inc. or its affiliates.
CDK TypeScript source
const keyValueStore = new
cdk.aws_cloudfront.KeyValueStore(this,
'KeyValueStore', {
source:
cdk.aws_cloudfront.ImportSource.fromAsset('src/re
directconfig.json'),
});
// CloudFront Functions を読み込んで、 KeyValueStore
Id をリプレースする
const functionCode = readFileSync("src/viewer-
request.js", "utf8").replace(
"KEY_VALUE_STORE_ID_PLACEHOLDER",
keyValueStore.keyValueStoreId,
);
…
8
const viewerRequestFunction = new
cdk.aws_cloudfront.Function(this, "ViewerRequestFunction", {
code: cdk.aws_cloudfront.FunctionCode.fromInline(functionCode),
runtime: cdk.aws_cloudfront.FunctionRuntime.JS_2_0,
keyValueStore: keyValueStore,
});
const distribution = new cdk.aws_cloudfront.Distribution(this,
"Distribution", {
defaultBehavior: {
origin: new
cdk.aws_cloudfront_origins.HttpOrigin("never.referenced"),
functionAssociations: [
{
function: viewerRequestFunction,
eventType:
cdk.aws_cloudfront.FunctionEventType.VIEWER_REQUEST,
},
],
},
});
new cdk.CfnOutput(this, 'DistributionURL', { value:
distribution.distributionDomainName });