aws_lambda_ric_jets 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +10 -0
- data/LICENSE +229 -0
- data/NOTICE +1 -0
- data/README.md +164 -0
- data/aws_lambda_ric.gemspec +45 -0
- data/bin/aws_lambda_ric +10 -0
- data/lib/aws_lambda_ric/aws_lambda_marshaller.rb +37 -0
- data/lib/aws_lambda_ric/bootstrap.rb +45 -0
- data/lib/aws_lambda_ric/lambda_context.rb +29 -0
- data/lib/aws_lambda_ric/lambda_errors.rb +85 -0
- data/lib/aws_lambda_ric/lambda_handler.rb +43 -0
- data/lib/aws_lambda_ric/lambda_log_formatter.rb +14 -0
- data/lib/aws_lambda_ric/lambda_logger.rb +10 -0
- data/lib/aws_lambda_ric/lambda_server.rb +89 -0
- data/lib/aws_lambda_ric/logger_patch.rb +21 -0
- data/lib/aws_lambda_ric/telemetry_log_sink.rb +43 -0
- data/lib/aws_lambda_ric/version.rb +7 -0
- data/lib/aws_lambda_ric/xray_cause.rb +42 -0
- data/lib/aws_lambda_ric.rb +179 -0
- data/lib/aws_lambda_ric_jets/version.rb +4 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 419c518036274ceb445e3b66e67a55331959f60c622481f420fcce305fee35fe
|
4
|
+
data.tar.gz: e99bad60222c55e767fc363adcce81d4693c327cd3f6b742367e4bb08e872755
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69ee4fe58a0f9ab23a7777e83c2ffc035a034a808e6ad54a44164075e8b32ba70a94ec171aed199d11e0a402d968e2dfa15c3ff017b80212b4ed7d90b6663495
|
7
|
+
data.tar.gz: 1bb742a4bf392d32e0180487d770e3a0ec8f1769ad10641d850c85213d73db3c1467c1756ebc2bdee23b76e93a1fe6e47140697dcac9d5d4eca337ebd7c53223
|
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
source 'https://rubygems.org'
|
6
|
+
|
7
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
8
|
+
|
9
|
+
# Specify your gem's dependencies in aws_lambda_ric.gemspec
|
10
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
Apache License
|
2
|
+
|
3
|
+
Version 2.0, January 2004
|
4
|
+
|
5
|
+
http://www.apache.org/licenses/ TERMS
|
6
|
+
AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
14
|
+
distribution as defined by Sections 1 through 9 of this document.
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
20
|
+
owner that is granting the License.
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
"Legal Entity" shall mean the
|
25
|
+
union of the acting entity and all other entities that control, are controlled
|
26
|
+
by, or are under common control with that entity. For the purposes of this
|
27
|
+
definition, "control" means (i) the power, direct or indirect, to cause the
|
28
|
+
direction or management of such entity, whether by contract or otherwise, or (ii)
|
29
|
+
ownership of fifty percent (50%) or more of the outstanding shares, or (iii)
|
30
|
+
beneficial ownership of such entity.
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
"You" (or "Your") shall mean
|
35
|
+
an individual or Legal Entity exercising permissions granted by this License.
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
"Source" form shall mean the preferred form for making modifications,
|
41
|
+
including but not limited to software source code, documentation source, and
|
42
|
+
configuration files.
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
"Object" form shall mean any form resulting
|
47
|
+
from mechanical transformation or translation of a Source form, including but not
|
48
|
+
limited to compiled object code, generated documentation, and conversions to
|
49
|
+
other media types.
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
"Work" shall mean the work of authorship,
|
54
|
+
whether in Source or Object form, made available under the License, as indicated
|
55
|
+
by a copyright notice that is included in or attached to the work (an example is
|
56
|
+
provided in the Appendix below).
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
"Derivative Works" shall mean any
|
61
|
+
work, whether in Source or Object form, that is based on (or derived from) the
|
62
|
+
Work and for which the editorial revisions, annotations, elaborations, or other
|
63
|
+
modifications represent, as a whole, an original work of authorship. For the
|
64
|
+
purposes of this License, Derivative Works shall not include works that remain
|
65
|
+
separable from, or merely link (or bind by name) to the interfaces of, the Work
|
66
|
+
and Derivative Works thereof.
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
"Contribution" shall mean any work
|
71
|
+
of authorship, including the original version of the Work and any modifications
|
72
|
+
or additions to that Work or Derivative Works thereof, that is intentionally
|
73
|
+
submitted to Licensor for inclusion in the Work by the copyright owner or by an
|
74
|
+
individual or Legal Entity authorized to submit on behalf of the copyright owner.
|
75
|
+
For the purposes of this definition, "submitted" means any form of electronic,
|
76
|
+
verbal, or written communication sent to the Licensor or its representatives,
|
77
|
+
including but not limited to communication on electronic mailing lists, source
|
78
|
+
code control systems, and issue tracking systems that are managed by, or on
|
79
|
+
behalf of, the Licensor for the purpose of discussing and improving the Work, but
|
80
|
+
excluding communication that is conspicuously marked or otherwise designated in
|
81
|
+
writing by the copyright owner as "Not a Contribution."
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of
|
87
|
+
whom a Contribution has been received by Licensor and subsequently incorporated
|
88
|
+
within the Work.
|
89
|
+
|
90
|
+
2. Grant of Copyright License. Subject to the terms and
|
91
|
+
conditions of this License, each Contributor hereby grants to You a perpetual,
|
92
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license
|
93
|
+
to reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
94
|
+
sublicense, and distribute the Work and such Derivative Works in Source or Object
|
95
|
+
form.
|
96
|
+
|
97
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
98
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
99
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
100
|
+
section) patent license to make, have made, use, offer to sell, sell, import, and
|
101
|
+
otherwise transfer the Work, where such license applies only to those patent
|
102
|
+
claims licensable by such Contributor that are necessarily infringed by their
|
103
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
104
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
105
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
106
|
+
alleging that the Work or a Contribution incorporated within the Work constitutes
|
107
|
+
direct or contributory patent infringement, then any patent licenses granted to
|
108
|
+
You under this License for that Work shall terminate as of the date such
|
109
|
+
litigation is filed.
|
110
|
+
|
111
|
+
4. Redistribution. You may reproduce and distribute
|
112
|
+
copies of the Work or Derivative Works thereof in any medium, with or without
|
113
|
+
modifications, and in Source or Object form, provided that You meet the following
|
114
|
+
conditions:
|
115
|
+
|
116
|
+
(a) You must give any other recipients of the Work or
|
117
|
+
Derivative Works a copy of this License; and
|
118
|
+
|
119
|
+
(b) You must cause any
|
120
|
+
modified files to carry prominent notices stating that You changed the files;
|
121
|
+
and
|
122
|
+
|
123
|
+
(c) You must retain, in the Source form of any Derivative Works that
|
124
|
+
You distribute, all copyright, patent, trademark, and attribution notices from
|
125
|
+
the Source form of the Work, excluding those notices that do not pertain to any
|
126
|
+
part of the Derivative Works; and
|
127
|
+
|
128
|
+
(d) If the Work includes a "NOTICE" text
|
129
|
+
file as part of its distribution, then any Derivative Works that You distribute
|
130
|
+
must include a readable copy of the attribution notices contained within such
|
131
|
+
NOTICE file, excluding those notices that do not pertain to any part of the
|
132
|
+
Derivative Works, in at least one of the following places: within a NOTICE text
|
133
|
+
file distributed as part of the Derivative Works; within the Source form or
|
134
|
+
documentation, if provided along with the Derivative Works; or, within a display
|
135
|
+
generated by the Derivative Works, if and wherever such third-party notices
|
136
|
+
normally appear. The contents of the NOTICE file are for informational purposes
|
137
|
+
only and do not modify the License. You may add Your own attribution notices
|
138
|
+
within Derivative Works that You distribute, alongside or as an addendum to the
|
139
|
+
NOTICE text from the Work, provided that such additional attribution notices
|
140
|
+
cannot be construed as modifying the License.
|
141
|
+
|
142
|
+
You may add Your own
|
143
|
+
copyright statement to Your modifications and may provide additional or different
|
144
|
+
license terms and conditions for use, reproduction, or distribution of Your
|
145
|
+
modifications, or for any such Derivative Works as a whole, provided Your use,
|
146
|
+
reproduction, and distribution of the Work otherwise complies with the conditions
|
147
|
+
stated in this License.
|
148
|
+
|
149
|
+
5. Submission of Contributions. Unless You explicitly
|
150
|
+
state otherwise, any Contribution intentionally submitted for inclusion in the
|
151
|
+
Work by You to the Licensor shall be under the terms and conditions of this
|
152
|
+
License, without any additional terms or conditions. Notwithstanding the above,
|
153
|
+
nothing herein shall supersede or modify the terms of any separate license
|
154
|
+
agreement you may have executed with Licensor regarding such Contributions.
|
155
|
+
|
156
|
+
|
157
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
158
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
159
|
+
for reasonable and customary use in describing the origin of the Work and
|
160
|
+
reproducing the content of the NOTICE file.
|
161
|
+
|
162
|
+
7. Disclaimer of Warranty. Unless
|
163
|
+
required by applicable law or agreed to in writing, Licensor provides the Work
|
164
|
+
(and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT
|
165
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including,
|
166
|
+
without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
|
167
|
+
MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible
|
168
|
+
for determining the appropriateness of using or redistributing the Work and
|
169
|
+
assume any risks associated with Your exercise of permissions under this
|
170
|
+
License.
|
171
|
+
|
172
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
173
|
+
whether in tort (including negligence), contract, or otherwise, unless required
|
174
|
+
by applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
175
|
+
writing, shall any Contributor be liable to You for damages, including any
|
176
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
177
|
+
arising as a result of this License or out of the use or inability to use the
|
178
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
179
|
+
computer failure or malfunction, or any and all other commercial damages or
|
180
|
+
losses), even if such Contributor has been advised of the possibility of such
|
181
|
+
damages.
|
182
|
+
|
183
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
184
|
+
the Work or Derivative Works thereof, You may choose to offer, and charge a fee
|
185
|
+
for, acceptance of support, warranty, indemnity, or other liability obligations
|
186
|
+
and/or rights consistent with this License. However, in accepting such
|
187
|
+
obligations, You may act only on Your own behalf and on Your sole responsibility,
|
188
|
+
not on behalf of any other Contributor, and only if You agree to indemnify,
|
189
|
+
defend, and hold each Contributor harmless for any liability incurred by, or
|
190
|
+
claims asserted against, such Contributor by reason of your accepting any such
|
191
|
+
warranty or additional liability. END OF TERMS AND CONDITIONS
|
192
|
+
|
193
|
+
APPENDIX: How to
|
194
|
+
apply the Apache License to your work.
|
195
|
+
|
196
|
+
To apply the Apache License to your work,
|
197
|
+
attach the following boilerplate notice, with the fields enclosed by brackets
|
198
|
+
"[]" replaced with your own identifying information. (Don't include the
|
199
|
+
brackets!) The text should be enclosed in the appropriate comment syntax for the
|
200
|
+
file format. We also recommend that a file or class name and description of
|
201
|
+
purpose be included on the same "printed page" as the copyright notice for easier
|
202
|
+
identification within third-party archives.
|
203
|
+
|
204
|
+
Copyright [yyyy] Edgar
|
205
|
+
Hernandez
|
206
|
+
|
207
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
208
|
+
|
209
|
+
you
|
210
|
+
may not use this file except in compliance with the License.
|
211
|
+
|
212
|
+
You may obtain a
|
213
|
+
copy of the License at
|
214
|
+
|
215
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
216
|
+
|
217
|
+
Unless
|
218
|
+
required by applicable law or agreed to in writing, software
|
219
|
+
|
220
|
+
distributed under
|
221
|
+
the License is distributed on an "AS IS" BASIS,
|
222
|
+
|
223
|
+
WITHOUT WARRANTIES OR CONDITIONS
|
224
|
+
OF ANY KIND, either express or implied.
|
225
|
+
|
226
|
+
See the License for the specific
|
227
|
+
language governing permissions and
|
228
|
+
|
229
|
+
limitations under the License.
|
data/NOTICE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
data/README.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
## AWS Lambda Ruby Runtime Interface Client
|
2
|
+
|
3
|
+
For the reason for this fork, see: [README.jets.md](/README.jets.md)
|
4
|
+
|
5
|
+
We have open-sourced a set of software packages, Runtime Interface Clients (RIC), that implement the Lambda
|
6
|
+
[Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html), allowing you to seamlessly extend your preferred
|
7
|
+
base images to be Lambda compatible.
|
8
|
+
The Lambda Runtime Interface Client is a lightweight interface that allows your runtime to receive requests from and send requests to the Lambda service.
|
9
|
+
|
10
|
+
The Lambda Ruby Runtime Interface Client is vended through [rubygems](https://rubygems.org/gems/aws_lambda_ric).
|
11
|
+
You can include this package in your preferred base image to make that base image Lambda compatible.
|
12
|
+
|
13
|
+
## Requirements
|
14
|
+
The Ruby Runtime Interface Client package currently supports Ruby versions:
|
15
|
+
- 2.5.x up to and including 2.7.x
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Creating a Docker Image for Lambda with the Runtime Interface Client
|
20
|
+
First step is to choose the base image to be used. The supported Linux OS distributions are:
|
21
|
+
|
22
|
+
- Amazon Linux 2
|
23
|
+
- Alpine
|
24
|
+
- CentOS
|
25
|
+
- Debian
|
26
|
+
- Ubuntu
|
27
|
+
|
28
|
+
In order to install the Runtime Interface Client, either add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'aws_lambda_ric'
|
32
|
+
```
|
33
|
+
|
34
|
+
And then execute:
|
35
|
+
|
36
|
+
$ bundle
|
37
|
+
|
38
|
+
Or install it manually as:
|
39
|
+
|
40
|
+
$ gem install aws_lambda_ric
|
41
|
+
|
42
|
+
|
43
|
+
Next step would be to copy your Lambda function code into the image's working directory.
|
44
|
+
```dockerfile
|
45
|
+
# Copy function code
|
46
|
+
RUN mkdir -p ${FUNCTION_DIR}
|
47
|
+
COPY app.rb ${FUNCTION_DIR}
|
48
|
+
|
49
|
+
WORKDIR ${FUNCTION_DIR}
|
50
|
+
```
|
51
|
+
|
52
|
+
The next step would be to set the `ENTRYPOINT` property of the Docker image to invoke the Runtime Interface Client and then set the `CMD` argument to specify the desired handler.
|
53
|
+
|
54
|
+
Example Dockerfile:
|
55
|
+
```dockerfile
|
56
|
+
FROM amazonlinux:latest
|
57
|
+
|
58
|
+
# Define custom function directory
|
59
|
+
ARG FUNCTION_DIR="/function"
|
60
|
+
|
61
|
+
# Install ruby
|
62
|
+
RUN amazon-linux-extras install -y ruby2.6
|
63
|
+
|
64
|
+
# Install bundler
|
65
|
+
RUN gem install bundler
|
66
|
+
|
67
|
+
# Install the Runtime Interface Client
|
68
|
+
RUN gem install aws_lambda_ric
|
69
|
+
|
70
|
+
# Copy function code
|
71
|
+
RUN mkdir -p ${FUNCTION_DIR}
|
72
|
+
COPY app.rb ${FUNCTION_DIR}
|
73
|
+
|
74
|
+
WORKDIR ${FUNCTION_DIR}
|
75
|
+
|
76
|
+
ENTRYPOINT ["/usr/local/bin/aws_lambda_ric"]
|
77
|
+
CMD ["app.App::Handler.process"]
|
78
|
+
```
|
79
|
+
|
80
|
+
Example Ruby handler `app.rb`:
|
81
|
+
```ruby
|
82
|
+
module App
|
83
|
+
class Handler
|
84
|
+
def self.process(event:, context:)
|
85
|
+
"Hello World!"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### Local Testing
|
92
|
+
|
93
|
+
To make it easy to locally test Lambda functions packaged as container images we open-sourced a lightweight web-server, Lambda Runtime Interface Emulator (RIE), which allows your function packaged as a container image to accept HTTP requests. You can install the [AWS Lambda Runtime Interface Emulator](https://github.com/aws/aws-lambda-runtime-interface-emulator) on your local machine to test your function. Then when you run the image function, you set the entrypoint to be the emulator.
|
94
|
+
|
95
|
+
*To install the emulator and test your Lambda function*
|
96
|
+
|
97
|
+
1) From your project directory, run the following command to download the RIE from GitHub and install it on your local machine.
|
98
|
+
|
99
|
+
```shell script
|
100
|
+
mkdir -p ~/.aws-lambda-rie && \
|
101
|
+
curl -Lo ~/.aws-lambda-rie/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \
|
102
|
+
chmod +x ~/.aws-lambda-rie/aws-lambda-rie
|
103
|
+
```
|
104
|
+
2) Run your Lambda image function using the docker run command.
|
105
|
+
|
106
|
+
```shell script
|
107
|
+
docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
|
108
|
+
--entrypoint /aws-lambda/aws-lambda-rie \
|
109
|
+
myfunction:latest \
|
110
|
+
/usr/local/bin/aws_lambda_ric app.App::Handler.process
|
111
|
+
```
|
112
|
+
|
113
|
+
This runs the image as a container and starts up an endpoint locally at `http://localhost:9000/2015-03-31/functions/function/invocations`.
|
114
|
+
|
115
|
+
3) Post an event to the following endpoint using a curl command:
|
116
|
+
|
117
|
+
```shell script
|
118
|
+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
|
119
|
+
```
|
120
|
+
|
121
|
+
This command invokes the function running in the container image and returns a response.
|
122
|
+
|
123
|
+
*Alternately, you can also include RIE as a part of your base image. See the AWS documentation on how to [Build RIE into your base image](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html#images-test-alternative).*
|
124
|
+
|
125
|
+
## Development
|
126
|
+
|
127
|
+
### Building the package
|
128
|
+
Clone this repository and run:
|
129
|
+
|
130
|
+
```shell script
|
131
|
+
make init
|
132
|
+
make build
|
133
|
+
```
|
134
|
+
|
135
|
+
### Running tests
|
136
|
+
|
137
|
+
Make sure the project is built:
|
138
|
+
```shell script
|
139
|
+
make init build
|
140
|
+
```
|
141
|
+
Then,
|
142
|
+
* to run unit tests: `make test-unit`
|
143
|
+
* to run integration tests: `make test-integ`
|
144
|
+
* to run smoke tests: `make test-smoke`
|
145
|
+
|
146
|
+
### Troubleshooting
|
147
|
+
While running integration tests, you might encounter the Docker Hub rate limit error with the following body:
|
148
|
+
```
|
149
|
+
You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limits
|
150
|
+
```
|
151
|
+
To fix the above issue, consider authenticating to a Docker Hub account by setting the Docker Hub credentials as below CodeBuild environment variables.
|
152
|
+
```shell script
|
153
|
+
DOCKERHUB_USERNAME=<dockerhub username>
|
154
|
+
DOCKERHUB_PASSWORD=<dockerhub password>
|
155
|
+
```
|
156
|
+
Recommended way is to set the Docker Hub credentials in CodeBuild job by retrieving them from AWS Secrets Manager.
|
157
|
+
|
158
|
+
## Security
|
159
|
+
|
160
|
+
If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.
|
161
|
+
|
162
|
+
## License
|
163
|
+
|
164
|
+
This project is licensed under the Apache-2.0 License.
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
6
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
7
|
+
require "./lib/aws_lambda_ric_jets/version"
|
8
|
+
|
9
|
+
Gem::Specification.new do |spec|
|
10
|
+
spec.name = "aws_lambda_ric_jets"
|
11
|
+
spec.version = AwsLambdaRicJets::VERSION
|
12
|
+
spec.authors = ["Tung Nguyen"]
|
13
|
+
# Original name and authors below for credit.
|
14
|
+
# They are not maintaining this fork.
|
15
|
+
# spec.name = 'aws_lambda_ric_jets'
|
16
|
+
# spec.version = AwsLambdaRicJets::VERSION
|
17
|
+
# spec.authors = ['AWS Lambda']
|
18
|
+
|
19
|
+
spec.summary = "AWS Lambda Runtime Interface Client for Ruby"
|
20
|
+
spec.description = "The AWS Lambda Ruby Runtime Interface Client implements the Lambda programming model for Ruby."
|
21
|
+
spec.homepage = "https://github.com/aws/aws-lambda-ruby-runtime-interface-client"
|
22
|
+
|
23
|
+
spec.license = "Apache-2.0"
|
24
|
+
spec.required_ruby_version = ">= 2.5"
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
spec.files = %w[
|
28
|
+
LICENSE
|
29
|
+
README.md
|
30
|
+
Gemfile
|
31
|
+
NOTICE
|
32
|
+
aws_lambda_ric.gemspec
|
33
|
+
bin/aws_lambda_ric
|
34
|
+
] + Dir["lib/**/*"]
|
35
|
+
|
36
|
+
spec.bindir = "bin"
|
37
|
+
# all application-style files are expected to be found in bindir
|
38
|
+
spec.executables = "aws_lambda_ric"
|
39
|
+
spec.require_paths = ["lib"]
|
40
|
+
|
41
|
+
spec.add_development_dependency "bundler", ">= 2.0"
|
42
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
43
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
44
|
+
spec.add_development_dependency "activesupport", "~> 6.0.1"
|
45
|
+
end
|
data/bin/aws_lambda_ric
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
module AwsLambda
|
8
|
+
class Marshaller
|
9
|
+
class << self
|
10
|
+
# By default, JSON-parses the raw request body. This can be overwritten
|
11
|
+
# by users who know what they are doing.
|
12
|
+
def marshall_request(raw_request)
|
13
|
+
content_type = raw_request['Content-Type']
|
14
|
+
if content_type == 'application/json'
|
15
|
+
JSON.parse(raw_request.body)
|
16
|
+
else
|
17
|
+
raw_request.body # return it unaltered
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# By default, just runs #to_json on the method's response value.
|
22
|
+
# This can be overwritten by users who know what they are doing.
|
23
|
+
# The response is an array of response, content-type.
|
24
|
+
# If returned without a content-type, it is assumed to be application/json
|
25
|
+
# Finally, StringIO/IO is used to signal a response that shouldn't be
|
26
|
+
# formatted as JSON, and should get a different content-type header.
|
27
|
+
def marshall_response(method_response)
|
28
|
+
case method_response
|
29
|
+
when StringIO, IO
|
30
|
+
[method_response, 'application/unknown']
|
31
|
+
else
|
32
|
+
method_response.to_json # application/json is assumed
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative '../aws_lambda_ric'
|
2
|
+
|
3
|
+
# Bootstrap runtime
|
4
|
+
module Bootstrap
|
5
|
+
|
6
|
+
def self.start
|
7
|
+
bootstrap_telemetry_log_sink
|
8
|
+
bootstrap_handler
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.fetch_runtime_server
|
12
|
+
ENV.fetch(AwsLambdaRuntimeInterfaceClient::LambdaRunner::ENV_VAR_RUNTIME_API)
|
13
|
+
rescue KeyError
|
14
|
+
puts 'Failed to get runtime server address from AWS_LAMBDA_RUNTIME_API env variable'
|
15
|
+
exit(-2)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.bootstrap_telemetry_log_sink(path_to_fd='/proc/self/fd/')
|
19
|
+
fd = ENV.fetch(AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper::ENV_VAR_TELEMETRY_LOG_FD)
|
20
|
+
ENV.delete(AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper::ENV_VAR_TELEMETRY_LOG_FD)
|
21
|
+
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.new(fd, path_to_fd)
|
22
|
+
rescue KeyError
|
23
|
+
puts 'Skipped bootstraping TelemetryLog'
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.bootstrap_handler
|
27
|
+
if ARGV.empty?
|
28
|
+
puts 'No handler specified, exiting Runtime Interface Client.'
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
app_root = Dir.pwd
|
32
|
+
handler = ARGV[0]
|
33
|
+
lambda_runner = AwsLambdaRuntimeInterfaceClient::LambdaRunner.new(fetch_runtime_server, get_user_agent)
|
34
|
+
puts "Executing '#{handler}' in function directory '#{app_root}'"
|
35
|
+
lambda_runner.run(app_root, handler)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.get_user_agent
|
39
|
+
ruby_version = RUBY_VERSION.to_s
|
40
|
+
version = AwsLambdaRuntimeInterfaceClient::VERSION
|
41
|
+
|
42
|
+
"aws-lambda-ruby/#{ruby_version}-#{version}"
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
class LambdaContext
|
6
|
+
attr_reader :aws_request_id, :invoked_function_arn, :log_group_name,
|
7
|
+
:log_stream_name, :function_name, :memory_limit_in_mb, :function_version,
|
8
|
+
:identity, :client_context, :deadline_ms
|
9
|
+
|
10
|
+
def initialize(request)
|
11
|
+
@clock_diff = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
12
|
+
@deadline_ms = request['Lambda-Runtime-Deadline-Ms'].to_i
|
13
|
+
@aws_request_id = request['Lambda-Runtime-Aws-Request-Id']
|
14
|
+
@invoked_function_arn = request['Lambda-Runtime-Invoked-Function-Arn']
|
15
|
+
@log_group_name = ENV['AWS_LAMBDA_LOG_GROUP_NAME']
|
16
|
+
@log_stream_name = ENV['AWS_LAMBDA_LOG_STREAM_NAME']
|
17
|
+
@function_name = ENV['AWS_LAMBDA_FUNCTION_NAME']
|
18
|
+
@memory_limit_in_mb = ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE']
|
19
|
+
@function_version = ENV['AWS_LAMBDA_FUNCTION_VERSION']
|
20
|
+
@identity = JSON.parse(request['Lambda-Runtime-Cognito-Identity']) unless request['Lambda-Runtime-Cognito-Identity'].to_s.empty?
|
21
|
+
@client_context = JSON.parse(request['Lambda-Runtime-Client-Context']) unless request['Lambda-Runtime-Client-Context'].to_s.empty?
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_remaining_time_in_millis
|
25
|
+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) + @clock_diff
|
26
|
+
remaining = @deadline_ms - now
|
27
|
+
remaining.positive? ? remaining : 0
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
module LambdaErrors
|
5
|
+
|
6
|
+
class LambdaErrors::InvocationError < StandardError;
|
7
|
+
end
|
8
|
+
|
9
|
+
class LambdaError < StandardError
|
10
|
+
def initialize(original_error, classification = 'Function')
|
11
|
+
@error_class = original_error.class.to_s
|
12
|
+
@error_type = "#{classification}<#{original_error.class}>"
|
13
|
+
@error_message = original_error.message
|
14
|
+
@stack_trace = _sanitize_stacktrace(original_error.backtrace_locations)
|
15
|
+
super(original_error)
|
16
|
+
end
|
17
|
+
|
18
|
+
def runtime_error_type
|
19
|
+
if _allowed_error?
|
20
|
+
@error_type
|
21
|
+
else
|
22
|
+
'Function<UserException>'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_lambda_response
|
27
|
+
{
|
28
|
+
:errorMessage => @error_message,
|
29
|
+
:errorType => @error_type,
|
30
|
+
:stackTrace => @stack_trace
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def _sanitize_stacktrace(stacktrace)
|
37
|
+
ret = []
|
38
|
+
safe_trace = true
|
39
|
+
if stacktrace
|
40
|
+
stacktrace.first(100).each do |line|
|
41
|
+
if safe_trace
|
42
|
+
if line.to_s.match(%r{^lib})
|
43
|
+
safe_trace = false
|
44
|
+
else
|
45
|
+
ret << line
|
46
|
+
end
|
47
|
+
end # else skip
|
48
|
+
end
|
49
|
+
end
|
50
|
+
ret
|
51
|
+
end
|
52
|
+
|
53
|
+
def _allowed_error?
|
54
|
+
# _aws_sdk_pattern? || _standard_error?
|
55
|
+
_standard_error?
|
56
|
+
end
|
57
|
+
|
58
|
+
# Currently unused, may be activated later.
|
59
|
+
def _aws_sdk_pattern?
|
60
|
+
@error_class.match(/Aws(::\w+)*::Errors/)
|
61
|
+
end
|
62
|
+
|
63
|
+
def _standard_error?
|
64
|
+
%w[ArgumentError NoMethodError Exception StandardError NameError LoadError SystemExit SystemStackError].include?(@error_class)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class LambdaHandlerError < LambdaError
|
69
|
+
end
|
70
|
+
|
71
|
+
class LambdaHandlerCriticalException < LambdaError
|
72
|
+
end
|
73
|
+
|
74
|
+
class LambdaRuntimeError < LambdaError
|
75
|
+
def initialize(original_error)
|
76
|
+
super(original_error, 'Runtime')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class LambdaRuntimeInitError < LambdaError
|
81
|
+
def initialize(original_error)
|
82
|
+
super(original_error, 'Init')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
require_relative 'aws_lambda_marshaller'
|
4
|
+
|
5
|
+
# frozen_string_literal: true
|
6
|
+
class LambdaHandler
|
7
|
+
attr_reader :handler_file_name, :handler_method_name
|
8
|
+
|
9
|
+
def initialize(env_handler:)
|
10
|
+
handler_split = env_handler.split('.')
|
11
|
+
if handler_split.size == 2
|
12
|
+
@handler_file_name, @handler_method_name = handler_split
|
13
|
+
elsif handler_split.size == 3
|
14
|
+
@handler_file_name, @handler_class, @handler_method_name = handler_split
|
15
|
+
else
|
16
|
+
raise ArgumentError.new("Invalid handler #{handler_split}, must be of form FILENAME.METHOD or FILENAME.CLASS.METHOD where FILENAME corresponds with an existing Ruby source file FILENAME.rb, CLASS is an optional module/class namespace and METHOD is a callable method. If using CLASS, METHOD must be a class-level method.")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def call_handler(request:, context:)
|
21
|
+
opts = {
|
22
|
+
event: request,
|
23
|
+
context: context
|
24
|
+
}
|
25
|
+
if @handler_class
|
26
|
+
response = Kernel.const_get(@handler_class).send(@handler_method_name, **opts)
|
27
|
+
else
|
28
|
+
response = __send__(@handler_method_name, **opts)
|
29
|
+
end
|
30
|
+
# serialization can be a part of user code
|
31
|
+
AwsLambda::Marshaller.marshall_response(response)
|
32
|
+
rescue NoMethodError => e
|
33
|
+
# This is a special case of standard error that we want to hard-fail for
|
34
|
+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
|
35
|
+
rescue NameError => e
|
36
|
+
# This is a special case error that we want to wrap
|
37
|
+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
|
38
|
+
rescue StandardError => e
|
39
|
+
raise LambdaErrors::LambdaHandlerError.new(e)
|
40
|
+
rescue Exception => e
|
41
|
+
raise LambdaErrors::LambdaHandlerCriticalException.new(e)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
class LambdaLogFormatter < Logger::Formatter
|
8
|
+
FORMAT = '%<sev>s, [%<datetime>s#%<process>d] %<severity>5s %<request_id>s -- %<progname>s: %<msg>s'
|
9
|
+
|
10
|
+
def call(severity, time, progname, msg)
|
11
|
+
(FORMAT % {sev: severity[0..0], datetime: format_datetime(time), process: $$, severity: severity,
|
12
|
+
request_id: $_global_aws_request_id, progname: progname, msg: msg2str(msg)}).encode!('UTF-8')
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'net/http'
|
6
|
+
require 'json'
|
7
|
+
require_relative 'lambda_errors'
|
8
|
+
|
9
|
+
class LambdaServer
|
10
|
+
LAMBDA_DEFAULT_SERVER_ADDRESS = '127.0.0.1:9001'
|
11
|
+
LAMBDA_RUNTIME_API_VERSION = '2018-06-01'
|
12
|
+
|
13
|
+
MAX_HEADER_SIZE_BYTES = 1024 * 1024
|
14
|
+
LONG_TIMEOUT_MS = 1_000_000
|
15
|
+
|
16
|
+
def initialize(server_address, user_agent)
|
17
|
+
server_address ||= LAMBDA_DEFAULT_SERVER_ADDRESS
|
18
|
+
@server_address = "http://#{server_address}/#{LAMBDA_RUNTIME_API_VERSION}"
|
19
|
+
@user_agent = user_agent
|
20
|
+
end
|
21
|
+
|
22
|
+
def next_invocation
|
23
|
+
next_invocation_uri = URI(@server_address + '/runtime/invocation/next')
|
24
|
+
begin
|
25
|
+
http = Net::HTTP.new(next_invocation_uri.host, next_invocation_uri.port)
|
26
|
+
http.read_timeout = LONG_TIMEOUT_MS
|
27
|
+
resp = http.start do |connection|
|
28
|
+
connection.get(next_invocation_uri.path, { 'User-Agent' => @user_agent })
|
29
|
+
end
|
30
|
+
if resp.is_a?(Net::HTTPSuccess)
|
31
|
+
request_id = resp['Lambda-Runtime-Aws-Request-Id']
|
32
|
+
[request_id, resp]
|
33
|
+
else
|
34
|
+
raise LambdaErrors::InvocationError.new(
|
35
|
+
"Received #{resp.code} when waiting for next invocation."
|
36
|
+
)
|
37
|
+
end
|
38
|
+
rescue LambdaErrors::InvocationError => e
|
39
|
+
raise e
|
40
|
+
rescue StandardError => e
|
41
|
+
raise LambdaErrors::InvocationError.new(e)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def send_response(request_id:, response_object:, content_type: 'application/json')
|
46
|
+
response_uri = URI(@server_address + "/runtime/invocation/#{request_id}/response")
|
47
|
+
begin
|
48
|
+
# unpack IO at this point
|
49
|
+
if content_type == 'application/unknown'
|
50
|
+
response_object = response_object.read
|
51
|
+
end
|
52
|
+
Net::HTTP.post(
|
53
|
+
response_uri,
|
54
|
+
response_object,
|
55
|
+
{ 'Content-Type' => content_type, 'User-Agent' => @user_agent }
|
56
|
+
)
|
57
|
+
rescue StandardError => e
|
58
|
+
raise LambdaErrors::LambdaRuntimeError.new(e)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def send_error_response(request_id:, error_object:, error:, xray_cause:)
|
63
|
+
response_uri = URI(@server_address + "/runtime/invocation/#{request_id}/error")
|
64
|
+
begin
|
65
|
+
headers = { 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type, 'User-Agent' => @user_agent }
|
66
|
+
headers['Lambda-Runtime-Function-XRay-Error-Cause'] = xray_cause if xray_cause.bytesize < MAX_HEADER_SIZE_BYTES
|
67
|
+
Net::HTTP.post(
|
68
|
+
response_uri,
|
69
|
+
error_object.to_json,
|
70
|
+
headers
|
71
|
+
)
|
72
|
+
rescue StandardError => e
|
73
|
+
raise LambdaErrors::LambdaRuntimeError.new(e)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def send_init_error(error_object:, error:)
|
78
|
+
uri = URI("#{@server_address}/runtime/init/error")
|
79
|
+
begin
|
80
|
+
Net::HTTP.post(
|
81
|
+
uri,
|
82
|
+
error_object.to_json,
|
83
|
+
{ 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type, 'User-Agent' => @user_agent }
|
84
|
+
)
|
85
|
+
rescue StandardError => e
|
86
|
+
raise LambdaErrors::LambdaRuntimeInitError.new(e)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
module LoggerPatch
|
4
|
+
def initialize(logdev, shift_age = 0, shift_size = 1048576, level: 'debug',
|
5
|
+
progname: nil, formatter: nil, datetime_format: nil,
|
6
|
+
binmode: false, shift_period_suffix: '%Y%m%d')
|
7
|
+
# use unpatched constructor if logdev is a filename or an IO Object other than $stdout or $stderr
|
8
|
+
if logdev && logdev != $stdout && logdev != $stderr
|
9
|
+
super(logdev, shift_age, shift_size, level: level, progname: progname,
|
10
|
+
formatter: formatter, datetime_format: datetime_format,
|
11
|
+
binmode: binmode, shift_period_suffix: shift_period_suffix)
|
12
|
+
else
|
13
|
+
self.level = level
|
14
|
+
self.progname = progname
|
15
|
+
@default_formatter = LambdaLogFormatter.new
|
16
|
+
self.datetime_format = datetime_format
|
17
|
+
self.formatter = formatter
|
18
|
+
@logdev = AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_sink
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
class TelemetryLogSink < Logger::LogDevice
|
8
|
+
|
9
|
+
# TelemetryLogSink implements the logging contract between runtimes and the platform. It implements a simple
|
10
|
+
# framing protocol so message boundaries can be determined. Each frame can be visualized as follows:
|
11
|
+
#
|
12
|
+
# +----------------------+------------------------+-----------------------+
|
13
|
+
# | Frame Type - 4 bytes | Length (len) - 4 bytes | Message - \'len\' bytes |
|
14
|
+
# +----------------------+------------------------+-----------------------+
|
15
|
+
#
|
16
|
+
# The first 4 bytes indicate the type of the frame - log frames have a type defined as the hex value 0xa55a0001. The
|
17
|
+
# second 4 bytes should indicate the message\'s length. The next \'len\' bytes contain the message. The byte order is
|
18
|
+
# big-endian.
|
19
|
+
|
20
|
+
def initialize(file:)
|
21
|
+
@file = file
|
22
|
+
end
|
23
|
+
|
24
|
+
FRAME_BYTES = [0xa55a0001].pack('L>')
|
25
|
+
|
26
|
+
def write(msg)
|
27
|
+
if @file.nil? || @file.closed?
|
28
|
+
$stdout.write(msg)
|
29
|
+
else
|
30
|
+
@file.write(FRAME_BYTES)
|
31
|
+
@file.write([msg.bytesize].pack('L>'))
|
32
|
+
@file.write(msg)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def reopen(log = nil)
|
37
|
+
# do nothing
|
38
|
+
end
|
39
|
+
|
40
|
+
def close
|
41
|
+
# do nothing
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
require_relative 'lambda_errors'
|
4
|
+
|
5
|
+
class XRayCause
|
6
|
+
MAX_DEPTH = 15
|
7
|
+
|
8
|
+
def initialize(lambda_error)
|
9
|
+
@cause = {
|
10
|
+
working_directory: Dir.pwd,
|
11
|
+
paths: Gem.paths.path,
|
12
|
+
exceptions: lambda_error ? normalize(err: lambda_error) : lambda_error
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_json
|
17
|
+
@as_json ||= begin
|
18
|
+
JSON.dump(@cause)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def normalize(err:)
|
25
|
+
exception = {
|
26
|
+
message: err[:errorMessage],
|
27
|
+
type: err[:errorType]
|
28
|
+
}
|
29
|
+
|
30
|
+
backtrace = err[:stackTrace]
|
31
|
+
if backtrace
|
32
|
+
exception[:stack] = backtrace.first(MAX_DEPTH).collect do |t|
|
33
|
+
{
|
34
|
+
path: t.path,
|
35
|
+
line: t.lineno,
|
36
|
+
label: t.label
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
[exception]
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require_relative 'aws_lambda_ric/lambda_errors'
|
6
|
+
require_relative 'aws_lambda_ric/lambda_server'
|
7
|
+
require_relative 'aws_lambda_ric/lambda_handler'
|
8
|
+
require_relative 'aws_lambda_ric/lambda_context'
|
9
|
+
require_relative 'aws_lambda_ric/lambda_logger'
|
10
|
+
require_relative 'aws_lambda_ric/lambda_log_formatter'
|
11
|
+
require_relative 'aws_lambda_ric/logger_patch'
|
12
|
+
require_relative 'aws_lambda_ric/telemetry_log_sink'
|
13
|
+
require_relative 'aws_lambda_ric/aws_lambda_marshaller'
|
14
|
+
require_relative 'aws_lambda_ric/xray_cause'
|
15
|
+
require_relative 'aws_lambda_ric/version'
|
16
|
+
require 'logger'
|
17
|
+
|
18
|
+
$stdout.sync = true # Ensures that logs are flushed promptly.
|
19
|
+
|
20
|
+
module AwsLambdaRuntimeInterfaceClient
|
21
|
+
|
22
|
+
class Error < StandardError; end
|
23
|
+
|
24
|
+
# Loads the user code and runs it upon invocation
|
25
|
+
class LambdaRunner
|
26
|
+
|
27
|
+
ENV_VAR_RUNTIME_API = 'AWS_LAMBDA_RUNTIME_API'
|
28
|
+
|
29
|
+
def initialize(runtime_server_addr, user_agent)
|
30
|
+
@lambda_server = LambdaServer.new(runtime_server_addr, user_agent)
|
31
|
+
@runtime_loop_active = true # if false, we will exit the program
|
32
|
+
@exit_code = 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def run(app_root, handler)
|
36
|
+
|
37
|
+
$LOAD_PATH.unshift(app_root) unless $LOAD_PATH.include?(app_root)
|
38
|
+
|
39
|
+
begin
|
40
|
+
@lambda_handler = LambdaHandler.new(env_handler: handler)
|
41
|
+
require @lambda_handler.handler_file_name
|
42
|
+
start_runtime_loop
|
43
|
+
rescue Exception => e # which includes LoadError or any exception within static user code
|
44
|
+
@runtime_loop_active = false
|
45
|
+
@exit_code = -4
|
46
|
+
send_init_error_to_server(e)
|
47
|
+
ensure
|
48
|
+
TelemetryLoggingHelper.close
|
49
|
+
end
|
50
|
+
|
51
|
+
exit(@exit_code)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def start_runtime_loop
|
57
|
+
while @runtime_loop_active
|
58
|
+
lambda_invocation_request = wait_for_invocation
|
59
|
+
run_user_code(lambda_invocation_request)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def wait_for_invocation
|
64
|
+
request_id, raw_request = @lambda_server.next_invocation
|
65
|
+
$_global_aws_request_id = request_id
|
66
|
+
if (trace_id = raw_request['Lambda-Runtime-Trace-Id'])
|
67
|
+
ENV['_X_AMZN_TRACE_ID'] = trace_id
|
68
|
+
end
|
69
|
+
request = AwsLambda::Marshaller.marshall_request(raw_request)
|
70
|
+
|
71
|
+
LambdaInvocationRequest.new(request_id, raw_request, request, trace_id)
|
72
|
+
rescue LambdaErrors::InvocationError => e
|
73
|
+
@runtime_loop_active = false # ends the loop
|
74
|
+
raise e # ends the process
|
75
|
+
end
|
76
|
+
|
77
|
+
def run_user_code(lambda_invocation_request)
|
78
|
+
context = LambdaContext.new(lambda_invocation_request.raw_request) # pass in opts
|
79
|
+
|
80
|
+
# start of user code
|
81
|
+
handler_response, content_type = @lambda_handler.call_handler(
|
82
|
+
request: lambda_invocation_request.request,
|
83
|
+
context: context
|
84
|
+
)
|
85
|
+
# end of user code
|
86
|
+
|
87
|
+
@lambda_server.send_response(
|
88
|
+
request_id: lambda_invocation_request.request_id,
|
89
|
+
response_object: handler_response,
|
90
|
+
content_type: content_type
|
91
|
+
)
|
92
|
+
|
93
|
+
rescue LambdaErrors::LambdaHandlerError => e
|
94
|
+
LambdaLogger.log_error(exception: e, message: 'Error raised from handler method')
|
95
|
+
send_error_response(lambda_invocation_request, e)
|
96
|
+
rescue LambdaErrors::LambdaHandlerCriticalException => e
|
97
|
+
LambdaLogger.log_error(exception: e, message: 'Critical exception from handler')
|
98
|
+
send_error_response(lambda_invocation_request, e, -1, false)
|
99
|
+
rescue LambdaErrors::LambdaRuntimeError => e
|
100
|
+
send_error_response(lambda_invocation_request, e, -2, false)
|
101
|
+
end
|
102
|
+
|
103
|
+
def send_init_error_to_server(err)
|
104
|
+
ex = LambdaErrors::LambdaRuntimeInitError.new(err)
|
105
|
+
LambdaLogger.log_error(exception: ex, message: "Init error when loading handler #{@env_handler}")
|
106
|
+
@lambda_server.send_init_error(error_object: ex.to_lambda_response, error: ex)
|
107
|
+
end
|
108
|
+
|
109
|
+
def send_error_response(lambda_invocation, err, exit_code = nil, runtime_loop_active = true)
|
110
|
+
error_object = err.to_lambda_response
|
111
|
+
@lambda_server.send_error_response(
|
112
|
+
request_id: lambda_invocation.request_id,
|
113
|
+
error_object: error_object,
|
114
|
+
error: err,
|
115
|
+
xray_cause: XRayCause.new(error_object).as_json
|
116
|
+
)
|
117
|
+
|
118
|
+
@exit_code = exit_code unless exit_code.nil?
|
119
|
+
@runtime_loop_active = runtime_loop_active
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Helper class to for mutating std logger with TelemetryLog
|
124
|
+
class TelemetryLoggingHelper
|
125
|
+
|
126
|
+
ENV_VAR_TELEMETRY_LOG_FD = '_LAMBDA_TELEMETRY_LOG_FD'
|
127
|
+
|
128
|
+
class << self
|
129
|
+
attr_accessor :telemetry_log_fd_file, :telemetry_log_sink
|
130
|
+
|
131
|
+
def close
|
132
|
+
telemetry_log_fd_file&.close
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def initialize(telemetry_log_fd, path_to_fd='/proc/self/fd/')
|
137
|
+
fd = "#{path_to_fd}#{telemetry_log_fd}"
|
138
|
+
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file = File.open(fd, 'wb')
|
139
|
+
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file.sync = true
|
140
|
+
|
141
|
+
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_sink = TelemetryLogSink.new(file: AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file)
|
142
|
+
|
143
|
+
mutate_std_logger
|
144
|
+
mutate_kernel_puts
|
145
|
+
rescue Errno::ENOENT
|
146
|
+
# If File.open() fails, then the mutation won't happen and the default behaviour (print to stdout) will prevail
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def mutate_std_logger
|
152
|
+
Logger.class_eval do
|
153
|
+
prepend LoggerPatch
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def mutate_kernel_puts
|
158
|
+
Kernel.module_eval do
|
159
|
+
def puts(*arg)
|
160
|
+
msg = arg.flatten.collect { |a| a.to_s.encode('UTF-8') }.join("\n")
|
161
|
+
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_sink.write(msg)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Represents a single Lambda Invocation Request
|
168
|
+
class LambdaInvocationRequest
|
169
|
+
|
170
|
+
attr_accessor :request_id, :raw_request, :request, :trace_id
|
171
|
+
|
172
|
+
def initialize(request_id, raw_request, request, trace_id)
|
173
|
+
@request_id = request_id
|
174
|
+
@raw_request = raw_request
|
175
|
+
@request = request
|
176
|
+
@trace_id = trace_id
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws_lambda_ric_jets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tung Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 6.0.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 6.0.1
|
69
|
+
description: The AWS Lambda Ruby Runtime Interface Client implements the Lambda programming
|
70
|
+
model for Ruby.
|
71
|
+
email:
|
72
|
+
executables:
|
73
|
+
- aws_lambda_ric
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- NOTICE
|
80
|
+
- README.md
|
81
|
+
- aws_lambda_ric.gemspec
|
82
|
+
- bin/aws_lambda_ric
|
83
|
+
- lib/aws_lambda_ric.rb
|
84
|
+
- lib/aws_lambda_ric/aws_lambda_marshaller.rb
|
85
|
+
- lib/aws_lambda_ric/bootstrap.rb
|
86
|
+
- lib/aws_lambda_ric/lambda_context.rb
|
87
|
+
- lib/aws_lambda_ric/lambda_errors.rb
|
88
|
+
- lib/aws_lambda_ric/lambda_handler.rb
|
89
|
+
- lib/aws_lambda_ric/lambda_log_formatter.rb
|
90
|
+
- lib/aws_lambda_ric/lambda_logger.rb
|
91
|
+
- lib/aws_lambda_ric/lambda_server.rb
|
92
|
+
- lib/aws_lambda_ric/logger_patch.rb
|
93
|
+
- lib/aws_lambda_ric/telemetry_log_sink.rb
|
94
|
+
- lib/aws_lambda_ric/version.rb
|
95
|
+
- lib/aws_lambda_ric/xray_cause.rb
|
96
|
+
- lib/aws_lambda_ric_jets/version.rb
|
97
|
+
homepage: https://github.com/aws/aws-lambda-ruby-runtime-interface-client
|
98
|
+
licenses:
|
99
|
+
- Apache-2.0
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '2.5'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.4.19
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: AWS Lambda Runtime Interface Client for Ruby
|
120
|
+
test_files: []
|