2
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?

【AWS SAM】入門してみた dockerで環境構築 ruby

Last updated at Posted at 2024-07-31

はじめに

今までserverless frame workを使用して、lambdaのデプロイを行ってきたのですが、serverless frame work V4から有料化ということを受けてSAMへ移行したいと思いました。

環境構築

dockerで行いました。

  • ruby3.3.4
  • node 20.16.0
  • aws-sam-cliをzipで公式ページから取得しインストール

FROM ruby:3.3.4-slim-bookworm

RUN useradd -u 1000 developper \
  && apt-get update -qq && apt-get install -qq --no-install-recommends \
    curl awscli unzip wget git vim

RUN wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
RUN unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
RUN ./sam-installation/install
RUN rm -rf aws-sam-cli-linux-x86_64.zip ./sam-installation

RUN gem install bundler -v 2.5.11

RUN chown -R developper:developper /usr/local/bundle
COPY --chown=developper:developper . /home/developper/app
WORKDIR /home/developper/app
RUN chown -R developper:developper /home/developper/app
USER developper

テンプレート生成

  • sam init 実行
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Data processing
        3 - Hello World Example with Powertools for AWS Lambda
        4 - Multi-step workflow
        5 - Scheduled task
        6 - Standalone function
        7 - Serverless API
        8 - Infrastructure event management
        9 - Lambda Response Streaming
        10 - Serverless Connector Hello World Example
        11 - Multi-step workflow with Connectors
        12 - GraphQLApi Hello World Example
        13 - Full Stack
        14 - Lambda EFS example
        15 - DynamoDB Example
        16 - Machine Learning
Template: 1

Use the most popular runtime and package type? (Python and zip) [y/N]: n

Which runtime would you like to use?
        1 - dotnet8
        2 - dotnet6
        3 - go (provided.al2)
        4 - go (provided.al2023)
        5 - graalvm.java11 (provided.al2)
        6 - graalvm.java17 (provided.al2)
        7 - java21
        8 - java17
        9 - java11
        10 - java8.al2
        11 - nodejs20.x
        12 - nodejs18.x
        13 - nodejs16.x
        14 - python3.9
        15 - python3.8
        16 - python3.12
        17 - python3.11
        18 - python3.10
        19 - ruby3.3
        20 - ruby3.2
        21 - rust (provided.al2)
        22 - rust (provided.al2023)
Runtime: 19

What package type would you like to use?
        1 - Zip
        2 - Image
Package type: 1

Based on your selections, the only dependency manager available is bundler.
We will proceed copying the template using bundler.

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: n

Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: n

Would you like to set Structured Logging in JSON format on your Lambda functions?  [y/N]: n

Project name [sam-app]:  
                                                                                                                                                                                          
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)                                                                                                 

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app
    Runtime: ruby3.3
    Architectures: x86_64
    Dependency Manager: bundler
    Application Template: hello-world
    Output Directory: .
    Configuration file: sam-app/samconfig.toml
    
    Next steps can be found in the README file at sam-app/README.md
        

Commands you can use next
=========================
[*] Create pipeline: cd sam-app && sam pipeline init --bootstrap
[*] Validate SAM template: cd sam-app && sam validate
[*] Test Function in the Cloud: cd sam-app && sam sync --stack-name {stack-name} --watch

lambdaにデプロイ

sam deploy --guided

Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Found
        Reading default arguments  :  Success

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [sam-app]: sam-test-app   
        AWS Region [ap-northeast-1]: 
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [Y/n]: y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: n
        HelloWorldFunction has no authentication. Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: y
        SAM configuration file [samconfig.toml]: 
        SAM configuration environment [default]: 

        Looking for resources needed for deployment:
        Creating the required resources...
        Successfully created!

おわりに

一旦samで提供された、templateをlambdaにデプロイしてみました。
まだ全体像が見えなくて、なんとも言えないですが
templateにtestのコードが含まれていたりdockerを入れればローカルでサーバーを立ち上げられたりとslsに比べて標準機能が充実していそうでした。

でも、slsに比べてプラグインが無い・・

後日調べてわかったこと

  • aws samではserverless frameworkのように.envファイルの読み込みが行えなさそう。.jsonファイルの読み込みは出来そうなので.envから.jsonファイルへ書き換え必要そう
  • slsで使っていたstageパラメータの代替えとして、--config-env,--config-fileというオプションでconfigファイルの値を変えることができそう

参考

2
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
2
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?