Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules
.cdk.staging
cdk.out

packages
packages
vendor
2 changes: 2 additions & 0 deletions lambdas/ruby-3.2/.bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BUNDLE_PATH: "vendor/bundle "
5 changes: 5 additions & 0 deletions lambdas/ruby-3.2/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem 'aws-sdk-dynamodb', '~> 1'
25 changes: 25 additions & 0 deletions lambdas/ruby-3.2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
remote: https://rubygems.org/
specs:
aws-eventstream (1.2.0)
aws-partitions (1.777.0)
aws-sdk-core (3.174.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.5)
jmespath (~> 1, >= 1.6.1)
aws-sdk-dynamodb (1.85.0)
aws-sdk-core (~> 3, >= 3.174.0)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.5.2)
aws-eventstream (~> 1, >= 1.0.2)
jmespath (1.6.2)

PLATFORMS
x86_64-darwin-22

DEPENDENCIES
aws-sdk-dynamodb (~> 1)

BUNDLED WITH
2.4.10
40 changes: 40 additions & 0 deletions lambdas/ruby-3.2/src/func.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "aws-sdk-dynamodb"
require "json"

DB = ::Aws::DynamoDB::Client.new
LANG_REGEXP = /^\/ruby-(\d)-(\d)-(yjit-)?(x86|arm)$/

Item = Data.define(:langCase, :iteration, :raw_event, :event_params) do
def initialize(langCase:, iteration:, raw_event: nil, event_params: {}) = super
def to_h = super.compact.except(:event_params).merge(**event_params)
end

def handler(event:, **)
event = JSON.parse(event.to_json, symbolize_names: true)
lang = event[:path].gsub(/\//, '')
event_params = parse_event_body_params(event) => iteration:

return { statusCode: 400 } unless iteration

item = Item.new(lang, iteration, event.to_h, event_params)

new_item = DB.put_item(
table_name: ENV["TABLE"],
item: item.to_h,
return_values: "ALL_OLD"
).attributes

previous_item = DB.get_item(
table_name: ENV["TABLE"],
key: Item.new(lang, iteration - 1).to_h
).item

{ statusCode: 200, body: (previous_item || new_item).to_json }
end

def parse_event_body_params(event) = case event
in isBase64Encoded: encoded, body: body, path: LANG_REGEXP
b = encoded ? Base64.decode64(body) : body

JSON.parse(b, symbolize_names: true)
end
70 changes: 53 additions & 17 deletions lib/resources/lambdas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,89 @@ import { RetentionDays } from "aws-cdk-lib/aws-logs";
import * as ssm from 'aws-cdk-lib/aws-ssm';
import { ITable } from "aws-cdk-lib/aws-dynamodb";

enum OLangCase {
Ruby2_7 = 'ruby-2-7-x86',
Ruby3_2 = 'ruby-3-2-x86',
Ruby3_2_YJIT = 'ruby-3-2-yjit-x86'
};

export interface LambdasProps extends StackProps {
baseTable: ITable;
}

export type TLambdas = {
'ruby-2.7-x86': IFunction
[OLangCase.Ruby2_7]: IFunction,
[OLangCase.Ruby3_2]: IFunction
[OLangCase.Ruby3_2_YJIT]: IFunction
}

type LambdaOptions = {
runtime: Runtime,
lpackage: string,
env?: { [key: string]: string }
runtime: Runtime;
lpackage: string;
env?: {
[key: string]: string;
}
}

export default class Lambdas extends Construct {
readonly Ruby2_7Lambda: Function;
public static readonly RUBY_LAMBDA_CONFIGS = {
[OLangCase.Ruby2_7]: {
runtime: Runtime.RUBY_2_7,
lpackage: 'ruby-2.7.zip',
env: {
GEM_PATH: './vendor'
}
},
[OLangCase.Ruby3_2]: {
runtime: Runtime.RUBY_3_2,
lpackage: 'ruby-3.2.zip'
},
[OLangCase.Ruby3_2_YJIT]: {
runtime: Runtime.RUBY_3_2,
lpackage: 'ruby-3.2.zip',
env: {
'RUBY_YJIT_ENABLE': '1'
}
},
}

private Ruby2_7Lambda: Function;
private Ruby3_2Lambda: Function;
private Ruby3_2YJITLambda: Function;

private props: LambdasProps;

constructor(scope: Construct, id: string, props: LambdasProps) {
super(scope, id);

this.props = props
this.Ruby2_7Lambda = this.createRubyLambda({
runtime: Runtime.RUBY_2_7, lpackage: 'ruby-2.7.zip'
});

this.Ruby2_7Lambda = this.createRubyLambda(OLangCase.Ruby2_7)
this.Ruby3_2Lambda = this.createRubyLambda(OLangCase.Ruby3_2)
this.Ruby3_2YJITLambda = this.createRubyLambda(OLangCase.Ruby3_2_YJIT)
}

public all = (): TLambdas => ({
'ruby-2.7-x86': this.Ruby2_7Lambda
[OLangCase.Ruby2_7]: this.Ruby2_7Lambda,
[OLangCase.Ruby3_2]: this.Ruby3_2Lambda,
[OLangCase.Ruby3_2_YJIT]: this.Ruby3_2YJITLambda
})

private createRubyLambda(opts: LambdaOptions) {
const name = opts.lpackage.replace('.zip', '').replace(/\W/g, '_').toUpperCase()
private createRubyLambda(version: OLangCase) {
const config: LambdaOptions = Lambdas.RUBY_LAMBDA_CONFIGS[version]

const lambdaProps = {
functionName: `${name}-Battle-Function`,
code: Code.fromAsset(`./packages/${opts.lpackage}`),
functionName: `${version}-Battle-Function`,
code: Code.fromAsset(`./packages/${config.lpackage}`),
handler: 'src/func.handler',
runtime: opts.runtime,
runtime: config.runtime,
environment: {
GEM_PATH: './vendor',
TABLE: this.props.baseTable.tableName
TABLE: this.props.baseTable.tableName,
...(config.env || {})
}
}

const rubyFunction = new Function(this, `${name}-lambda`, lambdaProps);
const rubyFunction = new Function(this, `${version}-lambda`, lambdaProps);

this.props.baseTable.grantReadWriteData(rubyFunction);

Expand Down