Skip to content

feat(apigatewayv2): add stage variables support for HTTP and WebSocket API #34548

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gasolima
Copy link

@gasolima gasolima commented May 24, 2025

Issue

Closes #11818 .

Reason for this change

To support stage variables to HttpApi & WebSocketApi.

Description of changes

Add stageVariables property to WebSocketStage & HttpStage.

Describe any new or updated permissions being added

N/A

Description of how you validated changes

  • Add a unit test
  • Tested it manually by creating stack to use the new props and check the CF template & UI

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team May 24, 2025 19:32
@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels May 24, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label May 24, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ Features must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

* @internal
*/
protected get _stageVariables(): { [key: string]: string } | undefined {
return Object.keys(this.stageVariables).length > 0 ? { ...this.stageVariables } : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why spread stageVariables into a new object?

{ ...this.stageVariables }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine we make a copy so that callers can't access x.stageVariables to get a mutable reference to the original dict, and mutate it outside addStageVariable() calls.

  • Direct access to mutable collections makes it possible to bypass validation we would put into mutator functions
  • Direct access to mutable collections makes for code that behaves differently in TypeScript vs across JSII, because in JSII there is no such thing as a reference to a mutable collection (all collections are always passed by copy across the wire)

Not strictly necessary because this is a private API and we can trust all the callers (i.e. ourselves) not to do stupid stuff (or can we? 🙃) but a good habit to get into for collection accessors.

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label May 26, 2025
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good with this change!

Added pr/do-not-merge so you can have some more talks about the details, feel free to remove the label whenever you're ready to merge.

* @internal
*/
protected get _stageVariables(): { [key: string]: string } | undefined {
return Object.keys(this.stageVariables).length > 0 ? { ...this.stageVariables } : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine we make a copy so that callers can't access x.stageVariables to get a mutable reference to the original dict, and mutate it outside addStageVariable() calls.

  • Direct access to mutable collections makes it possible to bypass validation we would put into mutator functions
  • Direct access to mutable collections makes for code that behaves differently in TypeScript vs across JSII, because in JSII there is no such thing as a reference to a mutable collection (all collections are always passed by copy across the wire)

Not strictly necessary because this is a private API and we can trust all the callers (i.e. ourselves) not to do stupid stuff (or can we? 🙃) but a good habit to get into for collection accessors.

@@ -87,4 +88,16 @@ export abstract class StageBase extends Resource implements IStage {
dimensionsMap: { ApiId: this.baseApi.apiId, Stage: this.stageName },
}).attachTo(this);
}

public addStageVariable(name: string, value: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this needs a docstring still

@@ -87,4 +88,16 @@ export abstract class StageBase extends Resource implements IStage {
dimensionsMap: { ApiId: this.baseApi.apiId, Stage: this.stageName },
}).attachTo(this);
}

public addStageVariable(name: string, value: string) {
this.stageVariables[name] = value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we had the checks mentioned in the interface doc string?

// @param name The name of the stage variable. Names can only contain alphanumeric characters and underscores.
// @param value The value of the stage variable. Values can contain letters, numbers, and the following characters: _, ., :, /, +, -, @ 

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: fadad17
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 pr/do-not-merge This PR should not be merged at this time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(api-gateway-v2): Add stage variables to HttpApi Stage
5 participants