如何使用AWS_CDK将资源参数从一个堆栈传递到另一堆栈

发布于 2025-02-09 03:09:30 字数 1829 浏览 0 评论 0原文

我正在尝试为2个资源创建两个单独的堆栈,即lambda功能和API网关。以下是我的app.py.py

import aws_cdk as cdk
from Stack.lambda_function.hello_handler_stack import HelloHandlerStack
from Stack.apigateway.hello_handler_apigateway_handler import HelloHandlerApigatewayStack
app = cdk.App()

Stack1 = HelloHandlerStack(app, "HelloHandlerStack", env=cdk.Environment(Account="**",Region="*"))

Stack2 = HelloHandlerApigatewayStack(app, "HelloHandlerApigatewayStack", env=cdk.Environment(Account="**",Region="*"), temp_lambda = Stack1.my_lambda)

app.synth()

hello_handler_stack.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
)

class HelloHandlerStack(Stack):

    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        my_lambda = _lambda.Function(
            self, 'HelloHandler',
            runtime=_lambda.Runtime.PYTHON_3_7,
            code=_lambda.Code.from_asset('lambda'),
            handler='hello.handler',
        )
        self.my_lambda = my_lambda

hello_handler_handler_apigateway_handler.py hello_handler_stack.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
    aws_apigateway as apigw,
)


class HelloHandlerApigatewayStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        apigw.LambdaRestApi(
          self, 'Endpoint',
          handler=kwargs["test_lambda"],
        )

typeerror:stack。 init ()有一个意外的关键字参数 'test_lambda'

我应该能够将关键字参数传递给第二个堆栈并使用它来构建API网关,在调用类时,我还应提供其他参考文献吗?

任何帮助都将受到赞赏。

I'm trying to create two separate stacks for 2 resources namely lambda function and API gateway. Below is the code from my app.py

import aws_cdk as cdk
from Stack.lambda_function.hello_handler_stack import HelloHandlerStack
from Stack.apigateway.hello_handler_apigateway_handler import HelloHandlerApigatewayStack
app = cdk.App()

Stack1 = HelloHandlerStack(app, "HelloHandlerStack", env=cdk.Environment(Account="**",Region="*"))

Stack2 = HelloHandlerApigatewayStack(app, "HelloHandlerApigatewayStack", env=cdk.Environment(Account="**",Region="*"), temp_lambda = Stack1.my_lambda)

app.synth()

hello_handler_stack.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
)

class HelloHandlerStack(Stack):

    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        my_lambda = _lambda.Function(
            self, 'HelloHandler',
            runtime=_lambda.Runtime.PYTHON_3_7,
            code=_lambda.Code.from_asset('lambda'),
            handler='hello.handler',
        )
        self.my_lambda = my_lambda

hello_handler_apigateway_handler.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
    aws_apigateway as apigw,
)


class HelloHandlerApigatewayStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        apigw.LambdaRestApi(
          self, 'Endpoint',
          handler=kwargs["test_lambda"],
        )

But when I do cdk ls, I get an error stating

TypeError: Stack.init() got an unexpected keyword argument
'test_lambda'

I should be able to pass the keyword argument to the second stack and use it to build the api gateway, is there any other references I should provide while calling the classes?

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2025-02-16 03:09:30

调试问题。必须在构造函数参数定义中编写关键字参数,该定义正在构造函数调用中传递。

hello_handler_apigateway_handler.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
    aws_apigateway as apigw,
)


class HelloHandlerApigatewayStack(Stack):
    def __init__(self, scope: Construct, id: str, test_lambda, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
    apigw.LambdaRestApi(
      self, 'Endpoint',
      handler=test_lambda,
    )

Debugged the problem. Had to write the keyword argument in the constructor argument definition that is being passed inside the constructor call.

hello_handler_apigateway_handler.py

from constructs import Construct
from aws_cdk import (
    Stack,
    aws_lambda as _lambda,
    aws_apigateway as apigw,
)


class HelloHandlerApigatewayStack(Stack):
    def __init__(self, scope: Construct, id: str, test_lambda, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
    apigw.LambdaRestApi(
      self, 'Endpoint',
      handler=test_lambda,
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文