简体   繁体   English

python 中的 lambda function 调用的代码构建项目的连接超时

[英]connection timeout to codebuild project invoked by lambda function in python

I have a simple lambda function in python that invokes the codebuild project:我在 python 中有一个简单的 lambda function 调用代码构建项目:

import json, boto3, time导入 json,boto3,时间

def handler(event, context):
    print(event)
    # execute the testsuite (by triggering a codebuild project which executes the soapui TestSuite)
    codebuild = boto3.client('codebuild')
    print ("hi")
    responseStart = codebuild.start_build(projectName="SpSoapUITest")
    print (responseStart['build']['id'])
    print (responseStart)
    # wait 180 seconds to read the result from codeBuild job
    time.sleep(180)
    
    # parse the result
    response = codebuild.batch_get_builds(
        ids=[ str(responseStart['build']['id']) ]
    )
    if response['builds'][0]['buildStatus'] == 'SUCCEEDED':
        status = "Succeeded"
    else:
        status = "Failed"
    
    # send result to event
    try:
        codedeploy = boto3.client('codedeploy')
        codedeploy.put_lifecycle_event_hook_execution_status(
            deploymentId=event["DeploymentId"],
            lifecycleEventHookExecutionId=event["LifecycleEventHookExecutionId"],
            status=status
        )
        return True
    except codedeploy.exceptions.ClientError as e:
        print("Unexpected error: %s" % e)
        return False

but it is timing out after printing hi saying the connection timeout但是在打印 hi 说连接超时后超时

[ERROR] ConnectTimeoutError: Connect timeout on endpoint URL: "https://codebuild.eu-central-1.amazonaws.com/"
Traceback (most recent call last):
  File "/var/task/spLambda.py", line 8, in handler
    responseStart = codebuild.start_build(projectName="mihir-usagemonitor-dev-SpSoapUITest")
  File "/var/runtime/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 662, in _make_api_call
    http, parsed_response = self._make_request(
  File "/var/runtime/botocore/client.py", line 682, in _make_request
    return self._endpoint.make_request(operation_model, request_dict)
  File "/var/runtime/botocore/endpoint.py", line 102, in make_request
    return self._send_request(request_dict, operation_model)
  File "/var/runtime/botocore/endpoint.py", line 136, in _send_request
    while self._needs_retry(attempts, operation_model, request_dict,
  File "/var/runtime/botocore/endpoint.py", line 253, in _needs_retry
    responses = self._event_emitter.emit(
  File "/var/runtime/botocore/hooks.py", line 356, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "/var/runtime/botocore/hooks.py", line 228, in emit
    return self._emit(event_name, kwargs)
  File "/var/runtime/botocore/hooks.py", line 211, in _emit
    response = handler(**kwargs)
  File "/var/runtime/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
  File "/var/runtime/botocore/retryhandler.py", line 250, in __call__
    should_retry = self._should_retry(attempt_number, response,
  File "/var/runtime/botocore/retryhandler.py", line 277, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/var/runtime/botocore/retryhandler.py", line 316, in __call__
    checker_response = checker(attempt_number, response,
  File "/var/runtime/botocore/retryhandler.py", line 222, in __call__
    return self._check_caught_exception(
  File "/var/runtime/botocore/retryhandler.py", line 359, in _check_caught_exception
    raise caught_exception
  File "/var/runtime/botocore/endpoint.py", line 200, in _do_get_response
    http_response = self._send(request)
  File "/var/runtime/botocore/endpoint.py", line 269, in _send
    return self.http_session.send(request)
  File "/var/runtime/botocore/httpsession.py", line 287, in send
    raise ConnectTimeoutError(endpoint_url=request.url, error=e)
END RequestId: 8e5bbd96-7373-4f5e-9f8d-adea8dc30211
REPORT RequestId: 8e5bbd96-7373-4f5e-9f8d-adea8dc30211  Duration: 305498.32 ms  Billed Duration: 305499 ms  Memory Size: 128 MB Max Memory Used: 73 MB  Init Duration: 323.83 ms

I have attached following policies to lambda role:我已将以下策略附加到 lambda 角色:

  1. AWSCodeBuildDeveloperAccess AWSCodeBuild 开发人员访问
  2. AWSCodeDeployFullAccess AWSCodeDeployFullAccess
  3. AmazonS3ReadOnlyAccess AmazonS3ReadOnlyAccess
  4. AWSLambdaVPCAccessExecutionRole AWSLambdaVPCAccessExecutionRole

What could be the reason for timout?超时的原因可能是什么?

Lambda function associated with a VPC has no internet access , unless VPC allows it.与 VPC 关联的 Lambda function无法访问 Internet ,除非 VPC 允许。 From docs :来自文档

When you connect a function to a VPC in your account, the function can't access the internet unless your VPC provides access.当您将 function 连接到您账户中的 VPC 时,除非您的 VPC 提供访问权限,否则 function将无法访问 Internet

Subsequently, your function can't connect to the public endpoints of the CodeBuild (CB) service.随后,您的 function无法连接到 CodeBuild (CB) 服务的公共端点。 To rectify this, there are two options :为了纠正这个问题,有两种选择

  • place your function in private subnet (public will not work), setup NAT gateway in a public subnet and configure route tables so that your function can access internet using NAT.将您的 function 放在私有子网中(公共将不起作用),在公共子网中设置NAT网关并配置路由表,以便您的 function 可以使用 NAT 访问互联网。 The process is explained here . 此处解释了该过程。

  • setup VPC endpoint for CB . 为 CB 设置 VPC 端点 This will allow your function to privately access CB service without the need for internet access.这将允许您的 function私下访问 CB 服务,而无需访问 Internet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM