question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[@aws-cdk/pipelines] Support multiple synthActions

See original GitHub issue

I think by allowing multiple synth actions, and allowing us to control which stacks get synthesized when, we can have more elaborate deployment behavior.

(I just realized this may already be possible, I explain in the “Other” section why I’m uncertain)

Use Case

I’m hoping there’s another way to think about this or approach this.

The problem is two stacks, where stack B has an asset of a directory that is generated in a separate directory by an out-of-band process (npm run build) requiring cloudformation outputs from deployed stack A, because the values get substituted into the files upon generation. This is problematic because synthesis time is when the files get looked for and copied to the cloud assembly directory, so normally I would have to synthesize the first, deploy it, run npm run build, then synthesize and deploy the second.

Imagine the following stacks:

  • Api with an AppSync resource and a CfnOutput of that resource’s GraphQL endpoint URL (AFAIK known only after deploying)

  • Web with an S3 CloudFront distribution, using s3-deployment to add a directory as an asset and deploy it to an S3 bucket and invalidate the CloudFront cache

    • The key thing is that the files that Web bundles need to be generated by webpack via npm run build before bundling.
    • Equally as important, the GraphQL endpoint URL needs to be provided as an environment variable GRAPHQL_URL when invoking npm run build so that webpack substitutes a placeholder in files with the actual value.

In other words, the dependency/sequence is something like this:

  1. Deploy Api stack, populates CfnOutputs like GRAPHQL_URL

  2. cd web/ && GRAPHQL_URL="READ_FROM_CLOUDFORMATION_OUTPUT" npm run build

    This generates files at web/build/ with GRAPHQL_URL substituted in the output files

  3. Deploy Web stack, which has web/build/ as an asset (for S3)

The problem with just slapping things together into a pipeline in the status quo is that the synthAction synthesizes all stacks at once.

This won’t work in this setup because doing so will “asset”-ize (?) Web’s files, even though they don’t exist yet since npm run build hasn’t run.

That can be be resolved by modifying synthAction’s synth command to run npm run build before the synth, but the problem is that the npm run build wouldn’t be appropriate here yet because Api hasn’t been deployed, so we wouldn’t know the GRAPHQL_URL yet.

We can go further and modify the synth command to synth Api only, and not synthesize Web (unless #9792 means that it’ll be synthesized anyway, but I probably misunderstood), so we can wait for Api to be deployed, then npm run build with the CfnOutput, but then how do we synthesize Web and deploy it? Now we’re left to our own devices, missing out on the pipeline machinery.

Proposed Solution

I honestly don’t know if this would fix the problem, but I just figure that if we can specify a synthAction anywhere like we can with other actions, then we can arrange them however we see fit. This way, I would have:

  1. a synthAction for Api
  2. ensure Api deploys (I don’t know if I need an explicit DeployCdkStackAction or if this is implicit)
  3. a SimpleShellAction for npm run build of Web’s assets, since we can now read Api’s deployed CloudFormation outputs
  4. finally another synthAction for Web now that the files have been generated

Other

Am I going about it the wrong way? Again, the issue fundamentally is two stacks where one has an asset requiring out-of-band generation that itself requires reading the CloudFormation outputs of the deployed stack, keeping in mind that synthesis-time is when CDK looks for and expects the assets.

I do see that SimpleSynthAction is just an IAction, but does that mean I really can just place it anywhere I want? The reason I’m not sure is because synthAction appears to be a specific attribute in CdkPipelineProps, so I’m not sure if simply adding it anywhere more than once would have the intended effect. If so, then I suppose I can just modify the CdkPipelineProps.synthAction’s synthCommand attribute to just synthesize Api, and then other actions, and then my own constructed SimpleSynthAction this time for Web?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
maciekbbcommented, Feb 28, 2021

@blaenk I wrote a post on how to solve it https://maciekbb.medium.com/cdk-pipelines-beyond-basics-37b731b7a182

hope you will find it useful!

0reactions
blaenkcommented, Jan 8, 2022

Sorry for the late reply @maciekbb but thank you for sharing. Your approach is clever and in my opinion the right way to go about this. I was aware of outputting the outputs to a file but it didn’t occur to me at the time. Nice job!

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/pipelines module - AWS Documentation - Amazon.com
CDK Pipelines supports multiple deployment engines (see Using a different deployment engine), and comes with a deployment engine that deploys CDK apps using ......
Read more >
Continuous integration and delivery (CI/CD) using CDK ...
CDK Pipelines is a construct library module for painless continuous delivery of AWS CDK applications. Whenever you check your AWS CDK app's source...
Read more >
CDK Pipelines - AWS Documentation - Amazon.com
CDK Pipelines supports multiple deployment engines, but this module vends a construct for only one such engine: AWS CodePipeline. It is also possible...
Read more >
Continuous delivery for AWS CDK applications
To create multiple AWS accounts, you can use AWS Control Tower. ... A CDK pipeline starts with several predefined stages and actions, ...
Read more >
aws-cdk/aws-codepipeline-actions module
Actions for deploying CloudFormation StackSets to multiple accounts. You can use CloudFormation StackSets to deploy the same CloudFormation template to multiple ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found