Skip to content

Commit

Permalink
Allow only one associated KV store
Browse files Browse the repository at this point in the history
  • Loading branch information
kylelaker committed Jan 10, 2024
1 parent 4825740 commit 8728e41
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 126 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,26 @@
"Name": "integfunctionkeyvaluestoreassocationTestKeyValueStoreAA209C02"
}
},
"TestKeyValueStore25F61F0F5": {
"Type": "AWS::CloudFront::KeyValueStore",
"Properties": {
"Name": "integfunctionkeyvaluestoreassocationTestKeyValueStore2A09DC08D"
}
},
"TestFunction22AD90FC": {
"Type": "AWS::CloudFront::Function",
"Properties": {
"AutoPublish": true,
"FunctionCode": "code",
"FunctionConfig": {
"Comment": {
"Fn::Join": [
"",
[
{
"Ref": "AWS::Region"
},
"integfunctreassocationTestFunctionEC38B947"
]
]
},
"Runtime": "cloudfront-js-1.0"
"Comment": "TestKvFunction",
"KeyValueStoreAssociations": [
{
"KeyValueStoreARN": {
"Fn::GetAtt": [
"TestKeyValueStore8D0C09A2",
"Arn"
]
}
}
],
"Runtime": "cloudfront-js-2.0"
},
"Name": {
"Fn::Join": [
"",
[
{
"Ref": "AWS::Region"
},
"integfunctreassocationTestFunctionEC38B947"
]
]
}
"Name": "TestKvFunction"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ 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')),
});
const store2 = new cloudfront.KeyValueStore(stack, 'TestKeyValueStore2');
new cloudfront.Function(stack, 'TestFunction', {
functionName: 'TestKvFunction',
code: cloudfront.FunctionCode.fromInline('code'),
keyValueStores: [store, store2],
keyValueStore: store,
});

new IntegTest(app, 'FunctionKeyValueStoreAssocation', {
Expand Down
18 changes: 5 additions & 13 deletions packages/aws-cdk-lib/aws-cloudfront/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ export interface FunctionProps {
readonly runtime?: FunctionRuntime;

/**
* The Key Value Store(s) to associate with this function.
* 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 keyValueStores?: IKeyValueStore[];
readonly keyValueStore?: IKeyValueStore;
}

/**
Expand Down Expand Up @@ -190,10 +190,10 @@ export class Function extends Resource implements IFunction {

this.functionName = props.functionName ?? this.generateName();

const defaultFunctionRuntime = props.keyValueStores?.length ? FunctionRuntime.JS_2_0.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.keyValueStores?.length && this.functionRuntime === FunctionRuntime.JS_1_0.value) {
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`,
);
Expand All @@ -205,7 +205,7 @@ export class Function extends Resource implements IFunction {
functionConfig: {
comment: props.comment ?? this.functionName,
runtime: this.functionRuntime,
keyValueStoreAssociations: this.buildKeyValueStoreProperties(props),
keyValueStoreAssociations: props.keyValueStore ? [{ keyValueStoreArn: props.keyValueStore?.keyValueStoreArn }] : undefined,
},
name: this.functionName,
});
Expand All @@ -221,14 +221,6 @@ export class Function extends Resource implements IFunction {
}
return name;
}

private buildKeyValueStoreProperties(props: FunctionProps): CfnFunction.KeyValueStoreAssociationProperty[] | undefined {
if (!props.keyValueStores?.length) {
return undefined;
}

return props.keyValueStores.map(({ keyValueStoreArn }) => ({ keyValueStoreArn }));
}
}

/**
Expand Down
32 changes: 6 additions & 26 deletions packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('CloudFront Function', () => {

new Function(stack, 'TestFn', {
code: FunctionCode.fromInline('code'),
keyValueStores: [keyValueStore],
keyValueStore,
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', {
Expand All @@ -215,8 +215,8 @@ describe('CloudFront Function', () => {

expect(() => new Function(stack, 'TestFn', {
code: FunctionCode.fromInline('code'),
keyValueStores: [keyValueStore],
runtime: FunctionRuntime.JS_1_0,
keyValueStore,
})).toThrow(/Key Value Stores cannot be associated to functions using the .* runtime/);
});

Expand All @@ -226,8 +226,8 @@ describe('CloudFront Function', () => {

new Function(stack, 'TestFn', {
code: FunctionCode.fromInline('code'),
keyValueStores: [keyValueStore],
runtime: undefined,
keyValueStore,
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', {
Expand All @@ -243,8 +243,8 @@ describe('CloudFront Function', () => {

new Function(stack, 'TestFn', {
code: FunctionCode.fromInline('code'),
keyValueStores: [keyValueStore],
runtime: FunctionRuntime.JS_2_0,
keyValueStore,
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', {
Expand All @@ -257,13 +257,13 @@ describe('CloudFront Function', () => {
});
});

test('empty list is equivalent to not specifying', () => {
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,
keyValueStores: [],
keyValueStore: undefined,
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', {
Expand All @@ -272,25 +272,5 @@ describe('CloudFront Function', () => {
},
});
});

test('multiple key value stores can be specified', () => {
const stack = new Stack();
const store1 = new KeyValueStore(stack, 'TestStore1');
const store2 = new KeyValueStore(stack, 'TestStore2');

new Function(stack, 'TestFn', {
code: FunctionCode.fromInline('code'),
keyValueStores: [store1, store2],
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Function', {
FunctionConfig: {
KeyValueStoreAssociations: [
{ KeyValueStoreARN: stack.resolve(store1.keyValueStoreArn) },
{ KeyValueStoreARN: stack.resolve(store2.keyValueStoreArn) },
],
},
});
});
});
});

0 comments on commit 8728e41

Please sign in to comment.