AWSのこと

SAMを使ってみる

SAMのハンズオンをやってみた。

SAMとは、Serverless Application Modelの接頭辞で、サーバレスアプリケーションを素早く開発するためのツールセットです。CloudFormationにより、必要なリソースのデプロイが行われます。今回は、Cloud9環境を用いてハンズオンを行いました。

SAMの使い方

大きな流れは以下の通りです。

  1. サーバレスアプリケーションの雛形作成(sam init
  2. コーディング
  3. ビルド(sam build
  4. ローカル環境でのテスト(sam local start-api
  5. デプロイ(sam deploy

サーバレスアプリケーションの雛形作成

sam init

今回は、以下のような構成を取りました。

  • テンプレートの種類: AWS Quick Start Templates
  • 使用するテンプレート: Serverless API
  • ランタイム: Node.js 16.x
  • X-Rayの使用有無: No X-Ray
  • アプリケーション名: sam-app
sam init

以下のとおり選択していきます。

You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.

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 - Multi-step workflow
        3 - Serverless API
        4 - Scheduled task
        5 - Standalone function
        6 - Data processing
        7 - Infrastructure event management
        8 - Lambda EFS example
        9 - Machine Learning
Template: 3

Which runtime would you like to use?
        1 - dotnet6
        2 - dotnetcore3.1
        3 - nodejs16.x
        4 - nodejs14.x
        5 - nodejs12.x
Runtime: 3

Based on your selections, the only Package type available is Zip.
We will proceed to selecting the Package type as Zip.

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

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

Project name [sam-app]: sam-app

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

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app
    Runtime: nodejs16.x
    Architectures: x86_64
    Dependency Manager: npm
    Application Template: quick-start-web
    Output Directory: .
    
    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: sam validate
    [*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
    

SAM CLI update available (1.70.0); (1.57.0 installed)
To download: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html

コーディング

ディレクトリ構成を確認しておきます。

ブログ環境をGitHubからAWSに移行しました

今までの構成

今までは、静的CMSであるHexoを使ってローカル環境で各ページを生成し、生成されたページをGitHub Pagesで公開する、という構成を取っていました。

この構成を取っていた理由は以下の2つです。

  • ブログ記事をMarkdown形式で書くことができ、HTMLを作るために必要なテンプレートや設定ファイルをgitで管理できるため
  • GitHub Pagesは、リポジトリにgit pushさえすればページが公開され、かつ無料で気軽に使うことができるため

しかし思うところがあり、今回、この構成をAWSに移行することにしました。