-1

I have a simple lambda function in python that invokes the codebuild project:

import json, boto3, time

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

[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:

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

What could be the reason for timout ?

MiGo
  • 137
  • 8

1 Answers1

1

Lambda function associated with a VPC has no internet access, unless VPC allows it. 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.

Subsequently, your function can't connect to the public endpoints of the CodeBuild (CB) service. 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. The process is explained here.

  • setup VPC endpoint for CB. This will allow your function to privately access CB service without the need for internet access.

Marcin
  • 108,294
  • 7
  • 83
  • 138