3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lambyを使ってみる

Last updated at Posted at 2024-02-14

はじめに

先月、AWS SAMを利用してrubyをlambdaで動かせることは分かったのですが、railsをlambdaで動かせないかと調べたところ、lambyが出てきたのでこの記事は調査禄となります。
(lambyそのものは前々から知ってたものの、今まで触ったことがなかったので今回が初となります)

スタートアップに準じて触ってみる

準備

quick-start記載の以下のコマンドを実行

$ docker run \
  --rm \
  --interactive \
  --volume "${PWD}:/var/task" \
  ghcr.io/rails-lambda/lamby-cookiecutter \
  "gh:rails-lambda/lamby-cookiecutter"

プロジェクト名を入力

project_name [my_awesome_lambda]: learning_deliverable

コンテナの中に入り、awsの設定を行う

aws configure

開発環境

初期設定などを実施

./bin/setup
./bin/setupの内容
#!/bin/sh
set -e

echo '== Installing dependencies =='
bundle install

echo "== Preparing database =="
./bin/rails db:prepare

echo "== Removing old logs and tempfiles =="
./bin/rails log:clear tmp:clear

echo "== Restarting application server =="
./bin/rails restart

# ./bin/yarn

ローカルでサーバー起動

./bin/rails s

AWSへデプロイ

テンプレートを変更

template.yaml

Globals:
  Function:
    Architectures:
-     - arm64
+     - x86_64

gemfileの最後に追記

gem 'rack' , '2.2.6.4'

本番用のdockerfileを修正

Dockerfile
# Shared image, envs, packages for both devcontainer & prod.
FROM ruby:3.2-bullseye

# Install the AWS Lambda Runtime Interface Client & Crypteia for secure SSM-backed envs.
RUN gem install 'aws_lambda_ric'
COPY --from=ghcr.io/rails-lambda/crypteia-extension-debian:1 /opt /opt
ENTRYPOINT [ "/usr/local/bundle/bin/aws_lambda_ric" ]
ENV LD_PRELOAD=/opt/lib/libcrypteia.so

# Create a secure user for prod and app directory.
RUN mkdir /app \
    && groupadd -g 10001 app \
    && useradd -u 10000 -g app app \
    && chown -R app:app /app
USER app
WORKDIR "/app"

# Copy prod application files and set handler.
ENV BUNDLE_IGNORE_CONFIG=1
ENV BUNDLE_PATH=./vendor/bundle
ENV BUNDLE_CACHE_PATH=./vendor/cache
ENV RAILS_SERVE_STATIC_FILES=1
COPY . .
+ RUN bundle exec rails assets:precompile RAILS_ENV=production

CMD ["config/environment.Lamby.cmd"]

アカウントの権限は前回と同じものを利用します
https://qiita.com/youfuku/items/38f582889a05027f713f

./bin/deploy
./bin/deployの内容
#!/bin/sh
set -e

RAILS_ENV=${RAILS_ENV-production}

AWS_REGION=${AWS_REGION-$(aws configure get region || echo 'us-east-1')}
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
IMAGE_REPOSITORY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/learning-deliverable"

if [ "$CI" != "true" ]; then
  echo "== Cleaning dev dependencies for local deploy. Run ./bin/setup again afterward! =="
  rm -rf ./.bundle \
         ./vendor/bundle
fi

echo '== Create ECR Repo if needed. =='
aws ecr describe-repositories \
  --repository-names "learning-deliverable" \
  --region "$AWS_REGION" > /dev/null || \
aws ecr create-repository \
  --repository-name "learning-deliverable" \
  --image-tag-mutability "MUTABLE" \
  --image-scanning-configuration "scanOnPush=true" \
  --region "$AWS_REGION" > /dev/null || true

echo '== Bundle For Deployment =='
bundle config --global silence_root_warning true
bundle config --local deployment true
bundle config --local without 'development test'
bundle config --local path './vendor/bundle'
bundle install --quiet --jobs 4

echo "== Asset Hosts & Precompiling =="
NODE_ENV='production' ./bin/rails assets:precompile

if [ "$CI" = "true" ]; then
  echo "== Cleanup Unused Files & Directories =="
  rm -rf \
    log \
    node_modules \
    test \
    tmp \
    vendor/bundle/ruby/*/cache
fi

echo "== SAM build =="
sam build \
  --parameter-overrides \
    RailsEnv="${RAILS_ENV}"

echo "== SAM package =="
sam package \
  --region "$AWS_REGION" \
  --template-file ./.aws-sam/build/template.yaml \
  --output-template-file ./.aws-sam/build/packaged.yaml \
  --image-repository "$IMAGE_REPOSITORY"

echo "== SAM deploy =="
sam deploy \
  --region "$AWS_REGION" \
  --template-file ./.aws-sam/build/packaged.yaml \
  --stack-name "learning-deliverable-${RAILS_ENV}" \
  --image-repository "$IMAGE_REPOSITORY" \
  --capabilities "CAPABILITY_IAM" \
  --parameter-overrides \
    RailsEnv="${RAILS_ENV}"

if [ "$CI" != "true" ]; then
  echo "== Cleaning prod deploy dependencies from local. =="
  rm -rf ./.bundle \
         ./vendor/bundle \
         ./node_modules \
         ./public/assets
fi

完了したら、こんな感じにログが出力される

CloudFormation outputs from deployed stack
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs                                                                                                                                                                                                          
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Key                 RailsLambdaUrl                                                                                                                                                                               
Description         Lambda Function URL                                                                                                                                                                          
Value               https://q6v2mqfadjczbm4t3dtzywb3ni0tregp.lambda-url.ap-northeast-1.on.aws/                                                                                                                   
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

URLにアクセスすると、railsの初期ページが表示されてました!
image.png

後片付け

リポジトリの削除

aws ecr delete-repository --repository-name "learning-deliverable" --force
sam delete --stack-name "learning-deliverable-production"

さいごに

今回、quick-startを元に初期ページを表示しました。
これが何処まで通用するのかは分からないので、何かを作りながら試そうかと思います

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?