AWSCDK:GetHttpRouteofgivenapidefinedinanotherstack
创始人
2024-09-24 01:30:47
0

要在 AWS CDK 中获取在另一个栈中定义的 API 的 HttpRoute,可以使用 StackReference 和 Fn.importValue 方法。以下是示例代码:

首先,在要导入 HttpRoute 的栈中,将其输出为导出值:

from aws_cdk import (
    core,
    aws_apigatewayv2 as apigw
)

class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        apigw.HttpApi(self, "MyHttpApi")  # Example HttpApi

        apigw_output = core.CfnOutput(self, "MyHttpApiOutput", value=my_api.http_api_id)
        apigw_output.export_name = "MyHttpApiId"

接下来,在另一个栈中导入该输出值,然后使用 StackReference 和 Fn.importValue 方法获取 HttpRoute:

from aws_cdk import (
    core,
    aws_apigatewayv2 as apigw,
    aws_cloudformation as cfn
)

class MyOtherStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        http_api_id = cfn.Fn.import_value("MyHttpApiId")

        http_api_stack = cfn.StackReference(self, "MyHttpApiStack", stack_name="MyHttpApiStack")

        http_api = apigw.HttpApi.from_api_id(self, "MyHttpApi", http_api_id, http_api_stack)

        http_api_route = http_api.get_route("MyRoute")  # Example route name

在代码中,我们首先从 CloudFormation 中导入输出值 "MyHttpApiId",它是在 MyStack 中定义的 HttpApi 的 ID。然后,我们使用 StackReference 和该 ID 获取 HttpApi。最后,我们使用 HttpApi 的 get_route 方法获取所需的 HttpRoute。

相关内容

热门资讯

技术分享!微乐自建房辅助工具脚... 技术分享!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)器1、下载好微乐家乡麻将...
重大来袭!微乐小程序麻将自建房... 重大来袭!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)脚本运微乐小程...
玩家必看科普!微乐自建房辅助工... 玩家必看科普!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)技巧1)微乐家乡麻将自...
必看攻略!微乐家乡麻将自建房辅... 必看攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本小薇(辅助器软...
科技新动态!微乐小程序麻将自建... 科技新动态!微乐小程序麻将自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)方法1)微乐小程序...
玩家必看分享!微乐自建房辅助工... 玩家必看分享!微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)技巧亲,关键说明,微乐自建房辅...
重大推荐!微乐家乡麻将自建房辅... 重大推荐!微乐家乡麻将自建房辅助app,微乐小程序免费黑科技(开挂自建房)app1、点击下载安装,微...
科普攻略!微乐家乡麻将自建房辅... 科普攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本1、不需要AI...
揭秘几款!微乐微信自建房辅助工... 揭秘几款!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)app1、进入游戏-大厅左侧...
今日头条!微乐自建房辅助工具神... 今日头条!微乐自建房辅助工具神器,微乐家乡麻将自建房辅助app(开挂自建房)工具1、首先打开微乐家乡...