{"id":17329979,"url":"https://github.com/coderbyheart/typesafe-stack-outputs-cdk","last_synced_at":"2025-03-27T05:43:58.836Z","repository":{"id":56249195,"uuid":"523519174","full_name":"coderbyheart/typesafe-stack-outputs-cdk","owner":"coderbyheart","description":"In order to ensure that the outputs of your CloudFormation Stacks are typed properly, you can use this technique.","archived":false,"fork":false,"pushed_at":"2022-08-11T09:22:25.000Z","size":99,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"saga","last_synced_at":"2025-02-01T11:11:15.541Z","etag":null,"topics":["cdk","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coderbyheart.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-10T22:54:42.000Z","updated_at":"2023-09-29T06:06:45.000Z","dependencies_parsed_at":"2022-08-15T15:21:05.373Z","dependency_job_id":null,"html_url":"https://github.com/coderbyheart/typesafe-stack-outputs-cdk","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderbyheart%2Ftypesafe-stack-outputs-cdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderbyheart%2Ftypesafe-stack-outputs-cdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderbyheart%2Ftypesafe-stack-outputs-cdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderbyheart%2Ftypesafe-stack-outputs-cdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coderbyheart","download_url":"https://codeload.github.com/coderbyheart/typesafe-stack-outputs-cdk/tar.gz/refs/heads/saga","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245791927,"owners_count":20672669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cdk","typescript"],"created_at":"2024-10-15T14:49:58.359Z","updated_at":"2025-03-27T05:43:58.818Z","avatar_url":"https://github.com/coderbyheart.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Type-safe stack outputs in AWS CDK\n\n_In order to ensure that the outputs of your CloudFormation Stacks are typed\nproperly, you can use this technique._\n\nWhen defining the outputs, use a helper function that is bound to a type that\ndefines all stack outputs. This ensures that outputs that are created are\ndefined in the type definition.\n\n```typescript\n/**\n * This defines the outputs the stack creates\n */\nexport type StackOutputs = {\n  bucketName: string;\n};\n\nexport class MyStack extends Stack {\n  constructor(scope: Construct, id: string, props?: StackProps) {\n    super(scope, id, props);\n\n    /**\n     * A helper function that only allows\n     * to create outputs that are defined in `StackOutputs`\n     */\n    const output = (name: keyof StackOutputs, value: string) =\u003e\n      new CfnOutput(this, name, {\n        value,\n      });\n\n    const bucket = new Bucket(this, \"bucket\");\n    output(\"bucketName\", bucket.bucketName);\n  }\n}\n```\n\nThis enables to use the type that defines the outputs when querying\nCloudFormation for the stack outputs.  \nIn the example below we use\n[`@nordicsemiconductor/cloudformation-helpers`](https://www.npmjs.com/package/@nordicsemiconductor/cloudformation-helpers),\nwhich simplifies fetching stack outputs and accepts a type for defining which\noutputs the stack returns. This will make `outputs` properly typed.\n\n```typescript\nconst cf = new CloudFormationClient({});\n\nconst outputs = await stackOutput(cf)\u003cStackOutputs\u003e(\"MyStack\");\n\nconsole.log(\"Bucket name:\", outputs.bucketName);\n```\n\n## Run the example\n\n    npm ci\n\n    npx cdk deploy\n    # MyStack\n    # Deployment time: 38.5s\n    #\n    # Outputs:\n    # MyStack.bucketName = mystack-bucket43879c71-1prqx9wocdsp7\n\n    npx tsx use-stack-outputs.ts\n    # Bucket name mystack-bucket43879c71-1prqx9wocdsp7\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderbyheart%2Ftypesafe-stack-outputs-cdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoderbyheart%2Ftypesafe-stack-outputs-cdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderbyheart%2Ftypesafe-stack-outputs-cdk/lists"}