Out of curiosity, why does sst.Stack’s constructor...
# sst
m
Out of curiosity, why does sst.Stack’s constructor require an sst.App instead of a construct? It’s a problem in the cdkPipeline because it gets called within the scope of a stage not an sst.App. I guess you’d have to implement an sst Stage type that implements the interface of sst.App to get around it? I guess that’s what the work in https://github.com/serverless-stack/serverless-stack/discussions/279 would involve?
f
The model we had in mind when first designing SST was that ppl would deploy an entire
App
to different environments/accounts. So we didn’t take
Stage
into consideration.
m
yeah to be fair, some of this stuff (like cross account deployment staging) are only available in CDK within the last 6-9 months or so
f
Yeah. I think we can change
sst.Stack
’s scope to take a
Construct
instead of limiting it to an
<http://sst.App|sst.App>
.
Is that the only thing blocking you from setting up SST to use CDK pipeline?
m
I think you’d maybe need to add in a stage as well? Basically you have
app->pipeline(app)->stage(pipeline)->stack(stage)
f
I haven’t thought this thru, but can you use cdk’s
Stage
construct?
m
I think so, everything seems to be ok until that last construct. For an idea of what we are trying to do check this repo out: https://github.com/aws-samples/cdk-pipelines-demo/tree/typescript. It has the following features which are super-cool: • self-updating pipeline so changes to the pipeline itself are automatically deployed and the whole thing restarted • the ability to block a stage on failing unit tests • the ability to roll back the pipeline if integration tests against a live stage fail • the ability to do a canary deploy of 10% of traffic to the new lambda version, 90% stay with the old for 10 minutes • any failure on the canary deploy (ie 5xx error) causes the pipeline to roll back • after 10 minutes of running cleanly with all tests green it deploys to pre-prod for QA and other to review • the prod stage is built and held until a manual step to approve deployment after QA reviews successfully To me, it’s the gold standard of what a CI/CD pipeline should be (including the github checks for linting and whatnot up front)
f
Yeah, that’s pretty neat! What’s missing to set it up? ie. say we changed Stack to take Construct in the constructor, does the following work?
<http://sst.App|sst.App> -> cdk.PipelineStack(app) -> cdk.Stage(pipeline) -> sst.Stack(stage)
m
I think so, I’d have to TIAS to know for sure
f
Yup.. let me make that change in the next release and let’s see from there
m
sounds good (no rush but any eta on that?)
f
I’ll aim for tmr
m
ok, no pressure - thanks!
f
Also, if you go into your
node_modules/@serverless-stack/resources/dist/Stack.d.ts
and change:
Copy code
constructor(scope: App, id: string, props?: StackProps);
to
Copy code
constructor(scope: cdk.Construct, id: string, props?: StackProps);
That should work for now.
Hey @Matthew Purdon Just released v0.10.7. Stack now takes
cdk.Construct
as its scope, give it a try
m
weee!
will do, thanks
Hey @Frank looks like we are a bit closer but running into
root.logicalPrefixedName
when we call the super() for the sst.Stack
if we try to change the signature for the stack to be cdk then child items like tables and functions raise the same error
I’m going to get some food but maybe I can take a swing at a PR after since I see what your commit was
f
Hey @Matthew Purdon I’m not totally following. Are you getting an error when child items call
root.logicalPrefixedName
? Can you share a code snippet maybe?
m
hey, sorry was getting dinner and chilling for a bit
ok @Frank this is about as simple of a pipeline as I could get - I put it into a unit test so you can just run it yourself to see what’s up: https://gist.github.com/mpurdon/737a5c382ce6d45e4304a9e9693a0044
you’ll need to add
"@aws-cdk/pipelines": "1.94.1",
to your
package.json
f
Thanks @Matthew Purdon Lemme take a look at it
m
you bet, lemme know if you have questions we can jump on a call maybe
f
Sounds good! I’m in the middle of working on a new release. I will try out ur code as soon as i get the release out of the way!
@Matthew Purdon I just wrapped up the release. I’m going to take a look at it tmr. 😅
Hi @Matthew Purdon I managed to reproduce the same issue on my end:
Copy code
Error: Stack 'dev-dev-my-app-stack' does not have deployment role information; use the 'DefaultStackSynthesizer' synthesizer, or set the '@aws-cdk/core:newStyleStackSynthesis' context key.
I think there’s an issue with the way SST runs
cdk bootstrap
. For now, this seems to be able to by pass the issue. Add a
cdk.json
file in the same folder as your
sst.json
with the content:
Copy code
{
  "context": {
    "@aws-cdk/core:newStyleStackSynthesis": "true"
  }
}
That let’s me bypass this error message. Give it a try.
m
I was considering that actually but I didn’t want to rely on the change if I was missing a more fundamental fix. I’ll give it a go, thanks for spending the time on it!
Also, is that test something that fits with SST’s testing/code style? I’d be fine adding more tests in a PR or something once we get the base pipeline going
f
Yeah for sure! We definitely want SST to work well with CDK pipelines. As for this specific issue, it might be hard to test for. To add some context, before u can deploy a CDK app, u need to run
cdk bootstrap
once per region per account that you are going to deploy into. And it seems CDK pipeline wants all accounts (dev, prod, etc plus the account for the pipeline) bootstrapped before synthesizing. At least that’s what I’ve gathered so far. So the accounts have been bootstrapped once before, you technically shouldn’t get this error anymore.
I want to take a deeper look at this at some point at put in a fix.. maybe after you managed to get the pipeline working 😄
m
so the change to cdk.json worked and I am past the build stage, now I am working on the self-update part which uses this buildspec:
Copy code
{
  "version": "0.2",
  "phases": {
    "install": {
      "commands": "npm install -g aws-cdk"
    },
    "build": {
      "commands": [
        "cdk -a . deploy dev-MyBackendPipeline --require-approval=never --verbose"
      ]
    }
  }
}
Which fails because it can’t find the
dev-MyBackendPipeline
stack in the output from the previous stages. I am checking into it now
s
I've also been working on getting CDK Pipelines to place nice with SST. I've managed to get Pipeline deployed with a stage containing my App. The issue I'm running into now is when I try to develop locally my sst.Function doesn't trigger locally. It seems like the function doesn't stub because Pipeline doesn't deploy the stack until it reaches the stage its meant to deploy at.
f
Oh hmm.. I can see that
Is it possible to do something like this:
Copy code
export default function main(app: <http://sst.App|sst.App>): void {
  if (process.env.IS_LOCAL) {
    new MyStack(app, "my-stack");
  }
  else {
    new cdk.Pipeline(...)
  }
}
So when running
sst start
u add the Stacks to the app directly
s
Wouldn't that destroy the Pipeline?
Since its no longer part of the synth output
f
😅 lol I obviously didn’t think that through…
s
haha yeah I had the same though too. I'm working through some ideas on how we can get this to work too
f
Or maybe you can work in a separate stage?
Copy code
export default function main(app: <http://sst.App|sst.App>): void {
  if (app.stage === "local") {
    new MyStack(app, "my-stack");
  }
  else {
    new cdk.Pipeline(...)
  }
}
like creating a dedicated
local
stage for development that’s not part of the pipeline.
m
we do this:
Copy code
if (process.env.npm_lifecycle_event === 'start') {
    new ApplicationStack(app, 'api', { tags });
  } else {
    new PipelineStack(app, 'BackendPipeline', {
      description: 'The Pipeline that deploys the API',
      tags,
    });
  }
just because we deploy to stages for all of our developer sandboxes with a start command in our package.json like this:
"start": "sst start --stage=${USER} --verbose"
s
That seems like a good solution thanks