如何解决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 角色:
- AWSCodeBuildDeveloperAccess
- AWSCodeDeployFullAccess
- AmazonS3ReadOnlyAccess
- AWSLambdaVPCAccessExecutionRole
超时的原因可能是什么?
解决方法
与 VPC 关联的 Lambda 函数无法访问互联网,除非 VPC 允许。来自docs:
当您将某个函数连接到您帐户中的 VPC 时,该函数无法访问互联网,除非您的 VPC 提供访问权限。
随后,您的函数无法连接到 CodeBuild (CB) 服务的公共端点。要纠正此问题,有两种选择:
-
将您的函数放在私有子网中(公共将不起作用),在公共子网中设置NAT网关并配置路由表,以便您的函数可以访问使用 NAT 上网。过程解释here。
-
设置 VPC endpoint for CB。这将允许您的函数私下访问 CB 服务,而无需访问互联网。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。