簡體   English   中英

AWS CDK API 網關構建庫

[英]AWS CDK API Gateway Construct Library

我正在嘗試使用 python 的 AWS_CDK 來配置 apigateway 集成。 https://pypi.org/project/aws-cdk.aws-apigateway/1.4.0/上的 typescript 很有幫助,https 上未經檢查的 python 翻譯也是如此://docs.aws.amazon.com/cdk/api /latest/python/aws_cdk.aws_apigateway.README.html但不太正確。

我試圖讓 python 版本正確,但在從 TS 到 python 的翻譯中我仍然遺漏了一些東西。目前我的代碼片段是;

getRisksIntegration = apigw.LambdaIntegration(self.getRisksFunction, proxy = False, 
        integration_responses=[{
        # Successful response from the Lambda function, no filter defined
        #  - the selectionPattern filter only tests the error message
        # We will set the response status code to 200
        "statusCode": "200",
        "response_templates": {
            # This template takes the "message" result from the Lambda function, adn embeds it in a JSON response
            # Check https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
            "application/json": "JSON.stringify(state='ok', greeting='$util.escapeJavaScript($input.body)')"
        },
        "response_parameters": {
            # We can map response parameters
            # - Destination parameters (the key) are the response parameters (used in mappings)
            # - Source parameters (the value) are the integration response parameters or expressions
            "method.response.header._content-_type": "'application/json'",
            "method.response.header._access-_control-_allow-_origin": "'*'",
            "method.response.header._access-_control-_allow-_credentials": "'true'"
        }
    }, {
        # For errors, we check if the error message is not empty, get the error data
        "selection_pattern": "(|.)+",
        # We will set the response status code to 200
        "status_code": "400",
        "response_templates": {
            "application/json": "JSON.stringify(state='error', message='$util.escapeJavaScript($input.path('$.errorMessage'))')"
        },
        "response_parameters": {
            "method.response.header._content-_type": "'application/json'",
            "method.response.header._access-_control-_allow-_origin": "'*'",
            "method.response.header._access-_control-_allow-_credentials": "'true'"
        }
    }
    ]);

返回

jsii.errors.JavaScriptError: 
  Error: Missing required properties for @aws-cdk/aws-apigateway.IntegrationResponse: statusCode
      at validateRequiredProps (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7228:15)
      at Object.deserialize (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6900:21)
      at Kernel._toSandbox (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:8222:61)
      at value.map.x (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6796:40)
      at Array.map (<anonymous>)
      at Object.deserialize (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6796:26)
      at Kernel._toSandbox (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:8222:61)
      at mapValues (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6906:29)
      at mapValues (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7167:27)
      at Kernel._wrapSandboxCode (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:8316:19)
      at Kernel._create (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:26)
      at Kernel.create (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7600:21)
      at KernelHost.processRequest (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7388:28)
      at KernelHost.run (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7328:14)
      at Immediate.setImmediate [as _onImmediate] (/home/ec2-user/environment/thoth/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7331:37)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 41, in <module>
    gremlin_layer = layer_stack.gremlin_python_layer,
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/home/ec2-user/environment/thoth/stacks/api_stack.py", line 110, in __init__
    "method.response.header._access-_control-_allow-_credentials": "'true'"
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/aws_cdk/aws_apigateway/__init__.py", line 17765, in __init__
    jsii.create(LambdaIntegration, self, [handler, options])
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/jsii/_kernel/__init__.py", line 229, in create
    interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])],
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/jsii/_kernel/providers/process.py", line 333, in create
    return self._process.send(request, CreateResponse)
  File "/home/ec2-user/environment/thoth/.env/lib64/python3.6/dist-packages/jsii/_kernel/providers/process.py", line 318, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Missing required properties for @aws-cdk/aws-apigateway.IntegrationResponse: statusCode
Subprocess exited with error 1

非常感謝任何幫助。

因此,經過大量研究,問題在於從 TS 到 Python 的翻譯。 重要的是要記住參數,例如 integration_response 的值,它仍然是 TS。 我發現

我還發現添加 api_gateway 方法響應也有點不直觀,所以在下面的工作示例中我已經包含了它。

        getRisksIntegration = apigw.LambdaIntegration(self.getRisksFunction, 
            proxy = False,
            # request_parameters = {},
            # allow_test_invoke = True,
            # request_templates = {},
            integration_responses=[{
                # Successful response from the Lambda function, no filter defined
                #  - the selectionPattern filter only tests the error message
                # We will set the response status code to 200
                "statusCode": "200",
                "contentHandling": "Passthrough",
                "responseTemplates": {
                    # This template takes the "message" result from the Lambda function, and embeds it in a JSON response
                    # Check https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
                    # "application/json": "JSON.stringify(state='ok', greeting='$util.escapeJavaScript($input.body)')"
                    },
                "responseParameters": {
                    # We can map response parameters
                    # - Destination parameters (the key) are the response parameters (used in mappings)
                    # - Source parameters (the value) are the integration response parameters or expressions
                    "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'",
                    "method.response.header.Access-Control-Allow-Methods": "'*'",
                    "method.response.header.Access-Control-Allow-Origin": "'*'"
                    }

                },{
                # For errors, we check if the error message is not empty, get the error data
                # "selection_pattern": "(|.)+",
                # We will set the response status code to 200
                "statusCode": "400",
                "responseTemplates": {
                    # "application/json": "JSON.stringify(state='error', message='$util.escapeJavaScript($input.path('$.errorMessage'))')"
                    },
                "responseParameters": {
                    "method.response.header._content-_type": "'application/json'",
                    "method.response.header._access-_control-_allow-_origin": "'*'",
                    "method.response.header._access-_control-_allow-_credentials": "'true'"
                    }
                }],

        );
        getRisks.add_method('GET', getRisksIntegration,
        method_responses = [{
            "statusCode": "200",
            "responseParameters": {
                "method.response.header.Access-Control-Allow-Headers": True,
                "method.response.header.Access-Control-Allow-Methods": True,
                "method.response.header.Access-Control-Allow-Origin": True
            },
        }]
        );

您是否嘗試過傳入IntegrationResponse object? 即喜歡:

...
integration_responses = [
    apigw.IntegrationResponse(
        status_code = "200",
        response_templates = {}
        response_parameters = {
            "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'",
            "method.response.header.Access-Control-Allow-Methods": "'*'",
            "method.response.header.Access-Control-Allow-Origin": "'*'"
        }
    ...
]
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM