From 2076e53b113da5bf8feca09a048c04a93dfa432e Mon Sep 17 00:00:00 2001 From: Kyle Laker Date: Wed, 24 Jan 2024 18:57:51 -0500 Subject: [PATCH] feat(cloudfront): associate key value stores to functions (#28571) CloudFormation now supports specifying [`KeyValueStoreAssocations`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-function-keyvaluestoreassociation.html) on a `AWS::CloudFront::Function`. This adds that support to the CDK. Even though the field is plural (in CloudFormation and the API), [a function can have one value store](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-associate.html) so the implementation here tries to provide a nicer API around that. If a Key Value Store is specified, the function will default to `js-2.0` instead of `js-1.0`. Behavior for existing functions is retained. Additionally, an error is thrown if 1.0 is used. Closes #28377. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- ...efaultTestDeployAssert986D766D.assets.json | 19 ++ ...aultTestDeployAssert986D766D.template.json | 36 +++ ...0affe8fd26234f67dcf2e93db5142bc2991a1.json | 12 + .../cdk.out | 1 + ...on-key-value-store-association.assets.json | 32 +++ ...-key-value-store-association.template.json | 86 +++++++ .../integ.json | 12 + .../manifest.json | 119 ++++++++++ .../tree.json | 211 ++++++++++++++++++ ...eg.function-key-value-store-association.ts | 23 ++ packages/aws-cdk-lib/aws-cloudfront/README.md | 13 ++ .../aws-cloudfront/lib/function.ts | 23 +- .../aws-cloudfront/test/function.test.ts | 72 +++++- 13 files changed, 655 insertions(+), 4 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/asset.9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets.json new file mode 100644 index 0000000000000..3720ef61a8e2c --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/asset.9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/asset.9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json new file mode 100644 index 0000000000000..c5999fd9d8723 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/asset.9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json @@ -0,0 +1,12 @@ +{ + "data":[ + { + "key":"key1", + "value":"value" + }, + { + "key":"key2", + "value":"value" + } + ] +} diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.assets.json new file mode 100644 index 0000000000000..78f78472e1e24 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.assets.json @@ -0,0 +1,32 @@ +{ + "version": "36.0.0", + "files": { + "9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1": { + "source": { + "path": "asset.9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + }, + "5ab0a11edcee54ef5956720528876fb90b8a0b2b8e51c40c5bdca486bf681ba1": { + "source": { + "path": "integ-function-key-value-store-association.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "5ab0a11edcee54ef5956720528876fb90b8a0b2b8e51c40c5bdca486bf681ba1.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.template.json new file mode 100644 index 0000000000000..7c57353b9fefb --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ-function-key-value-store-association.template.json @@ -0,0 +1,86 @@ +{ + "Resources": { + "TestKeyValueStore8D0C09A2": { + "Type": "AWS::CloudFront::KeyValueStore", + "Properties": { + "Comment": "A test Key Value Store for CloudFront", + "ImportSource": { + "SourceArn": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":s3:::", + { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "/9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json" + ] + ] + }, + "SourceType": "S3" + }, + "Name": "integfunctionkeyvaluestoreassociationTestKeyValueStoreA8304A36" + } + }, + "TestFunction22AD90FC": { + "Type": "AWS::CloudFront::Function", + "Properties": { + "AutoPublish": true, + "FunctionCode": "code", + "FunctionConfig": { + "Comment": "TestKvFunction", + "KeyValueStoreAssociations": [ + { + "KeyValueStoreARN": { + "Fn::GetAtt": [ + "TestKeyValueStore8D0C09A2", + "Arn" + ] + } + } + ], + "Runtime": "cloudfront-js-2.0" + }, + "Name": "TestKvFunction" + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ.json new file mode 100644 index 0000000000000..156ca2a1a40d2 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "FunctionKeyValueStoreAssociation/DefaultTest": { + "stacks": [ + "integ-function-key-value-store-association" + ], + "assertionStack": "FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert", + "assertionStackName": "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/manifest.json new file mode 100644 index 0000000000000..2a4dd6b9a9353 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/manifest.json @@ -0,0 +1,119 @@ +{ + "version": "36.0.0", + "artifacts": { + "integ-function-key-value-store-association.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-function-key-value-store-association.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-function-key-value-store-association": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "integ-function-key-value-store-association.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/5ab0a11edcee54ef5956720528876fb90b8a0b2b8e51c40c5bdca486bf681ba1.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-function-key-value-store-association.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "integ-function-key-value-store-association.assets" + ], + "metadata": { + "/integ-function-key-value-store-association/TestKeyValueStore/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TestKeyValueStore8D0C09A2" + } + ], + "/integ-function-key-value-store-association/TestFunction/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TestFunction22AD90FC" + } + ], + "/integ-function-key-value-store-association/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-function-key-value-store-association/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "integ-function-key-value-store-association" + }, + "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "FunctionKeyValueStoreAssociationDefaultTestDeployAssert986D766D.assets" + ], + "metadata": { + "/FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/tree.json new file mode 100644 index 0000000000000..dd3459c6f09c3 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.js.snapshot/tree.json @@ -0,0 +1,211 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "integ-function-key-value-store-association": { + "id": "integ-function-key-value-store-association", + "path": "integ-function-key-value-store-association", + "children": { + "TestKeyValueStore": { + "id": "TestKeyValueStore", + "path": "integ-function-key-value-store-association/TestKeyValueStore", + "children": { + "ImportSource": { + "id": "ImportSource", + "path": "integ-function-key-value-store-association/TestKeyValueStore/ImportSource", + "children": { + "Stage": { + "id": "Stage", + "path": "integ-function-key-value-store-association/TestKeyValueStore/ImportSource/Stage", + "constructInfo": { + "fqn": "aws-cdk-lib.AssetStaging", + "version": "0.0.0" + } + }, + "AssetBucket": { + "id": "AssetBucket", + "path": "integ-function-key-value-store-association/TestKeyValueStore/ImportSource/AssetBucket", + "constructInfo": { + "fqn": "aws-cdk-lib.aws_s3.BucketBase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_s3_assets.Asset", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-function-key-value-store-association/TestKeyValueStore/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::KeyValueStore", + "aws:cdk:cloudformation:props": { + "comment": "A test Key Value Store for CloudFront", + "importSource": { + "sourceType": "S3", + "sourceArn": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":s3:::", + { + "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" + }, + "/9c0f502203ec9d802046021b8a20affe8fd26234f67dcf2e93db5142bc2991a1.json" + ] + ] + } + }, + "name": "integfunctionkeyvaluestoreassociationTestKeyValueStoreA8304A36" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnKeyValueStore", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.KeyValueStore", + "version": "0.0.0" + } + }, + "TestFunction": { + "id": "TestFunction", + "path": "integ-function-key-value-store-association/TestFunction", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-function-key-value-store-association/TestFunction/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Function", + "aws:cdk:cloudformation:props": { + "autoPublish": true, + "functionCode": "code", + "functionConfig": { + "comment": "TestKvFunction", + "runtime": "cloudfront-js-2.0", + "keyValueStoreAssociations": [ + { + "keyValueStoreArn": { + "Fn::GetAtt": [ + "TestKeyValueStore8D0C09A2", + "Arn" + ] + } + } + ] + }, + "name": "TestKvFunction" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.Function", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "integ-function-key-value-store-association/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "integ-function-key-value-store-association/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "FunctionKeyValueStoreAssociation": { + "id": "FunctionKeyValueStoreAssociation", + "path": "FunctionKeyValueStoreAssociation", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "FunctionKeyValueStoreAssociation/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "FunctionKeyValueStoreAssociation/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "FunctionKeyValueStoreAssociation/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.ts new file mode 100644 index 0000000000000..b11a3cc47c131 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.function-key-value-store-association.ts @@ -0,0 +1,23 @@ +import * as path from 'node:path'; +import * as cdk from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'integ-function-key-value-store-association'); + +const store = new cloudfront.KeyValueStore(stack, 'TestKeyValueStore', { + comment: 'A test Key Value Store for CloudFront', + source: cloudfront.ImportSource.fromAsset(path.join(__dirname, 'test-import-source.json')), +}); +new cloudfront.Function(stack, 'TestFunction', { + functionName: 'TestKvFunction', + code: cloudfront.FunctionCode.fromInline('code'), + keyValueStore: store, +}); + +new IntegTest(app, 'FunctionKeyValueStoreAssociation', { + testCases: [stack], +}); + +app.synth(); diff --git a/packages/aws-cdk-lib/aws-cloudfront/README.md b/packages/aws-cdk-lib/aws-cloudfront/README.md index 333367b4ec176..6c9aa2d3a461c 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/README.md +++ b/packages/aws-cdk-lib/aws-cloudfront/README.md @@ -558,6 +558,19 @@ const store = new cloudfront.KeyValueStore(this, 'KeyValueStore', { }); ``` +The Key Value Store can then be associated to a function using the `cloudfront-js-2.0` runtime +or newer: + +```ts +const store = new cloudfront.KeyValueStore(this, 'KeyValueStore'); +new cloudfront.Function(this, 'Function', { + code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'), + // Note that JS_2_0 must be used for Key Value Store support + runtime: cloudfront.FunctionRuntime.JS_2_0, + keyValueStore: store, +}); +``` + ### Logging You can configure CloudFront to create log files that contain detailed information about every user request that CloudFront receives. diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts index 71b874adb1934..bdf72350b689e 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts @@ -1,6 +1,7 @@ import * as fs from 'fs'; import { Construct } from 'constructs'; import { CfnFunction } from './cloudfront.generated'; +import { IKeyValueStore } from './key-value-store'; import { IResource, Names, Resource, Stack } from '../../core'; /** @@ -132,9 +133,19 @@ export interface FunctionProps { /** * The runtime environment for the function. - * @default FunctionRuntime.JS_1_0 + * @default FunctionRuntime.JS_1_0 (unless `keyValueStore` is specified, then `FunctionRuntime.JS_2_0`) */ readonly runtime?: FunctionRuntime; + + /** + * The Key Value Store to associate with this function. + * + * In order to associate a Key Value Store, the `runtime` must be + * `cloudfront-js-2.0` or newer. + * + * @default - no key value store is associated + */ + readonly keyValueStore?: IKeyValueStore; } /** @@ -179,7 +190,14 @@ export class Function extends Resource implements IFunction { this.functionName = props.functionName ?? this.generateName(); - this.functionRuntime = props.runtime?.value ?? FunctionRuntime.JS_1_0.value; + const defaultFunctionRuntime = props.keyValueStore ? FunctionRuntime.JS_2_0.value : FunctionRuntime.JS_1_0.value; + this.functionRuntime = props.runtime?.value ?? defaultFunctionRuntime; + + if (props.keyValueStore && this.functionRuntime === FunctionRuntime.JS_1_0.value) { + throw new Error( + `Key Value Stores cannot be associated to functions using the ${this.functionRuntime} runtime`, + ); + } const resource = new CfnFunction(this, 'Resource', { autoPublish: true, @@ -187,6 +205,7 @@ export class Function extends Resource implements IFunction { functionConfig: { comment: props.comment ?? this.functionName, runtime: this.functionRuntime, + keyValueStoreAssociations: props.keyValueStore ? [{ keyValueStoreArn: props.keyValueStore.keyValueStoreArn }] : undefined, }, name: this.functionName, }); diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts index 142559b07ea7c..5e17a59362535 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts @@ -1,7 +1,7 @@ import * as path from 'path'; -import { Template } from '../../assertions'; +import { Template, Match } from '../../assertions'; import { App, Stack } from '../../core'; -import { Function, FunctionCode, FunctionRuntime } from '../lib'; +import { Function, FunctionCode, FunctionRuntime, KeyValueStore } from '../lib'; describe('CloudFront Function', () => { @@ -188,4 +188,72 @@ describe('CloudFront Function', () => { }, }); }); + + describe('key value store association', () => { + test('minimal example', () => { + const stack = new Stack(); + const keyValueStore = new KeyValueStore(stack, 'TestStore'); + + new Function(stack, 'TestFn', { + code: FunctionCode.fromInline('code'), + keyValueStore, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', { + FunctionConfig: { + Runtime: 'cloudfront-js-2.0', + KeyValueStoreAssociations: [{ + KeyValueStoreARN: stack.resolve(keyValueStore.keyValueStoreArn), + }], + }, + }); + }); + + test('rejects key value store with v1.0 runtime', () => { + const stack = new Stack(); + const keyValueStore = new KeyValueStore(stack, 'TestStore'); + + expect(() => new Function(stack, 'TestFn', { + code: FunctionCode.fromInline('code'), + runtime: FunctionRuntime.JS_1_0, + keyValueStore, + })).toThrow(/Key Value Stores cannot be associated to functions using the .* runtime/); + }); + + test('works with js-2.0 runtime specified', () => { + const stack = new Stack(); + const keyValueStore = new KeyValueStore(stack, 'TestStore'); + + new Function(stack, 'TestFn', { + code: FunctionCode.fromInline('code'), + runtime: FunctionRuntime.JS_2_0, + keyValueStore, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', { + FunctionConfig: { + Runtime: 'cloudfront-js-2.0', + KeyValueStoreAssociations: [{ + KeyValueStoreARN: stack.resolve(keyValueStore.keyValueStoreArn), + }], + }, + }); + }); + + test('no value is used in CloudFormation when unspecified in CDK', () => { + const stack = new Stack(); + + new Function(stack, 'TestFn', { + code: FunctionCode.fromInline('code'), + runtime: FunctionRuntime.JS_2_0, + keyValueStore: undefined, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', { + FunctionConfig: { + KeyValueStoreAssociations: Match.absent(), + }, + }); + }); + }); });