我正在尝试构建一个 cdk 堆栈来通过 api 网关将 lambda 函数连接到 React 静态网站。
from aws_cdk import (
Stack,
aws_lambda as _lambda,
# aws_s3_notifications as s3_notify,
aws_s3 as s3,
aws_iam as iam,
aws_s3_deployment as s3_deployment,
)
from aws_cdk.aws_apigatewayv2 import HttpApi, HttpMethod
from aws_cdk.aws_apigatewayv2_integrations import (
HttpLambdaIntegration,
)
# Create the Lambda function
lambda_function = _lambda.Function(
self,
id="MtLambdaFunction",
runtime=_lambda.Runtime.PYTHON_3_8,
handler="main.handler", # Replace with your actual handler
code=_lambda.Code.from_asset("./Backend/main.zip"),
# Add other Lambda function configurations
)
# Grant the Lambda function permission to read from the S3 bucket
models_bucket.grant_read(lambda_function) #
# create the api gateway
react_ui_integration = HttpLambdaIntegration(
id="ReactIntegration", handler=lambda_function
)
http_api = HttpApi(self, "ReactHttpApi")
http_api.add_routes(
path="/translate",
methods=[HttpMethod.ANY],
integration=react_ui_integration,
)
我从文档获取了api网关的代码示例。
导入时收到以下消息
aws_apigatewayv2_integrations
Import "aws_cdk.aws_apigatewayv2_integrations" could not be resolved
我安装了该软件包,因此收到一条消息错误,指出该构建包已从 10.3.0 降级至 3.4.344
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behavior is the source of the following dependency conflicts.
aws-cdk-lib 2.110.0 requires constructs<11.0.0,>=10.0.0, but you have constructs 3.4.344 which is incompatible.
顺便说一句,我使用的是虚拟环境并安装了以下软件包:
aws-cdk-lib==2.110.0
constructs>=10.0.0,<11.0.0
那么如何直接从 aws-cdk-lib 使用
aws_apigatewayv2_integrations
而无需手动安装呢?
我选择了 v2 版本,这样我就可以使用 HTTP Api。您认为这是最好的选择吗?或者,最好使用 v1 aws_apigateway
。
如果您在部署此网关以链接 React 静态应用程序和 lambda 函数时需要考虑任何其他事项,请告诉我,因为这是我的第一个项目
cdk
升级
aws-cdk-lib
到最新版本:
aws-cdk-lib==2.114.1
constructs>=10.0.0,<11.0.0