如何使用AWS_CDK将资源参数从一个堆栈传递到另一堆栈
我正在尝试为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调试问题。必须在构造函数参数定义中编写关键字参数,该定义正在构造函数调用中传递。
hello_handler_apigateway_handler.py
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