From 594f524ec88affb464d62c6c00855dc96d9f127e Mon Sep 17 00:00:00 2001 From: watany <76135106+watany-dev@users.noreply.github.com> Date: Fri, 29 Dec 2023 13:34:43 +0900 Subject: [PATCH] feat(cloudfront): CloudFront Function runtime property (#28099) fixed #28163 This provides an option to change the runtime for CloudFront Functions. This makes it easier to access the CloudFront KeyValueStore. https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-function-functionconfig.html ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .pre-commit-config.yaml | 5 + ...efaultTestDeployAssertB25F4037.assets.json | 19 ++ ...aultTestDeployAssertB25F4037.template.json | 36 +++ .../cdk.out | 1 + .../integ-distribution-function.assets.json | 20 ++ .../integ-distribution-function.template.json | 141 ++++++++++ .../integ.json | 12 + .../manifest.json | 149 ++++++++++ .../tree.json | 265 ++++++++++++++++++ .../integ.distribution-function-runtime.ts | 43 +++ packages/aws-cdk-lib/aws-cloudfront/README.md | 1 + .../aws-cloudfront/lib/function.ts | 49 +++- .../aws-cloudfront/test/function.test.ts | 58 +++- packages/aws-cdk-lib/awslint.json | 1 + 14 files changed, 798 insertions(+), 2 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.ts diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000..aa94c88eb6de9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,5 @@ +repos: + - repo: https://github.com/gitleaks/gitleaks + rev: v8.17.0 + hooks: + - id: gitleaks diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.assets.json new file mode 100644 index 0000000000000..7b81b6fbbc089 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.assets.json @@ -0,0 +1,19 @@ +{ + "version": "35.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "CF2RuntimeDefaultTestDeployAssertB25F4037.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.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/CF2RuntimeDefaultTestDeployAssertB25F4037.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.distribution-function-runtime.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/cdk.out new file mode 100644 index 0000000000000..c5cb2e5de6344 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"35.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.assets.json new file mode 100644 index 0000000000000..eaec73d98c45d --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.assets.json @@ -0,0 +1,20 @@ +{ + "version": "35.0.0", + "files": { + "5289568fbbfd952fddb41b828b4b0a8e4b54ec5892e158efd72cf494c0327e01": { + "source": { + "path": "integ-distribution-function.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-eu-west-1": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-eu-west-1", + "objectKey": "5289568fbbfd952fddb41b828b4b0a8e4b54ec5892e158efd72cf494c0327e01.json", + "region": "eu-west-1", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-eu-west-1" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.template.json new file mode 100644 index 0000000000000..1ea7e8f64f1fa --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.template.json @@ -0,0 +1,141 @@ +{ + "Resources": { + "FunctionRequest95528B2F": { + "Type": "AWS::CloudFront::Function", + "Properties": { + "AutoPublish": true, + "FunctionCode": "function handler(event) { return event.request }", + "FunctionConfig": { + "Comment": "eu-west-1integdistributionfunctionFunctionRequest8E65DEEB", + "Runtime": "cloudfront-js-1.0" + }, + "Name": "eu-west-1integdistributionfunctionFunctionRequest8E65DEEB" + } + }, + "FunctionResponse4EF2D1D3": { + "Type": "AWS::CloudFront::Function", + "Properties": { + "AutoPublish": true, + "FunctionCode": "function handler(event) { return event.response }", + "FunctionConfig": { + "Comment": "eu-west-1integdistributionfunctionFunctionResponseDD4BADA1", + "Runtime": "cloudfront-js-2.0" + }, + "Name": "eu-west-1integdistributionfunctionFunctionResponseDD4BADA1" + } + }, + "DistB3B78991": { + "Type": "AWS::CloudFront::Distribution", + "Properties": { + "DistributionConfig": { + "DefaultCacheBehavior": { + "CachePolicyId": "4135ea2d-6df8-44a3-9df3-4b5a84be39ad", + "Compress": true, + "FunctionAssociations": [ + { + "EventType": "viewer-request", + "FunctionARN": { + "Fn::GetAtt": [ + "FunctionRequest95528B2F", + "FunctionARN" + ] + } + }, + { + "EventType": "viewer-response", + "FunctionARN": { + "Fn::GetAtt": [ + "FunctionResponse4EF2D1D3", + "FunctionARN" + ] + } + } + ], + "TargetOriginId": "integdistributionfunctionDistOrigin1D1E9DF17", + "ViewerProtocolPolicy": "allow-all" + }, + "Enabled": true, + "HttpVersion": "http2", + "IPV6Enabled": true, + "Origins": [ + { + "CustomOriginConfig": { + "OriginProtocolPolicy": "https-only" + }, + "DomainName": "www.example.com", + "Id": "integdistributionfunctionDistOrigin1D1E9DF17" + } + ] + } + } + } + }, + "Outputs": { + "RequestFunctionArn": { + "Value": { + "Fn::GetAtt": [ + "FunctionRequest95528B2F", + "FunctionARN" + ] + } + }, + "RequestFunctionStage": { + "Value": { + "Fn::GetAtt": [ + "FunctionRequest95528B2F", + "Stage" + ] + } + }, + "ResponseFunctionArn": { + "Value": { + "Fn::GetAtt": [ + "FunctionResponse4EF2D1D3", + "FunctionARN" + ] + } + }, + "ResponseFunctionStage": { + "Value": { + "Fn::GetAtt": [ + "FunctionResponse4EF2D1D3", + "Stage" + ] + } + } + }, + "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.distribution-function-runtime.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ.json new file mode 100644 index 0000000000000..3f83a1ff45115 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "35.0.0", + "testCases": { + "CF2Runtime/DefaultTest": { + "stacks": [ + "integ-distribution-function" + ], + "assertionStack": "CF2Runtime/DefaultTest/DeployAssert", + "assertionStackName": "CF2RuntimeDefaultTestDeployAssertB25F4037" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/manifest.json new file mode 100644 index 0000000000000..a71b2eef33f91 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/manifest.json @@ -0,0 +1,149 @@ +{ + "version": "35.0.0", + "artifacts": { + "integ-distribution-function.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-distribution-function.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-distribution-function": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/eu-west-1", + "properties": { + "templateFile": "integ-distribution-function.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-eu-west-1", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-eu-west-1", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-eu-west-1/5289568fbbfd952fddb41b828b4b0a8e4b54ec5892e158efd72cf494c0327e01.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-distribution-function.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-eu-west-1", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "integ-distribution-function.assets" + ], + "metadata": { + "/integ-distribution-function/FunctionRequest/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "FunctionRequest95528B2F" + } + ], + "/integ-distribution-function/FunctionResponse/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "FunctionResponse4EF2D1D3" + } + ], + "/integ-distribution-function/Dist/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "DistB3B78991" + } + ], + "/integ-distribution-function/RequestFunctionArn": [ + { + "type": "aws:cdk:logicalId", + "data": "RequestFunctionArn" + } + ], + "/integ-distribution-function/RequestFunctionStage": [ + { + "type": "aws:cdk:logicalId", + "data": "RequestFunctionStage" + } + ], + "/integ-distribution-function/ResponseFunctionArn": [ + { + "type": "aws:cdk:logicalId", + "data": "ResponseFunctionArn" + } + ], + "/integ-distribution-function/ResponseFunctionStage": [ + { + "type": "aws:cdk:logicalId", + "data": "ResponseFunctionStage" + } + ], + "/integ-distribution-function/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-distribution-function/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "integ-distribution-function" + }, + "CF2RuntimeDefaultTestDeployAssertB25F4037.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "CF2RuntimeDefaultTestDeployAssertB25F4037.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "CF2RuntimeDefaultTestDeployAssertB25F4037": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "CF2RuntimeDefaultTestDeployAssertB25F4037.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": [ + "CF2RuntimeDefaultTestDeployAssertB25F4037.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": [ + "CF2RuntimeDefaultTestDeployAssertB25F4037.assets" + ], + "metadata": { + "/CF2Runtime/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/CF2Runtime/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "CF2Runtime/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.distribution-function-runtime.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/tree.json new file mode 100644 index 0000000000000..47bb430289c3b --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/tree.json @@ -0,0 +1,265 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "integ-distribution-function": { + "id": "integ-distribution-function", + "path": "integ-distribution-function", + "children": { + "FunctionRequest": { + "id": "FunctionRequest", + "path": "integ-distribution-function/FunctionRequest", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-distribution-function/FunctionRequest/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Function", + "aws:cdk:cloudformation:props": { + "autoPublish": true, + "functionCode": "function handler(event) { return event.request }", + "functionConfig": { + "comment": "eu-west-1integdistributionfunctionFunctionRequest8E65DEEB", + "runtime": "cloudfront-js-1.0" + }, + "name": "eu-west-1integdistributionfunctionFunctionRequest8E65DEEB" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.Function", + "version": "0.0.0" + } + }, + "FunctionResponse": { + "id": "FunctionResponse", + "path": "integ-distribution-function/FunctionResponse", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-distribution-function/FunctionResponse/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Function", + "aws:cdk:cloudformation:props": { + "autoPublish": true, + "functionCode": "function handler(event) { return event.response }", + "functionConfig": { + "comment": "eu-west-1integdistributionfunctionFunctionResponseDD4BADA1", + "runtime": "cloudfront-js-2.0" + }, + "name": "eu-west-1integdistributionfunctionFunctionResponseDD4BADA1" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.Function", + "version": "0.0.0" + } + }, + "Dist": { + "id": "Dist", + "path": "integ-distribution-function/Dist", + "children": { + "Origin1": { + "id": "Origin1", + "path": "integ-distribution-function/Dist/Origin1", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-distribution-function/Dist/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Distribution", + "aws:cdk:cloudformation:props": { + "distributionConfig": { + "enabled": true, + "origins": [ + { + "domainName": "www.example.com", + "id": "integdistributionfunctionDistOrigin1D1E9DF17", + "customOriginConfig": { + "originProtocolPolicy": "https-only" + } + } + ], + "defaultCacheBehavior": { + "pathPattern": "*", + "targetOriginId": "integdistributionfunctionDistOrigin1D1E9DF17", + "cachePolicyId": "4135ea2d-6df8-44a3-9df3-4b5a84be39ad", + "compress": true, + "viewerProtocolPolicy": "allow-all", + "functionAssociations": [ + { + "functionArn": { + "Fn::GetAtt": [ + "FunctionRequest95528B2F", + "FunctionARN" + ] + }, + "eventType": "viewer-request" + }, + { + "functionArn": { + "Fn::GetAtt": [ + "FunctionResponse4EF2D1D3", + "FunctionARN" + ] + }, + "eventType": "viewer-response" + } + ] + }, + "httpVersion": "http2", + "ipv6Enabled": true + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnDistribution", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.Distribution", + "version": "0.0.0" + } + }, + "RequestFunctionArn": { + "id": "RequestFunctionArn", + "path": "integ-distribution-function/RequestFunctionArn", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + }, + "RequestFunctionStage": { + "id": "RequestFunctionStage", + "path": "integ-distribution-function/RequestFunctionStage", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + }, + "ResponseFunctionArn": { + "id": "ResponseFunctionArn", + "path": "integ-distribution-function/ResponseFunctionArn", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + }, + "ResponseFunctionStage": { + "id": "ResponseFunctionStage", + "path": "integ-distribution-function/ResponseFunctionStage", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "integ-distribution-function/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "integ-distribution-function/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "CF2Runtime": { + "id": "CF2Runtime", + "path": "CF2Runtime", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "CF2Runtime/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "CF2Runtime/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "CF2Runtime/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "CF2Runtime/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "CF2Runtime/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.distribution-function-runtime.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.ts new file mode 100644 index 0000000000000..9ac8bcfe9e768 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.ts @@ -0,0 +1,43 @@ +import * as cdk from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import { TestOrigin } from './test-origin'; +import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'integ-distribution-function', { env: { region: 'eu-west-1' } }); + +const cfFunctionRequest = new cloudfront.Function(stack, 'FunctionRequest', { + code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'), + runtime: cloudfront.FunctionRuntime.JS_1_0, +}); + +const cfFunctionResponse = new cloudfront.Function(stack, 'FunctionResponse', { + code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.response }'), + runtime: cloudfront.FunctionRuntime.JS_2_0, +}); + +new cloudfront.Distribution(stack, 'Dist', { + defaultBehavior: { + origin: new TestOrigin('www.example.com'), + cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED, + functionAssociations: [{ + function: cfFunctionRequest, + eventType: cloudfront.FunctionEventType.VIEWER_REQUEST, + }, { + function: cfFunctionResponse, + eventType: cloudfront.FunctionEventType.VIEWER_RESPONSE, + }], + }, +}); + +new cdk.CfnOutput(stack, 'RequestFunctionArn', { value: cfFunctionRequest.functionArn }); +new cdk.CfnOutput(stack, 'RequestFunctionStage', { value: cfFunctionRequest.functionStage }); + +new cdk.CfnOutput(stack, 'ResponseFunctionArn', { value: cfFunctionResponse.functionArn }); +new cdk.CfnOutput(stack, 'ResponseFunctionStage', { value: cfFunctionResponse.functionStage }); + +new IntegTest(app, 'CF2Runtime', { + 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 18b47bcd6ea51..333367b4ec176 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/README.md +++ b/packages/aws-cdk-lib/aws-cloudfront/README.md @@ -518,6 +518,7 @@ You can also deploy CloudFront functions and add them to a CloudFront distributi // Add a cloudfront Function to a Distribution const cfFunction = new cloudfront.Function(this, 'Function', { code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'), + runtime: cloudfront.FunctionRuntime.JS_2_0, }); declare const s3Bucket: s3.Bucket; diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts index d4dd19e98a18b..71b874adb1934 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/function.ts @@ -100,6 +100,13 @@ export interface FunctionAttributes { * The ARN of the function. */ readonly functionArn: string; + + /** + * The Runtime of the function. + * @default FunctionRuntime.JS_1_0 + */ + readonly functionRuntime?: string; + } /** @@ -122,6 +129,12 @@ export interface FunctionProps { * The source code of the function. */ readonly code: FunctionCode; + + /** + * The runtime environment for the function. + * @default FunctionRuntime.JS_1_0 + */ + readonly runtime?: FunctionRuntime; } /** @@ -136,6 +149,7 @@ export class Function extends Resource implements IFunction { return new class extends Resource implements IFunction { public readonly functionName = attrs.functionName; public readonly functionArn = attrs.functionArn; + public readonly functionRuntime = attrs.functionRuntime ?? FunctionRuntime.JS_1_0.value; }(scope, id); } @@ -154,18 +168,25 @@ export class Function extends Resource implements IFunction { * @attribute */ public readonly functionStage: string; + /** + * the runtime of the CloudFront function + * @attribute + */ + public readonly functionRuntime: string; constructor(scope: Construct, id: string, props: FunctionProps) { super(scope, id); this.functionName = props.functionName ?? this.generateName(); + this.functionRuntime = props.runtime?.value ?? FunctionRuntime.JS_1_0.value; + const resource = new CfnFunction(this, 'Resource', { autoPublish: true, functionCode: props.code.render(), functionConfig: { comment: props.comment ?? this.functionName, - runtime: 'cloudfront-js-1.0', + runtime: this.functionRuntime, }, name: this.functionName, }); @@ -212,3 +233,29 @@ export interface FunctionAssociation { /** The type of event which should invoke the function. */ readonly eventType: FunctionEventType; } + +/** + * The function's runtime environment version. + */ +export class FunctionRuntime { + /** + * cloudfront-js-1.0 + */ + public static readonly JS_1_0 = new FunctionRuntime('cloudfront-js-1.0'); + + /** + * cloudfront-js-2.0 + */ + public static readonly JS_2_0 = new FunctionRuntime('cloudfront-js-2.0'); + + /** + * A custom runtime string. + * + * Gives full control over the runtime string fragment. + */ + public static custom(runtimeString: string): FunctionRuntime { + return new FunctionRuntime(runtimeString); + } + + private constructor(public readonly value: string) {} +} 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 7573e1c7e65af..142559b07ea7c 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 { App, Stack } from '../../core'; -import { Function, FunctionCode } from '../lib'; +import { Function, FunctionCode, FunctionRuntime } from '../lib'; describe('CloudFront Function', () => { @@ -132,4 +132,60 @@ describe('CloudFront Function', () => { }, }); }); + + test('runtime testing', () => { + const app = new App(); + const stack = new Stack(app, 'Stack', { + env: { account: '123456789012', region: 'testregion' }, + }); + new Function(stack, 'CF2', { + code: FunctionCode.fromInline('code'), + runtime: FunctionRuntime.JS_2_0, + }); + + Template.fromStack(stack).templateMatches({ + Resources: { + CF2D7241DD7: { + Type: 'AWS::CloudFront::Function', + Properties: { + Name: 'testregionStackCF2CE3F783F', + AutoPublish: true, + FunctionCode: 'code', + FunctionConfig: { + Comment: 'testregionStackCF2CE3F783F', + Runtime: 'cloudfront-js-2.0', + }, + }, + }, + }, + }); + }); + + test('custom runtime testing', () => { + const app = new App(); + const stack = new Stack(app, 'Stack', { + env: { account: '123456789012', region: 'testregion' }, + }); + new Function(stack, 'CF2', { + code: FunctionCode.fromInline('code'), + runtime: FunctionRuntime.custom('cloudfront-js-2.0'), + }); + + Template.fromStack(stack).templateMatches({ + Resources: { + CF2D7241DD7: { + Type: 'AWS::CloudFront::Function', + Properties: { + Name: 'testregionStackCF2CE3F783F', + AutoPublish: true, + FunctionCode: 'code', + FunctionConfig: { + Comment: 'testregionStackCF2CE3F783F', + Runtime: 'cloudfront-js-2.0', + }, + }, + }, + }, + }); + }); }); diff --git a/packages/aws-cdk-lib/awslint.json b/packages/aws-cdk-lib/awslint.json index 9f44fe0c02597..4798a4f3fc2fd 100644 --- a/packages/aws-cdk-lib/awslint.json +++ b/packages/aws-cdk-lib/awslint.json @@ -236,6 +236,7 @@ "docs-public-apis:aws-cdk-lib.aws_autoscaling.TargetTrackingScalingPolicy", "docs-public-apis:aws-cdk-lib.aws_cloudfront.ViewerCertificate.aliases", "docs-public-apis:aws-cdk-lib.aws_cloudfront.ViewerCertificate.props", + "docs-public-apis:aws-cdk-lib.aws_cloudfront.FunctionRuntime.value", "docs-public-apis:aws-cdk-lib.aws_cloudtrail.InsightType.value", "docs-public-apis:aws-cdk-lib.aws_cloudwatch.DefaultValue.val", "docs-public-apis:aws-cdk-lib.aws_cloudwatch.Values.toJson",