Open SydneyUni-Jim opened 4 months ago
It would be great if find
could be a JavaScript regular expression or a string. And it would be great if replace
could be any
, with findReplace
converting anything not a string into a string. But I don't know if that's possible with JSII.
Possible implementation of findReplace
.
protected findReplace(code: string, findReplaceArr?: FindReplace[]): string {
if (!findReplaceArr?.length) return code
function reducer(acc: string, opt: FindReplace) {
const r: string = typeof opt.replace === 'string' ? opt.replace : cdk.Token.asString(opt.replace)
return opt.all ? acc.replaceAll(opt.find, r) : acc.replace(opt.find, r)
}
return findReplaceArr.reduce(reducer, code)
}
Hi @SydneyUni-Jim , thanks for reaching out. I see this readme doc talks about Key-value store ,quite similar to what you have proposed in solution. I might not fully comprehend about the solution but we are open to suggestions and please feel free to submit a PR !
HI @khushail. Thanks for pointing out the docs. Unfortunately that only associates the Key Value Store with the function. It doesn't make the store's id available to the function's code. I will work on a PR.
Describe the feature
CloudFront Functions (not Lambda@Edge) don't provide a way to parameterise the code, in the way environment variables can with Lambda@Edge, for example. Allow code to be replaced with values from the CDK app during synth.
Use Case
Proposed Solution
In aws-cdk-lib/aws-cloudfront/lib/function.ts:
Add these interfaces.
This is a naïve find (all) and replace. It does not attempt to parse the function code to find appropriate tokens. It does not attempt to replace code meaningfully. There is no guarantee that syntactically valid code remains syntactically valid after the find/replace.
Proof of concept
This is the code I'm currently using to get the KVS ID into a CloudFront Function. The proposed solution is a generalisation of this. ```typescript interface FileCodeWithKvsIdSubstitutionOptions extends cloudfront.FileCodeOptions { readonly keyValueStore: cloudfront.IKeyValueStore } class FileCodeWithKvsIdSubstitution extends cloudfront.FunctionCode { constructor(private options: FileCodeWithKvsIdSubstitutionOptions) { super() } public render(): string { return ( fs.readFileSync(this.options.filePath, { encoding: 'utf-8' }) .replace('«KVS_ID»', cdk.Token.asString(this.options.keyValueStore.keyValueStoreId)) ) } } ```
Example of using the proposed solution
Given this code for a CloudFront Function.
__KVS_ID and __IS_PROD could be replaced in the code with this.
If the KVS id is
61736184-1ad9-4b1b-9466-6ca1fb629685
and isProd isfalse
, the code that would be sent to CloudFront would be this.Other Information
The Key Value Store can be used for parameterisation. But there's costs associated with that. And there's the conundrum of how to get the KVS ID into the CloudFront Function to begin with.
Acknowledgements
CDK version used
2.145.0
Environment details (OS name and version, etc.)
NodeJS/iron