aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.33k stars 3.76k forks source link

aws-s3-deployment: Support `CfnMapping` (Fn::FindInMap) in deploy time values for `Source.jsonData` #30369

Open valebedu opened 1 month ago

valebedu commented 1 month ago

Describe the feature

As discussed in https://github.com/aws/aws-cdk/issues/25504 and addressed partially in https://github.com/aws/aws-cdk/pull/27237, support for Fn::FindInMap in deploy time values for Source.jsonData is still needed.

Currently, using Fn::FindInMap results in the following error:

api-infra: Error: Invalid CloudFormation reference. Key must start with any of "Ref" or "Fn::GetAtt" or "Fn::Select". Got {"Fn::FindInMap":["DefaultMapping","responseTemplate","full"]}
api-infra:     at addMarker (/Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/render-data.js:1:1336)
api-infra:     at renderData (/Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/render-data.js:1:799)
api-infra:     at Object.bind (/Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/source.js:1:1919)
api-infra:     at Object.bind (/Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/source.js:1:2306)
api-infra:     at /Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.js:1:3768
api-infra:     at Array.map (<anonymous>)
api-infra:     at new BucketDeployment (/Users/vale/Workspace/api/node_modules/.pnpm/aws-cdk-lib@2.143.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.js:1:3749)
api-infra:     at new TypeSafeRestApi (/Users/vale/Workspace/api/node_modules/.pnpm/file+..+..+..+lab+aws-pdk+packages+pdk+dist+js+package_@aws-cdk+aws-cognito-identitypool-alph_2yxqjbpf3vtng3yuse5lau2hn4/node_modules/@aws/pdk/type-safe-api/construct/type-safe-rest-api.ts:176:5)
api-infra:     at new Api (/Users/vale/Workspace/api/packages/api/generated/infrastructure/typescript/src/api.ts:18:5)
api-infra:     at new Api (/Users/vale/Workspace/api/packages/api/infra/src/constructs/apis/api.ts:468:5)

Use Case

In large APIs with custom response templates, such as code error mappings, it's necessary to use CfnMapping to avoid CloudFormation template size limits. Repeating the same mapping multiple times causes CloudFormation to raise an error due to template size constraints. By using CfnMapping, we can maintain a single instance of the mapping, reducing the overall template size.

This is discussed here https://github.com/aws/aws-pdk/issues/771

Proposed Solution

Extend the functionality introduced in https://github.com/aws/aws-cdk/pull/27237 to support Fn::FindInMap in deploy time values for Source.jsonData.

Other Information

No response

Acknowledgements

CDK version used

2.143.0

Environment details (OS name and version, etc.)

macOS Sonoma 14.5 Apple M1 Pro

khushail commented 1 month ago

Right now. these are the supported functions -

https://github.com/aws/aws-cdk/blob/d27de72bfcfe648e38d0e0edee7fba062b7a469d/packages/aws-cdk-lib/aws-s3-deployment/lib/render-data.ts#L61

Fn:FindInMap function support makes sense given the usecase. Marking it as appropriate. Thanks @valebedu for the FR!