Skip to content

Version 3 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-on-push-and-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- name: Set up ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
ruby-version: 3.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason to use 3.1 over 3.3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was what was internal. My focus here was to do the syncing and do incremental upgrades on top later. I also wanted to discuss a release strategy. Typically ruby gems support multiple versions of ruby like this gem seemed to do. I think for lambdas use case, it may be simpler to release minor versions corresponding to the ruby version and support a singular ruby minor version. E.g. gem version 3.4 supports ruby 3.4 only. This may work for lambda because you deploy a single ruby version runtime using this gem correct?

- name: Run 'pr' target
run: make pr
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

372 changes: 159 additions & 213 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setup-codebuild-agent:

.PHONY: test-smoke
test-smoke: setup-codebuild-agent
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_one.sh test/integration/codebuild/buildspec.os.alpine.1.yml alpine 3.12 2.7
CODEBUILD_IMAGE_TAG=codebuild-agent test/integration/codebuild-local/test_one.sh test/integration/codebuild/buildspec.os.alpine.1.yml alpine 3.16 3.1

.PHONY: test-unit
test-unit:
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ First step is to choose the base image to be used. The supported Linux OS distri

- Amazon Linux 2
- Alpine
- CentOS
- Debian
- Ubuntu

Expand Down
9 changes: 4 additions & 5 deletions aws_lambda_ric.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
Expand All @@ -8,15 +6,15 @@ require './lib/aws_lambda_ric/version'

Gem::Specification.new do |spec|
spec.name = 'aws_lambda_ric'
spec.version = AwsLambdaRuntimeInterfaceClient::VERSION
spec.version = AwsLambdaRIC::VERSION
spec.authors = ['AWS Lambda']

spec.summary = 'AWS Lambda Runtime Interface Client for Ruby'
spec.description = 'The AWS Lambda Ruby Runtime Interface Client implements the Lambda programming model for Ruby.'
spec.homepage = 'https://github.com/aws/aws-lambda-ruby-runtime-interface-client'

spec.license = 'Apache-2.0'
spec.required_ruby_version = '>= 2.5'
spec.required_ruby_version = '>= 3.0'

# Specify which files should be added to the gem when it is released.
spec.files = %w[
Expand All @@ -33,8 +31,9 @@ Gem::Specification.new do |spec|
spec.executables = 'aws_lambda_ric'
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '>= 2.0'
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'activesupport', '~> 6.0.1'
spec.add_development_dependency 'test-unit', '~> 3.5.5'
end
9 changes: 4 additions & 5 deletions bin/aws_lambda_ric
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#! /usr/bin/env ruby

# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

require 'bundler/setup'
require_relative '../lib/aws_lambda_ric/bootstrap'
require_relative '../lib/aws_lambda_ric'

lambda_ric = AwsLambdaRIC::Bootstrap.new

Bootstrap.start
lambda_ric.start
23 changes: 23 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

version: 0.2

env:
shell: bash

phases:
install:
commands:
- docker logout public.ecr.aws
- docker pull public.ecr.aws/amazonlinux/amazonlinux:2

pre_build:
commands:
- mkdir -p "${OUT_DIR:-build-artifacts}"

build:
commands:
- make build-all OUT_DIR="${OUT_DIR:-build-artifacts}"

artifacts:
files:
- "build-artifacts/**"
69 changes: 56 additions & 13 deletions lib/aws_lambda_ric.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

require_relative 'aws_lambda_ric/lambda_errors'
Expand All @@ -17,17 +15,24 @@

$stdout.sync = true # Ensures that logs are flushed promptly.

module AwsLambdaRuntimeInterfaceClient
module AwsLambdaRIC

class Error < StandardError; end

def self.get_user_agent
ruby_version = RUBY_VERSION.to_s
version = AwsLambdaRIC::VERSION

"aws-lambda-ruby/#{ruby_version}-#{version}"
end

# Loads the user code and runs it upon invocation
class LambdaRunner

ENV_VAR_RUNTIME_API = 'AWS_LAMBDA_RUNTIME_API'

def initialize(runtime_server_addr, user_agent)
@lambda_server = LambdaServer.new(runtime_server_addr, user_agent)
@lambda_server = RapidClient.new(runtime_server_addr, user_agent)
@runtime_loop_active = true # if false, we will exit the program
@exit_code = 0
end
Expand All @@ -45,7 +50,7 @@ def run(app_root, handler)
@exit_code = -4
send_init_error_to_server(e)
ensure
TelemetryLoggingHelper.close
TelemetryLogger.close
end

exit(@exit_code)
Expand Down Expand Up @@ -121,7 +126,7 @@ def send_error_response(lambda_invocation, err, exit_code = nil, runtime_loop_ac
end

# Helper class to for mutating std logger with TelemetryLog
class TelemetryLoggingHelper
class TelemetryLogger

ENV_VAR_TELEMETRY_LOG_FD = '_LAMBDA_TELEMETRY_LOG_FD'

Expand All @@ -133,19 +138,27 @@ def close
end
end

def initialize(telemetry_log_fd, path_to_fd='/proc/self/fd/')
fd = "#{path_to_fd}#{telemetry_log_fd}"
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file = File.open(fd, 'wb')
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file.sync = true
def initialize(telemetry_log_fd)
fd = telemetry_log_fd.to_i
AwsLambdaRIC::TelemetryLogger.telemetry_log_fd_file = IO.new(fd, 'wb')
AwsLambdaRIC::TelemetryLogger.telemetry_log_fd_file.sync = true

AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_sink = TelemetryLogSink.new(file: AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_fd_file)
AwsLambdaRIC::TelemetryLogger.telemetry_log_sink = TelemetryLogSink.new(file: AwsLambdaRIC::TelemetryLogger.telemetry_log_fd_file)

mutate_std_logger
mutate_kernel_puts
rescue Errno::ENOENT
rescue Errno::ENOENT, Errno::EBADF
# If File.open() fails, then the mutation won't happen and the default behaviour (print to stdout) will prevail
end

def self.from_env()
if ENV.key?(ENV_VAR_TELEMETRY_LOG_FD)
fd = ENV.fetch(AwsLambdaRIC::TelemetryLogger::ENV_VAR_TELEMETRY_LOG_FD)
ENV.delete(AwsLambdaRIC::TelemetryLogger::ENV_VAR_TELEMETRY_LOG_FD)
AwsLambdaRIC::TelemetryLogger.new(fd)
end
end

private

def mutate_std_logger
Expand All @@ -158,7 +171,11 @@ def mutate_kernel_puts
Kernel.module_eval do
def puts(*arg)
msg = arg.flatten.collect { |a| a.to_s.encode('UTF-8') }.join("\n")
AwsLambdaRuntimeInterfaceClient::TelemetryLoggingHelper.telemetry_log_sink.write(msg)
if msg[-1] != "\n"
msg += "\n"
end
AwsLambdaRIC::TelemetryLogger.telemetry_log_sink.write(msg)
return nil
end
end
end
Expand All @@ -176,4 +193,30 @@ def initialize(request_id, raw_request, request, trace_id)
@trace_id = trace_id
end
end

class Bootstrap

def start
AwsLambdaRIC::TelemetryLogger::from_env()
bootstrap_handler
end

def fetch_runtime_server
ENV.fetch(AwsLambdaRIC::LambdaRunner::ENV_VAR_RUNTIME_API)
rescue KeyError
puts 'Failed to get runtime server address from AWS_LAMBDA_RUNTIME_API env variable'
exit(-2)
end

def bootstrap_handler
if ENV['_HANDLER'] == nil
puts 'No handler specified, exiting Runtime Interface Client.'
exit
end
app_root = Dir.pwd
handler = ENV['_HANDLER']
lambda_runner = AwsLambdaRIC::LambdaRunner.new(fetch_runtime_server, AwsLambdaRIC::get_user_agent)
lambda_runner.run(app_root, handler)
end
end
end
2 changes: 0 additions & 2 deletions lib/aws_lambda_ric/aws_lambda_marshaller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

require 'stringio'
Expand Down
45 changes: 0 additions & 45 deletions lib/aws_lambda_ric/bootstrap.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/aws_lambda_ric/lambda_context.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

class LambdaContext
Expand Down
11 changes: 5 additions & 6 deletions lib/aws_lambda_ric/lambda_errors.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

module LambdaErrors

class LambdaErrors::InvocationError < StandardError;
Expand All @@ -25,9 +24,9 @@ def runtime_error_type

def to_lambda_response
{
:errorMessage => @error_message,
:errorType => @error_type,
:stackTrace => @stack_trace
:errorMessage => @error_message,
:errorType => @error_type,
:stackTrace => @stack_trace
}
end

Expand All @@ -39,7 +38,7 @@ def _sanitize_stacktrace(stacktrace)
if stacktrace
stacktrace.first(100).each do |line|
if safe_trace
if line.to_s.match(%r{^lib})
if line.to_s.match(%r{^/var/runtime/lib})
safe_trace = false
else
ret << line
Expand Down
6 changes: 3 additions & 3 deletions lib/aws_lambda_ric/lambda_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# frozen_string_literal: true

require_relative 'aws_lambda_marshaller'

Expand All @@ -19,8 +19,8 @@ def initialize(env_handler:)

def call_handler(request:, context:)
opts = {
event: request,
context: context
event: request,
context: context
}
if @handler_class
response = Kernel.const_get(@handler_class).send(@handler_method_name, **opts)
Expand Down
6 changes: 2 additions & 4 deletions lib/aws_lambda_ric/lambda_log_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

# frozen_string_literal: true

require 'logger'

class LambdaLogFormatter < Logger::Formatter
FORMAT = '%<sev>s, [%<datetime>s#%<process>d] %<severity>5s %<request_id>s -- %<progname>s: %<msg>s'
class LogFormatter < Logger::Formatter
FORMAT = '%<sev>s, [%<datetime>s #%<process>d] %<severity>5s %<request_id>s -- %<progname>s: %<msg>s'

def call(severity, time, progname, msg)
(FORMAT % {sev: severity[0..0], datetime: format_datetime(time), process: $$, severity: severity,
Expand Down
2 changes: 1 addition & 1 deletion lib/aws_lambda_ric/lambda_logger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# frozen_string_literal: true

class LambdaLogger
class << self
Expand Down
Loading
Loading