0
0

More than 3 years have passed since last update.

【AWS SAM CLI】GoからRubyへの移行

Last updated at Posted at 2021-08-20

はじめに

AWS SAM CLIでランタイムにGoを使っているプロジェクトがありましたが、プロジェクトを作った人は退職してしまいました。
私含め、他のメンバーはRubyでの開発が主であり、Goの知識が薄い状態です。
そこで、GoからRubyに書き直してメンテナンスしやすくした時にやった事をまとめました。

環境

  • SAM CLI, version 1.27.2
  • ランタイム, go1.x => Ruby2.7

1. Rubyプロジェクトの初期化

sam initコマンドを使って新規のRubyプロジェクトを立ち上げました。
ディレクトリ構成は下記のようにしました。

sam_cli
 |
 |-- sam-app-go # Goで書かれたプロジェクト(既存)
 |
 |-- sam-app-ruby # Rubyで書かれたプロジェクト(新規)

以下は対話型でruby2.7のランタイムを指定して作成している例です。

Which template source would you like to use?
    1 - AWS Quick Start Templates
    2 - Custom Template Location
Choice: 1
What package type would you like to use?
    1 - Zip (artifact is a zip uploaded to S3)
    2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 1

Which runtime would you like to use?
    1 - nodejs14.x
    2 - python3.8
    3 - ruby2.7
    4 - go1.x
    5 - java11
    6 - dotnetcore3.1
    7 - nodejs12.x
    8 - nodejs10.x
    9 - python3.7
    10 - python3.6
    11 - python2.7
    12 - ruby2.5
    13 - java8.al2
    14 - java8
    15 - dotnetcore2.1
Runtime: 3

Project name [sam-app]: sam-app-ruby

Cloning from https://github.com/aws/aws-sam-cli-app-templates

AWS quick start application templates:
    1 - Hello World Example
    2 - Step Functions Sample App (Stock Trader)
Template selection: 1

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app-ruby
    Runtime: ruby2.7
    Dependency Manager: bundler
    Application Template: hello-world
    Output Directory: .

    Next steps can be found in the README file at ./sam-app-ruby/README.md

2. コードをGoからRubyへ移行

Goで書かれたLambda関数のコードをRubyに移行します。
importしているライブラリをLambda関数を定義してあるフォルダのGemfileに置き換えていきます。
プロジェクト直下にあるGemfileはテストで使うファイルなので、テストを書く場合は、ここにも記載します。

3. template.ymlの修正

Go側のResourcesでLambda関数を定義している箇所をRuby側のtemplate.ymlにコピーします。
HandlerやRuntimeプロパティを修正していきます。
修正後、sam validateでチェックしましょう。
例として、以下にsam initで作成した初期テンプレートの変更箇所を上げます。

template.yml
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
-      Handler: hello-world
-      Runtime: go1.x
-      Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
+      Handler: app.lambda_handler
+      Runtime: ruby2.7
       Events:
-          CatchAll:
+          HelloWorld
           Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
           Properties:
           Path: /hello
           Method: GET
-      Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
-        Variables:
-          PARAM1: VALUE

4. sam deploy

sam deployコマンドでデプロイします。
スタック名は別名に変えてデプロイしました。(同じスタック名でデプロイするとどうなるかは未確認)
AWS Cloudformationにアクセスしてデプロイが出来ていたら完了です!

まとめ

sam initで作成されるテンプレートを基にRubyに移行しました。
Goを勉強した事がなく、Rubyに書き直すのに苦労しましたが、いい経験になりました。

0
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
0
0