Skip to content

Commit e867cd8

Browse files
authored
Merge pull request #100 from ruby-no-kai/lambda
App Runner → Lambda
2 parents b0d7f4d + fc977d6 commit e867cd8

33 files changed

+463
-120
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,25 @@ jobs:
3434
uses: aws-actions/amazon-ecr-login@062b18b96a7aff071d4dc91bc00c4c1a7945b076 # v2.0.1
3535
- run: "echo '${{ github.sha }}' > REVISION"
3636
- name: 'Build Docker image'
37-
uses: 'docker/build-push-action@1104d471370f9806843c095c1db02b5a90c5f8b6' # v3.3.1
38-
with:
39-
context: '.'
40-
load: true
41-
tags: "sponsor-app-test:latest,${{ steps.login-ecr.outputs.registry }}/sponsor-app:${{ github.sha }},${{ steps.login-ecr.outputs.registry }}/sponsor-app:latest"
42-
cache-from: type=gha
43-
cache-to: type=gha,mode=max
44-
- name: 'Push Docker image'
4537
uses: 'docker/build-push-action@1104d471370f9806843c095c1db02b5a90c5f8b6' # v3.3.1
4638
with:
4739
context: '.'
4840
push: true
4941
tags: "${{ steps.login-ecr.outputs.registry }}/sponsor-app:${{ github.sha }},${{ steps.login-ecr.outputs.registry }}/sponsor-app:latest"
42+
cache-from: type=gha
43+
cache-to: type=gha,mode=max
44+
# prevent manifest from being pushed instead of image on the specified tags for Lambda
45+
provenance: false
46+
sbom: false
5047

5148
deploy-prod:
5249
if: "${{ success() && github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') }}"
5350
name: deploy-prod
54-
needs: ["build"]
51+
needs: ["build", "test"]
5552
permissions:
5653
contents: read
5754
id-token: write
58-
runs-on: ubuntu-latest
55+
runs-on: ubuntu-slim
5956
concurrency:
6057
group: production
6158
cancel-in-progress: true
@@ -64,24 +61,20 @@ jobs:
6461
url: https://sponsorships.rubykaigi.org
6562
env:
6663
BUNDLE_GEMFILE: "${{ github.workspace }}/deploy/Gemfile"
64+
IMAGE_URI: "${{ needs.build.outputs.image-tag }}"
6765
steps:
68-
- run: 'false' # disable temporarily
6966
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
7067
- uses: ruby/setup-ruby@d5126b9b3579e429dd52e51e68624dda2e05be25 # v1.267.0
7168
with:
72-
ruby-version: '3.2'
69+
ruby-version: '3.4'
7370
bundler-cache: true
74-
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
7571
- uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4.3.1
7672
with:
7773
aws-region: "us-west-2"
7874
role-skip-session-tagging: true
7975
role-to-assume: "arn:aws:iam::005216166247:role/GhaSponsorDeploy"
8076
mask-aws-account-id: false
81-
- name: 'Run pre-deploy task'
82-
run: "bundle exec hako oneshot --tag '${{ github.sha }}' ./deploy/hako/sponsor-app-batch.jsonnet bundle exec rake db:migrate"
83-
- name: "Deploy sponsor-app-worker (ECS)"
84-
run: "bundle exec hako deploy --tag '${{ github.sha }}' ./deploy/hako/sponsor-app-worker.jsonnet"
85-
- name: "Deploy sponsor-app (App Runner)"
86-
working-directory: '${{ github.workspace }}/tf/'
87-
run: "terraform init && terraform apply -target=aws_apprunner_service.prd -auto-approve"
77+
- run: 'aws lambda update-function-code --function-name sponsor-app-runner-prd --image-uri "$IMAGE_URI" && aws lambda wait function-updated --function-name sponsor-app-runner-prd'
78+
- run: 'bundle exec ./deploy/lambrunner.rb sponsor-app-runner-prd bundle exec rake db:migrate'
79+
- run: 'aws lambda update-function-code --function-name sponsor-app-lambdakiq-prd --image-uri "$IMAGE_URI" && aws lambda wait function-updated --function-name sponsor-app-lambdakiq-prd'
80+
- run: 'aws lambda update-function-code --function-name sponsor-app-web-prd --image-uri "$IMAGE_URI" && aws lambda wait function-updated --function-name sponsor-app-web-prd'

Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ WORKDIR /app
2727
COPY Gemfile /app/
2828
COPY Gemfile.lock /app/
2929

30-
RUN bundle install --path /gems --jobs 100 --deployment --without development:test
30+
RUN bundle config set deployment true \
31+
&& bundle config set without development:test \
32+
&& bundle config set path /gems \
33+
&& true
34+
ENV BUNDLE_JOBS=100
35+
RUN bundle install
36+
RUN bundle binstubs bundler aws_lambda_ric --force --path /usr/local/bin
3137

3238
###
3339

@@ -39,11 +45,16 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,t
3945
&& rm -rf /var/lib/apt/lists/*
4046

4147
WORKDIR /app
48+
RUN ln -s /tmp/apptmp /app/tmp
4249
COPY --from=builder /gems /gems
4350
COPY --from=builder /app/.bundle /app/.bundle
51+
COPY --from=builder /usr/local/bin/bundle /usr/local/bin/aws_lambda_ric /usr/local/bin
4452
COPY --from=nodebuilder /app/public/vite /app/public/vite
4553
COPY . /app/
54+
COPY config/lambda_entrypoint.sh /lambda_entrypoint.sh
55+
COPY config/docker_entrypoint.sh /docker_entrypoint.sh
4656

4757
ENV PORT 3000
4858
ENV LANG C.UTF-8
59+
ENTRYPOINT ["/docker_entrypoint.sh"]
4960
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

Gemfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'commonmarker'
1212
gem 'aws-sdk-core' # STS
1313
gem 'aws-sdk-s3'
1414
gem 'aws-sdk-sqs'
15+
gem 'aws-sdk-ssm'
1516
gem 'omniauth'
1617
gem 'omniauth-github'
1718
gem 'octokit'
@@ -25,6 +26,7 @@ gem 'rexml', require: false # letter-opener
2526
gem 'jsonnet', require: false
2627

2728
gem 'shoryuken'
29+
gem 'lambdakiq'
2830

2931
gem 'jbuilder', '~> 2.9'
3032
gem 'haml'
@@ -37,10 +39,13 @@ gem "sentry-ruby"
3739
gem "sentry-rails"
3840
gem 'rails_semantic_logger'
3941

40-
gem 'puma'
42+
gem 'puma', require: false
43+
gem 'aws_lambda_ric', require: false
44+
gem 'apigatewayv2_rack'
45+
46+
gem 'open3', require: false
4147

4248
group :production do
43-
gem 'barnes'
4449
end
4550

4651
group :development, :test do

Gemfile.lock

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ GEM
7777
uri (>= 0.13.1)
7878
addressable (2.8.7)
7979
public_suffix (>= 2.0.2, < 7.0)
80+
apigatewayv2_rack (0.4.0)
81+
base64
82+
rack
83+
stringio
8084
aws-eventstream (1.4.0)
8185
aws-partitions (1.1182.0)
8286
aws-sdk-core (3.237.0)
@@ -97,11 +101,12 @@ GEM
97101
aws-sdk-sqs (1.106.0)
98102
aws-sdk-core (~> 3, >= 3.234.0)
99103
aws-sigv4 (~> 1.5)
104+
aws-sdk-ssm (1.206.0)
105+
aws-sdk-core (~> 3, >= 3.234.0)
106+
aws-sigv4 (~> 1.5)
100107
aws-sigv4 (1.12.1)
101108
aws-eventstream (~> 1, >= 1.0.2)
102-
barnes (0.0.9)
103-
multi_json (~> 1)
104-
statsd-ruby (~> 1.1)
109+
aws_lambda_ric (3.1.3)
105110
base64 (0.3.0)
106111
bigdecimal (3.3.1)
107112
builder (3.3.0)
@@ -178,6 +183,11 @@ GEM
178183
mini_portile2 (>= 2.2.0)
179184
jwt (3.1.2)
180185
base64
186+
lambdakiq (2.3.0)
187+
activejob
188+
aws-sdk-sqs
189+
concurrent-ruby
190+
railties
181191
launchy (3.1.1)
182192
addressable (~> 2.8)
183193
childprocess (~> 5.0)
@@ -206,7 +216,6 @@ GEM
206216
mini_mime (1.1.5)
207217
mini_portile2 (2.8.9)
208218
minitest (5.26.1)
209-
multi_json (1.17.0)
210219
multi_xml (0.7.2)
211220
bigdecimal (~> 3.1)
212221
multipart-post (2.4.1)
@@ -246,6 +255,7 @@ GEM
246255
omniauth-oauth2 (1.8.0)
247256
oauth2 (>= 1.4, < 3)
248257
omniauth (~> 2.0)
258+
open3 (0.2.1)
249259
openssl (3.3.2)
250260
pg (1.6.2)
251261
pp (0.6.3)
@@ -372,7 +382,6 @@ GEM
372382
snaky_hash (2.0.3)
373383
hashie (>= 0.1.0, < 6)
374384
version_gem (>= 1.1.8, < 3)
375-
statsd-ruby (1.5.0)
376385
stringio (3.1.8)
377386
temple (0.10.4)
378387
thor (1.4.0)
@@ -404,10 +413,12 @@ PLATFORMS
404413

405414
DEPENDENCIES
406415
addressable
416+
apigatewayv2_rack
407417
aws-sdk-core
408418
aws-sdk-s3
409419
aws-sdk-sqs
410-
barnes
420+
aws-sdk-ssm
421+
aws_lambda_ric
411422
commonmarker
412423
connection_pool
413424
factory_bot_rails
@@ -419,12 +430,14 @@ DEPENDENCIES
419430
jbuilder (~> 2.9)
420431
jsonnet
421432
jwt
433+
lambdakiq
422434
letter_opener_web
423435
listen
424436
nokogiri
425437
octokit
426438
omniauth
427439
omniauth-github
440+
open3
428441
openssl
429442
pg (>= 0.18, < 2.0)
430443
premailer-rails

config.ru

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
# This file is used by Rack-based servers to start the application.
2-
31
require_relative "config/environment"
42

3+
if ENV['AWS_LAMBDA_FUNCTION_NAME']
4+
use Apigatewayv2Rack::Middlewares::CloudfrontVerify, ENV['CLOUDFRONT_VERIFY'] if ENV['CLOUDFRONT_VERIFY']
5+
use Apigatewayv2Rack::Middlewares::CloudfrontXff
6+
end
7+
58
run Rails.application
69
Rails.application.load_server

config/application.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,23 @@ class Application < Rails::Application
4242
config.i18n.default_locale = :en
4343
config.i18n.available_locales = [:en, :ja]
4444
config.i18n.fallbacks = [:en]
45+
46+
begin
47+
config.semantic_logger.application = "sponsor-app"
48+
config.semantic_logger.environment = Rails.env
49+
config.rails_semantic_logger.started = :info
50+
51+
config.log_tags = {
52+
request_id: :request_id,
53+
method: :request_method,
54+
path: :path_info,
55+
ip: :ip,
56+
}
57+
58+
if ENV["RAILS_LOG_TO_STDOUT"].present?
59+
config.rails_semantic_logger.add_file_appender = false
60+
config.semantic_logger.add_appender(io: $stdout, formatter: :json)
61+
end
62+
end
4563
end
4664
end

config/boot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
22

33
require "bundler/setup" # Set up gems listed in the Gemfile.
4+
5+
require_relative 'lambda_boot' if ENV['AWS_LAMBDA_FUNCTION_NAME']

config/docker_entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
mkdir -p /tmp/apptmp
3+
exec "$@"

config/environments/development.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
# Change to :null_store to avoid any caching.
3131
config.cache_store = :memory_store
3232

33-
config.active_job.queue_adapter = :inline
34-
if ENV['ENABLE_SIDEKIQ']
35-
config.active_job.queue_name_prefix = "sponsor_app"
36-
config.active_job.queue_adapter = :sidekiq
37-
end
33+
# see also config/initializers/active_job.rb
34+
# config.active_job.queue_adapter = :inline
3835

3936
if ENV['MAILGUN_SMTP_PASSWORD']
4037
config.action_mailer.smtp_settings = {

config/environments/production.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@
7575
secure: true,
7676
)
7777

78-
# Use a real queuing backend for Active Job (and separate queues per environment)
79-
config.active_job.queue_adapter = ENV.fetch('ENABLE_SHORYUKEN', '1') == '1' ? :shoryuken : :inline
80-
# config.active_job.queue_name_prefix = "sponsor_app2_production"
78+
# see also config/initializers/active_job.rb
79+
# config.active_job.queue_adapter = :inline
8180

8281
# Disable caching for Action Mailer templates even if Action Controller
8382
# caching is enabled.

0 commit comments

Comments
 (0)