LoginSignup
1
0

AWS SAM入門してみた

Last updated at Posted at 2023-12-04

はじめに

jenkinsおじさんになりたくないって言いながらIaCらへんから逃げてきたにわのわです。
筋トレするとたちまちムキムキになるわけないのと一緒で
IaCできるからってjenkinsおじさんになるわけではないですからね。
やりましょう。

ちなみに一番好きなjenkinsおじさんはこれ
image.png

今回やること

デベロッパーガイドを読んで、解釈してHello Worldすることをゴールとします。

AWS Serverless Application Model (AWS SAM) とは

AWS Serverless Application Model (AWS SAM) は、AWS 上でサーバーレスアプリケーションを構築および実行するデベロッパーのエクスペリエンスを改善するツールキットです。

ふむ、なるほど。
これを使うとコマンドを叩くとよしなにAPI Gatewayとかlambdaとかを構成してくれるはず。

インストール

これまたデベロッパーガイドを元にインストールしていきます。

2023 年 9 月以降、AWS では AWS で管理される AWS SAM CLI (aws/tap/aws-sam-cli) 用 Homebrew インストーラーのメンテナンスを行いません。

とのことなので、brew installは使いません。

pkgファイルをダウンロードして以下のコマンドを叩きました。

sudo installer -pkg ~/Downloads/aws-sam-cli-macos-x86_64.pkg -target / 

こんな感じ
image.png

ガイドさんは親切なので動作確認まで書いてくれてます。

image.png

Hello World

例によってデベロッパーガイドを元にやっていきます。

ステップ 1: サンプルの Hello World アプリケーションを初期化する

sam init以降の入力を参考までに残します。

~/D/aws_sam_hello_world ❯❯❯ 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 - 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 - Hello World Example With Powertools for AWS Lambda
        16 - DynamoDB Example
        17 - Machine Learning
Template: 

1を入力

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

yを入力

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/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]: 

Project name [sam-app]:

3回ほどエンターキー連打(デフォルトでよい)

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

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app
    Runtime: python3.9
    Architectures: x86_64
    Dependency Manager: pip
    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

こんな感じでできました。

ステップ 2: アプリケーションを構築する

sam buildを叩くとlambdaへデプロイするようにディレクトリを作ってくれたり、
関連するcfnテンプレートを作ってくれるっぽいです。

cd sam-app
sam build

ステップ 7: (オプション) アプリケーションをローカルでテストする

数字が飛びました。
本来のステップ3はデプロイなのですが、ローカルで動かすほうが先でしょ、ということでこの順番にしています。

以下のコマンドで実行ができます。
app.pyのlambda_handlerが呼び出されます。

sam local invoke

--eventオプションでイベントを渡すことができることと、
--env-varsオプションで環境変数が設定できることは覚えておくと役立つかもです。

以下のコマンドでlambdaのホストができます。
こっちがメインで使いそう

sam local start-api

ちょっとcurlが文句言ってますがうまく動いてますね。
image.png

ステップ 3: アプリケーションを AWS クラウド にデプロイする

きちんと動くことが確認出たのでデプロイしていきます。

sam deploy --guided

チュートリアルでは以下の要素がデフォルトとは異なった設定となっているので注意してください。

 Confirm changes before deploy [Y/n]: n
 HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y

以下のように表示されれば成功

Successfully created/updated stack - sam-app in ap-northeast-1

早速アクセスしてみるとうまくデプロイできているっぽいです。
image.png

マネコンからも確認できました。

Amazon API Gateway
image.png

AWS Lambda
image.png

おわりに

AWS SAMを使ってHello Worldをデプロイするところまでやってみました。
思わず僕もサムズアップ(これが言いたかった)

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