Amplify CLI を用いて定期実行の Lambda 関数をデプロイする

記事タイトルとURLをコピーする

こんにちは、技術1課の加藤です。

今回は、AWS Amplify のお話。簡単にアプリケーションが構築できちゃう超便利サービスな Amplify なわけですが、一定の型から外れようとするとなかなか苦労する印象がありました。

しかし機能の拡充は進み、今となってはバッチ処理用の Lambda 関数を用意するのもお茶の子さいさいとのこと。

素敵な機能だと思ったので試してみました。

手順

  1. Amplify プロジェクトの作成
  2. Lambda 関数の追加
  3. デプロイ

以下のブログを参考にしています。
How to schedule recurring Lambda functions using the Amplify CLI

1. Amplify プロジェクトの作成

Amplify CLI のインストールや amplify configure の実行がまだの方はGet started - Installation - Amplify Docs に従い事前準備をお願いします。

上記が完了していましたら、ターミナルを開き、任意の場所で以下コマンドを実行します。エディタ設定は環境に合わせてお好きなものを選んでください。

$ mkdir amplify-lambda-sample
$ cd amplify-lambda-sample
$ amplify init
Scanning for plugins...
Plugin scan successful
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project amplifylambdasample
? Enter a name for the environment dev
? Choose your default editor: Vim (via Terminal, Mac OS only)
? Choose the type of app that you're building javascript
Please tell us about your project
? What javascript framework are you using none
? Source Directory Path:  src
? Distribution Directory Path: dist
? Build Command:  npm run-script build
? Start Command: npm run-script start
Using default provider  awscloudformation

For more information on AWS Profiles, see:
https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html

? Do you want to use an AWS profile? Yes
? Please choose the profile you want to use default

2. Lambda 関数の追加

では今回の本題、Lambda 関数の追加を行なっていきます。

コマンドは以下。

$ amplify add function
Using service: Lambda, provided by: awscloudformation
? Provide a friendly name for your resource to be used as a label for this categ
ory in the project: amplifylambdasamplexxxxxx
? Provide the AWS Lambda function name: amplifylambdasamplexxxxxx
? Choose the function runtime that you want to use: NodeJS
? Choose the function template that you want to use: Hello World
? Do you want to access other resources created in this project from your Lambda
 function? No
? Do you want to invoke this function on a recurring schedule? Yes
? At which interval should the function be invoked: Minutes
? Enter the rate in minutes: 1
? Do you want to edit the local lambda function now? No
  • 定期実行 (毎分1回): 正しく実行されているか素早く確認するため
  • Node.js
  • HelloWorld テンプレートを使用

という設定を行いました。

関数のテンプレートとして、REST API や( ドキュメント見ると serverless-express ベースになってるみたい) 、DynamoDB Stream や Kinesis Stream に対応する Lambda Trigger が存在する様子。

また API に AppSync(GraphQL) を利用している場合、schema.json にて@function ディレクティブを使ってあげるとここで作った関数をデータソースとして利用することができるとのこと。
Amplify プロジェクトで使いたくなる Lambda の用途はだいたい網羅している感じがしますね。良き良き。

ひとまず今回はテストということで、通常の関数を作成するだけの HelloWorld テンプレートを選びました。
すると amplify-lambda-sample/amplify/backend/function/amplifylambdasamplexxxxxx/src 内にLambda のコードが用意されます。何気に event.json とかも用意されているのでローカルテストもできそうです。

3. デプロイ

ではデプロイしてみましょう。以下コマンドを実行します。

$ amplify push
✔ Successfully pulled backend environment dev from the cloud.

Current Environment: dev

| Category | Resource name               | Operation | Provider plugin   |
| -------- | --------------------------- | --------- | ----------------- |
| Function | amplifylambdasamplexxxxx | Create    | awscloudformation |
? Are you sure you want to continue? Yes

これでリソースが AWS に作られ、 1分毎に "HelloWorld" をリターンし続けるはずです。

AWS マネージメントコンソールで Lambda を開いてみると、きちんと想定通りの Lambda 関数が作成されていることが確認できます。

また少々時間をおいて [モニタリング]> [CloudWatch のログを表示] から実行ログを見てみましょう。1分おきに関数が実行されていることも確認できます。

環境の削除

環境を削除する際には以下のコマンドを実行します。

$ amplify delete 
? Are you sure you want to continue? (This would delete all the environments of the project from the cloud and wipe out all the local amplify resource files) Yes

まとめ

Amplify CLI を使って、 Lambda 関数のデプロイを行ってみました。

非常に簡単な手順で行えますし、これで Amplify プロジェクトないに閉じた形で関数を管理できるので非常に便利だと感じます。

ぜひ皆さんも使ってみてください。

Amplify 関連記事