0

sam build对我来说效果很好,除非我包含恰好被编译的 mysql2 gem。当我包含这个 gem 时,lambda 告诉我它找不到 mysql2。

看着我的.aws-sam/build/MyLambdaFunctionName/vendor/bundle/ruby/2.5.0/gems我有一个mysql2-.0.5.2目录

我也尝试在bundle install --path vendor/bundle本地上传,但它有所不同

为了在我的 AWS Ruby Lambda 函数中包含 mysql2,我该怎么做?


编辑:添加--use-containersam build导致以下情况:

2019-01-15 09:58:39 Starting Build inside a container
2019-01-15 09:58:39 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-15 09:58:39 Building resource 'MyLambdaFunctionName'
Traceback (most recent call last):
  File "/home/daver/venv/bin/sam", line 11, in <module>
    sys.exit(cli())
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/commands/build/command.py", line 94, in cli
    skip_pull_image, parameter_overrides)  # pragma: no cover
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/commands/build/command.py", line 132, in do_cli
    artifacts = builder.build()
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/lib/build/app_builder.py", line 129, in build
    lambda_function.runtime)
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/lib/build/app_builder.py", line 201, in _build_function
    runtime)
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/lib/build/app_builder.py", line 249, in _build_function_on_container
    self._container_manager.run(container)
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/local/docker/manager.py", line 75, in run
    is_image_local = self.has_image(image_name)
  File "/home/daver/venv/lib/python2.7/site-packages/samcli/local/docker/manager.py", line 153, in has_image
    self.docker_client.images.get(image_name)
  File "/home/daver/venv/lib/python2.7/site-packages/docker/models/images.py", line 312, in get
    return self.prepare_model(self.client.api.inspect_image(name))
  File "/home/daver/venv/lib/python2.7/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/docker/api/image.py", line 245, in inspect_image
    self._get(self._url("/images/{0}/json", image)), True
  File "/home/daver/venv/lib/python2.7/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/docker/api/client.py", line 215, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/home/daver/venv/lib/python2.7/site-packages/requests/sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/daver/venv/lib/python2.7/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(2, 'No such file or directory'))
Error: sam build failed

编辑 2

尝试了类似结果的 python 3.6:

2019-01-15 11:43:47 Starting Build inside a container
2019-01-15 11:43:47 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-15 11:43:47 Building resource 'MyLambdaFunctionName'
Traceback (most recent call last):
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
    sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/daver/venv/lib64/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/daver/venv/lib64/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/opt/rh/rh-python36/root/usr/lib64/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
    sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/daver/venv/bin/sam", line 11, in <module>
    sys.exit(cli())
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/commands/build/command.py", line 94, in cli
    skip_pull_image, parameter_overrides)  # pragma: no cover
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/commands/build/command.py", line 132, in do_cli
    artifacts = builder.build()
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/lib/build/app_builder.py", line 129, in build
    lambda_function.runtime)
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/lib/build/app_builder.py", line 201, in _build_function
    runtime)
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/lib/build/app_builder.py", line 249, in _build_function_on_container
    self._container_manager.run(container)
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/local/docker/manager.py", line 75, in run
    is_image_local = self.has_image(image_name)
  File "/home/daver/venv/lib64/python3.6/site-packages/samcli/local/docker/manager.py", line 153, in has_image
    self.docker_client.images.get(image_name)
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/models/images.py", line 316, in get
    return self.prepare_model(self.client.api.inspect_image(name))
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/api/image.py", line 245, in inspect_image
    self._get(self._url("/images/{0}/json", image)), True
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/docker/api/client.py", line 225, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/home/daver/venv/lib64/python3.6/site-packages/requests/sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/daver/venv/lib64/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
Error: sam build failed

编辑 3

远一点。当 docker 未运行时会发生此错误。当它运行时,我仍然得到一个错误

2019-01-15 12:15:12 Starting Build inside a container
2019-01-15 12:15:12 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-15 12:15:12 Building resource 'MyLambdaFunctionName'

Fetching lambci/lambda:build-ruby2.5 Docker container image.......................................................................................................................................................................................
2019-01-15 12:15:32 Mounting /home/daver/code/project/project3/backend/code as /tmp/samcli/source:ro inside runtime container
Build Failed
'ruby' runtime has not been validated!
Running RubyBundlerBuilder:CopySource
Running RubyBundlerBuilder:RubyBundle
Error: RubyBundlerBuilder:RubyBundle - Bundler Failed:

编辑 4

我将此失败作为错误添加到 aws-sam-cli 的 github 页面:https ://github.com/awslabs/aws-sam-cli/issues/939

4

1 回答 1

0

MySQL gem 使用 C 库。您的本地环境可能与 Lambda 不匹配,因此编译的 C 库不兼容。尝试在 docker 容器中构建它。使用 SAM,您可以sam build --use-container在类似 Lambda 的环境中运行构建。

于 2019-01-15T13:33:08.303 回答