aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

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

Open gitaugakwa opened this issue 6 years ago • 17 comments

I have an HttpApi Stage that needs stage functions to call different aliases of Lambda Functions.

Also, it's supported on the RestApi Stages and should be supported for HttpApi Stages

Then also add support to stage variables for HttpApi integrations similar to #6143 but for HttpApi integrations

Use Case

I have an HttpApi Stage that needs stage functions to call different aliases of Lambda Functions.

Proposed Solution

The same way you would add stage variables to a RestApi Stage is the same way you should to an HttpApi Stage

Other

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

gitaugakwa avatar Dec 01 '20 21:12 gitaugakwa

So this really havent been done right? No wonder I didnt see the StageVariables options for me to fill in here

But in cloudformation docs, stageVariables is available as shown here

Blocked me for whole day. any solution for this?

@PoofyFoopy do you have any workaround to this??

kenchoong avatar May 26 '21 16:05 kenchoong

@kenchoong not really, my workaround was to actually go into the console and add the variables. But I hope this gets added soon

gitaugakwa avatar May 26 '21 18:05 gitaugakwa

@PoofyFoopy Then how you invoke the different Lambda Alias? Can you do that?

kenchoong avatar May 27 '21 04:05 kenchoong

Alright, I list down all the problem I face as a record here:

  1. In HttpApi contsruct, You can add_stage(..) but you cant add stageVariables into the stage

  2. Then add_routes() method, integration is accepting IHttpRouteIntegration object as parameter, therefore you need to use one of the class inside aws_apigatewayv2_integrations which will cause the following problem:

  • When using LambdaProxyIntegration, it can only accept IFunction as the handler.
  • That's means you cant invoke the LambdaAlias like myFunction:dev or myFunction:prod, it only accept myFunction
  • So thats means no matter what stage whether is dev, prod, beta, it only will invoke myFunction only, all go to $Latest version of the function.
  • Thought to use HttpProxyIntegration , so I can invoke the lambda like this(as stated here):
# invoke the lambda function via a URI, with POST method
api_uri = (
            f"arn:aws:apigateway:"
            f"{cdk.Aws.REGION}"
            f":lambda:path/2015-03-31/functions/"
            f"{function.function_arn}"
            f":"
            f"${{stageVariables.lambdaAlias}}" 
            f"/invocations"
        )

No you cant do that, cause HttpProxyIntegration only accept a Http url. Not this Lambda Uri.

  • Then I tot to use HttpIntegration for the route integration. No, you cant do that as well, cause remember? addRoutes() only accept IHttpRouteIntegration, but HttpIntegration is IHttpIntegration, is 2 different thing.

Beside, HttpIntegration is the integration for the whole HttpApi, which all the different route, also will use the same Integration,this is what I tried:

     # try to invoke different lambdaAlias 
      stage_lambda_id = function.node.id + "LambdaStageIntegration"
         stage_function_arn = (
            f"{function.function_arn}"
            f":"
            f"${{stageVariables.lambdaAlias}}"
         )
      
       # then wanna use it in the HttpIntegration
        my_integration = apigateway.HttpIntegration(
            self, stage_lambda_id,
            http_api=api,  # this is mandatory, will plug in the whole API to use this integration, so all go to same `lambda`
            integration_type=apigateway.HttpIntegrationType.LAMBDA_PROXY,
            integration_uri=stage_function_arn
        )
        
       # but the integration,cant use it per route, only for whole API 
       user_route_list = api.add_routes(
            path="/user",
            methods=api_methods_list,
            authorizer=some_authorizer,
            integration=my_integration # u cant use it here, because it only accept IHttpRouteIntegration
        )

Alright, all this is what I tried, conclusion is:

  • HttpApi cant add in StageVariables into each stage
  • HttpApi cant invoke different Lambda Alias according to stageVariable for each stage
  • HttpApi only can 1 route invoke 1 Lambda Version which is $Latest (cause it cant invoke different alias, therefore only $Latest)

1 thing I like about HttpApi is: The build in JWT authorizer. This is very good! Save me a lot of time from writing it myself.

But look like it scarified all the other benefit of RestAPI.

Just my little opinion, I hope this can fix soon. I need to move on from this now

kenchoong avatar May 27 '21 08:05 kenchoong

Any news about this feature please? Thank you!

picosam avatar Jan 18 '22 21:01 picosam

We also would very much like to utilise this feature!

hermanbanken avatar Jan 29 '22 17:01 hermanbanken

Can the CDK seriously not create stage variables for a HTTP api? We want to set a stage variable (on our default/only stage) for a "JWT Audience" that a common lambda request authorizer uses, as a parameter. (and no, your builtin jwt authorizer won't work for us, it doesn't support Cloudflare Zero Trust).

e.g. httpapi.defaultStage.stagevariables["audience"] = "123456789"

our workaround right now is to add this manually in the web console.

kjpgit avatar Apr 11 '22 16:04 kjpgit

Hi there, is there anything that can be done to help prioritize this feature?

My team has just run into this obstacle. I see that this has been open for 2 years and that someone marked it as "low effort". We could really use it. Thanks!

phitoduck avatar Jul 28 '22 02:07 phitoduck

I've just come across this same issue.

martsbradley avatar Sep 23 '22 12:09 martsbradley

It's been three years, when can I use stage variables in the integration uri? Is there any alternative way to do so?

samchan0221 avatar May 15 '23 04:05 samchan0221

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

github-actions[bot] avatar Jul 16 '23 00:07 github-actions[bot]

Same issue I ran into, wasted a good few hours until I stumbled on this. Any updates will help.

Quick overview solutions:

  • custom cloud formation escape hatch to setup the url
  • manually create those stage variables via console or aws cli script
  • use different gateways per env.

lunchboxsushi avatar Aug 03 '23 02:08 lunchboxsushi

Any update on this issue? I still cannot see the option to add stage variables

pankajan avatar Jun 12 '24 13:06 pankajan

This issue still hasnt been fixed, have to create a CfnStage and CfnApiMapping in order to achieve the same result.

mbsff avatar Nov 28 '24 13:11 mbsff

After 4 years, it is not natively supported yet?

mehrdad-shokri avatar Feb 25 '25 12:02 mehrdad-shokri

has this been implemented yet?

jarrodlilkendey avatar Apr 29 '25 04:04 jarrodlilkendey

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

github-actions[bot] avatar Jul 04 '25 09:07 github-actions[bot]