(aws-s3-deployment): assets with deploy-time values
See original GitHub issueAllow for deploying of an inline file to an s3 bucket, like as can be done for Lambda functions.
Use Case
This is one way to allow for resources generated at synth time to be aware of resources that are created in a stack at deploy time, e.g. allowing a static frontend (with a config file that is deployed by this mechanism) to know the URIs of backend resources like a Cognito auth endpoint or an API Gateway. Since normally static resources would be built at the asset generation phase, it would normally be impossible for them to obtain these attributes of resources generated later in the lifecycle. Instead, you can build your code with the assumption of a config file being generated at deploy time and deployed to the bucket and postpone actually needing those URIs.
This is particularly helpful in CDK Pipelines, where it is desired to have all the assets generated up-front and then configure a stage at deploy time, to prevent extensive customization of the Pipeline.
Proposed Solution
From the description of how the s3-deployments module works under the hood, it’s not exactly clear at what point the files are uploaded to the intermediary asset bucket, but it seems at least naively a custom resource could create a resource in a bucket from a parameter (resolved by cloudformation) from which a SourceConfig could be generated.
The workaround currently is to create a custom resource which deploys this file to the bucket, which seems to be how s3-deployments already work, but does not have the in-built feature of invalidating Cloudfront.
Other
S3 Deployments module: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-deployment-readme.html Lambda InlineCode: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.InlineCode.html StackOverflow question: https://stackoverflow.com/questions/60074546/aws-cdk-passing-api-gateway-url-to-static-site-in-same-stack/65799623#65799623 Example usage:
const sources = [
s3deploy.Source.asset('./website-dist'),
s3deploy.Source.inline('config.js', `exports.config = { cognitoUserPool: '${ cognitoUserPool.userPoolClientId }', apiURI: '${ api.apiId }'`)
];
new s3deploy.BucketDeployment(this, 'DeployWebsite', {
sources,
destinationBucket: websiteBucket,
destinationKeyPrefix: 'web/static'
}); // my file should exist at web/static/config.js and have the tokens resolved
- 👋 I may be able to implement this feature request
- ⚠️ This feature might incur a breaking change
This is a 🚀 Feature Request
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:15 (6 by maintainers)
Top GitHub Comments
@eladb I am seeing a problem when values are passed cross-stack… it doesn’t seem to generate the proper cfn-imports/exports and so throws an error because the generated code expects the resource in the same stack
Started to work on this here in case anyone is interested to track/comment: https://github.com/aws/aws-cdk/pull/18659