Hey! Thanks for creating the project! I've been u...
# help
a
Hey! Thanks for creating the project! I've been using the cdk to deploy a Hasura backend, using this template: https://github.com/lineupninja/hasura-cdk I've been trying to migrate the current setup to the sst, and I'm facing a problem. Right now, I use a post-deploy script that picks up values from cdk.CfnOutput (cdk.outputs.json) -> https://github.com/lineupninja/hasura-cdk/blob/master/cdk/bin/post-deploy.ts I understand that the sst doesn't use a cdk.json file. Any suggestion of alternative ways of getting the data?
j
Hmm that's interesting. We haven't really considered that use case before. How does this project generate the cdk.outputs.json?
f
@Andreas I just opened an issue to support
--output-files
flag for
sst deploy
- https://github.com/serverless-stack/serverless-stack/issues/94
a
@Jay looks like it uses CfnOutput to output the data.
Copy code
// Output the Endpoint Address so it can be used in post-deploy
        new CfnOutput(this, 'HasuraDatabaseUserSecretArn', {
            value: hasuraUserSecret.secretArn,
        });
j
Ah I see.
a
And also the --outputs-file flag on cdk deploy
Let me know if I can do anything to help with this enhancement. I'm excited to use the sst for the project. If you could point me in the right direction and if it isn't too complicated I'll try to put together a PR.
f
Hey @Andreas, yeah I can help you get started. Let's just check a few things real quick: • Beside
cdk deploy
, any other cdk commands accept the
--outputs-file
flag? • Is there any cdk command that consumes the outputs file as input? • What's the outputted file format? (Might be helpful to share an example here) • Does deploying a single stack (ie.
cdk deploy stackA --outputs-file xxxx
) also outputs the file? Is the format the same for deploying all stacks (ie.
cdk deploy --outputs-file xxxx
)?
Let me know and I will point you to the relavent files.
a
• Beside
cdk deploy
, any other cdk commands accept the
--outputs-file
flag? No, only
deploy
accepts the flags
—outputs-file
and the shorthand
-O
• Is there any cdk command that consumes the outputs file as input? No. I've been programmatically reading it in a post deploy script.
Copy code
const cdkOutputsData = JSON.parse(readFileSync('cdk.outputs.json', 'utf-8'));
const masterSecretArn = cdkOutputsData['app-name-HasuraStack'].HasuraDatabaseMasterSecretArn;
• What's the outputted file format? (Might be helpful to share an example here)
Copy code
# HasuraStack.ts
new cdk.CfnOutput(this, 'HasuraDatabaseUserSecretArn', {
      value: hasuraUserSecret.secretArn,
    });

new cdk.CfnOutput(this, 'HasuraDatabaseMasterSecretArn', {
  value: hasuraDatabase.secret!.secretArn,
});

cdk deploy --outputs-file cdk.outputs.json
# cdk.outputs.json
{
  "app-name-HasuraStack": {
    "HasuraDatabaseMasterSecretArn": "arn:aws:secretsmanager:us-west-2:123:secret:HasuraDatabaseSecret123",
    "HasuraDatabaseUrlSecretArn": "arn:aws:secretsmanager:us-west-2:123:secret:AppName-HasuraDatabaseUrl-123"
  },
	"app-name-CognitoStack": {
    "UserPoolId": "us-west-2_123",
    "ValidateEmailEndpoint865F3B39": "<<https://123.execute-api.us-west-2.amazonaws.com/prod/>>"
  },
}
• Does deploying a single stack (ie.
cdk deploy stackA --outputs-file xxxx
) also outputs the file? Is the format the same for deploying all stacks (ie.
cdk deploy --outputs-file xxxx
)? Yes it does, with the same format. When only deploying one stack in a multistack environment the output file will only include that stack and have this format:
Copy code
{
  "myStackOne": {
    "MyStackOneArn": "arn:aws:s3:::mystackone-1234"
  }
}
f
Awesome! Let me put together a couple steps to get started.
Alright🎉 To get started: • Checkout the code https://github.com/serverless-stack/serverless-stack • Run
yarn
at the root • Run
yarn test
to make sure the tests pass • All the cli options are defined in
packages/cli/bin/scripts.js
. SST uses yargs to parse the options. This is their doc http://yargs.js.org/docs/#api-reference-commandcmd-desc-builder-handler
sst deploy
runs
packages/cli/scripts/deploy.js
. There is a
printResults
method that prints out stack outputs and exports to the terminal at the end of the deploy. The
stackStates
object should have all the data you need to create the outputs file. One thing I'd check is that in the case some stacks deployed successfully and some failed, does cdk create the outputs file with partial data? Let's keep the behavior the same. To test the deploy command, go into
packages/cli/test/base
and run
yarn
. And then you can run
yarn deploy --outputs-file
to test your command. Currently there are 4 stacks in the
base
project, feel free to change it as suited. Just don't commit the changes.
a
Sounds good, I'm on it 🙂
Here's some comments in the cdk source code about failed deployments and the outputs file: Outputs are written after all stacks have been deployed. If a stack deployment fails, all of the outputs from successfully deployed stacks before the failure will still be written. https://github.com/aws/aws-cdk/blob/master/packages/aws-cdk/lib/cdk-toolkit.ts#L220 So to achieve the same behavior as the cdk the outputs file could be written when you print the results in
deploy.js
, right?
Also, I see that the cdk uses ensureFileSync and writeJson from the fs-extra library to write the file. Are you ok with adding that, or is it good enough to just use the native fs library with writeFileSync and doing a try/catch?
f
Yup writing file when printing the results sound good.
We already use fs-extra, and it’s already included as a dependency.