-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-s3-deployment): assets with deploy-time values #12903
Comments
Implemented current workaround in https://stackoverflow.com/a/66084764/2088936 as proof of feasibility/starting point for discussion. |
I'm offering a solution for this here https://github.com/jogold/cloudstructs/tree/master/src/static-website (static website with backend config saved to the bucket). |
@johnameyer I'm not opposed to having this functionality, seems useful. So the current proposal is to:
Right? P.S Is what @jogold referenced fully addresses your use-case? |
That's the current workaround and I think it is the least disagreeable. However, it is not a particularly efficient solution since it's adding a custom resource and a bucket that just feeds into another custom resource and bucket under the hood. I think there a few ways to make it less heavy-handed. @jogold's solution is pretty good, but it doesn't directly integrate with s3-deployments (and as such things like the cloudfront invalidation that I need). |
@johnameyer Yeap, I think option 3 is the best, just to make sure we aligned, basically adding a new property in aws-cdk/packages/@aws-cdk/aws-s3-deployment/lib/source.ts Lines 9 to 19 in 3b5ff05
And take it from there. Yes? |
Oh yep yep SourceConfig is the one. |
@johnameyer Sounds great. feel free to pick this up whenever you get a chance :) |
I found a simple way to do this, but it doesn't use aws-s3-deployment:
|
@fjmacagno
|
Unfortunate, though not susprising |
I believe the root of the problem is deeper than what has been discussed. In the CDK it is assumed that "Assets" are dependencies for the constructs in the stack (for example a ZIP asset containing lambda handler code is a dependency of the lambda construct). Since "Assets" are assumed to be dependencies of the constructs they are built and deployed before the rest of the stack. This design breaks down when the dependencies are flipped: constructs in the stack are dependencies of the assets. For example, an "Asset" containing the files for a static s3 website depend on the stack constructs since the API gateway/cloudfront distrubution needs to be baked into the frontend "Asset". To "properly" solve this issue I think that "Assets" should have an optional argument to determine when they are deployed. If the assets are dependencies for the constructs they should be deployed before the constructs -- as they are currently. If the assets depend on the constructs they should be deployed at the same time as the constructs so any dynamic values can be injected into the assets. I realize this is a very large change, but I think this is the "correct" solution. |
This is a fun challenge, and can be a major enabler, so definitely worth looking into if anyone is interested to pick this up. Here's a proposed approach: materialize "deploy-time values" into the asset file during synthesis (e.g. replace tokens with a set of markers) and then substitute these markers with their deploy-time value inside the s3-deployment custom resource when the file is copied from the assets bucket to its final destination. I think it will be simpler to support this for JSON first (due to how token resolution works), but we can add text files in later: Sketch: const files = Source.json({
'config.json': {
Foo: {
Bar: sns.topicArn // <-- { Ref }
}
},
});
new BucketDeployment(this, 'BucketDeployment', {
sources: [files]
}); The
{
"Foo": {
"Bar": "{{MARKER_11122128378}}"
}
} The |
Allow deploying test-based content that can potentially include deploy-time values such as attributes of cloud resources. Introduce a `Source.content(objectKey, text)` where `text` can naturally include tokens that resolve only at deploy time. This is implemented by replacing the deploy-time tokens with markers that are replaced inside the s3-deployment custome resource. Fixes #12903 TODO: - [ ] Implement + test the custom resource code - [ ] Documentation - [ ] Finalize integration test
Started to work on this here in case anyone is interested to track/comment: #18659 |
Allow deploying test-based content that can potentially include deploy-time values such as attributes of cloud resources. Introduce a `Source.data(objectKey, text)` and `Source.jsonData(objectKey, obj)` where the data can naturally include deploy-time tokens such as references to resources (`Ref`) or to resource attributes (`Fn::GetAtt`). For example: ```ts const appConfig = { topic_arn: topic.topicArn, base_url: 'https://my-endpoint', }; new s3deploy.BucketDeployment(this, 'BucketDeployment', { sources: [s3deploy.Source.jsonData('config.json', config)], destinationBucket: destinationBucket, }); ``` This is implemented by replacing the deploy-time tokens with markers that are replaced inside the s3-deployment custom resource. Fixes #12903 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
Allow deploying test-based content that can potentially include deploy-time values such as attributes of cloud resources. Introduce a `Source.data(objectKey, text)` and `Source.jsonData(objectKey, obj)` where the data can naturally include deploy-time tokens such as references to resources (`Ref`) or to resource attributes (`Fn::GetAtt`). For example: ```ts const appConfig = { topic_arn: topic.topicArn, base_url: 'https://my-endpoint', }; new s3deploy.BucketDeployment(this, 'BucketDeployment', { sources: [s3deploy.Source.jsonData('config.json', config)], destinationBucket: destinationBucket, }); ``` This is implemented by replacing the deploy-time tokens with markers that are replaced inside the s3-deployment custom resource. Fixes aws#12903 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@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
|
Allow 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:
This is a 🚀 Feature Request
The text was updated successfully, but these errors were encountered: