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

如何解决python中由lambda函数调用的代码构建项目的连接超时

我在 python 中有一个简单的 lambda 函数调用 codebuild 项目:

导入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

但是打印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,File "/var/runtime/botocore/retryhandler.py",line 277,in _should_retry
    return self._checker(attempt_number,caught_exception)
  File "/var/runtime/botocore/retryhandler.py",line 316,in __call__
    checker_response = checker(attempt_number,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

我已将以下政策附加到 lambda 角色:

  1. AWSCodeBuildDeveloperAccess
  2. AWSCodeDeployFullAccess
  3. AmazonS3ReadOnlyAccess
  4. AWSLambdaVPCAccessExecutionRole

超时的原因可能是什么?

解决方法

与 VPC 关联的 Lambda 函数无法访问互联网,除非 VPC 允许。来自docs

当您将某个函数连接到您帐户中的 VPC 时,该函数无法访问互联网,除非您的 VPC 提供访问权限。

随后,您的函数无法连接到 CodeBuild (CB) 服务的公共端点。要纠正此问题,有两种选择

  • 将您的函数放在私有子网中(公共将不起作用),在公共子网中设置NAT网关并配置路由表,以便您的函数可以访问使用 NAT 上网。过程解释here

  • 设置 VPC endpoint for CB。这将允许您的函数私下访问 CB 服务,而无需访问互联网。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?