LoginSignup
0
1

More than 1 year has passed since last update.

Amplify CLI でカスタムリソースを作成する手順

Last updated at Posted at 2021-06-25

Add custom AWS resources
Amplify CLI で対応していない設定やリソースを使用したいときはカスタムリソースを作成する必要がある。
つまり、amplify CLI で自動生成された CloudFormation テンプレートを手動で書き換える。この時の手順に関して、上記のドキュメントを改めて確認したメモ。

まとめ

  • Amplify CLI で対応していないリソースや細かな設定は CloudFormation テンプレートを手動で変更する必要がある
  • 手動で変更した CloudFormation テンプレートは何かの拍子(amplify update xxxなど)で上書きされて消える可能性があるので保管方法には注意が必要
  • 参照しているドキュメントはカスタムリソースの作成についてであり、CloudFormation スタックを新規に作成している。しかしfunctionなど既に Amplify CLI から作成できるが設定を細かく変更したいという場合には自動生成されたテンプレートを編集する形になる(functionカテゴリの場合にはamplify/backend/function/<function-name>/<function-name>-cloudformation-template.json)。この記事では特に触れていない

事前準備

手元に実際に amplify プロジェクトが欲しかったため、以下を実行しておく。

amplify init
amplify add storage  # storage の他に auth, functionも一度に追加できる

functionstorage のテンプレートはそれぞれ以下のファイル。普通の CloudFormation テンプレート
function
amplify/backend/function/<function-name>/<function-name>-cloudformation-template.json
storage
amplify/backend/storage/<bucket-name>/s3-cloudformation-template.json

ドキュメント内容確認

1. Configure backend configuration with custom resource

Modify amplify/backend/backend-config.json in your project:

backend-config.jsonの編集が必要らしい。backend-config.jsonとは。
Core Amplify Filesに説明がある。

backend-config.json

Manual edits okay: YES
Add to version control: YES

The backend-config.json in the backend directory contains configuration about your project’s backend, such as how connects to AWS resources (eg. Cognito for auth or AppSync for an API backend). Typically, this file is updated by the CLI commands like amplify add auth or amplify add api. It can also be extended manually to configure your backend beyond Amplify CLI’s features. Both the amplify/backend and amplify/#current-cloud-backend directories contain an backend-config.json file.

具体的に何をしているファイルなのかピンとこなかったが、手元に作成された以下のbackend-config.jsonを見るとバックエンドリソースの依存関係(CloudFormationスタックのデプロイ順序)を表しているように見える。dependsOnくらいしか有益な情報なさそう。

{
  "auth": {
    "<auth-name>": {
      "service": "Cognito",
      "providerPlugin": "awscloudformation",
      "dependsOn": [],
      "customAuth": false
    }
  },
  "function": {
    "<function-name>": {
      "service": "Lambda",
      "providerPlugin": "awscloudformation",
      "build": true
    }
  },
  "storage": {
    "<bucket-name>": {
      "service": "S3",
      "providerPlugin": "awscloudformation",
      "dependsOn": [
        {
          "category": "function",
          "resourceName": "<function-name>",
          "attributes": [
            "Name",
            "Arn",
            "LambdaExecutionRole"
          ]
        }
      ]
    }
  }
}

作業としては実際に何をするかというと、backend-config.jsonファイルに他のリソースを真似てカスタムカテゴリーをかく。ドキュメントのサンプルは↓。<custom-category-name>の部分が上記の自動生成されたテンプレートのfunctionauthにあたる。

  "<custom-category-name>": {
    "<custom-resource-name>": {
      "service": <custom-aws-service-name>,
      "providerPlugin": "awscloudformation"
    }
  }

2. Modify folder structure for custom resource

Under amplify/backend folder, make a folder structure like the following:

amplify
  \backend
    \<custom-category-name>
      \<custom-resource-name>
        parameters.json
        template.json
  1. で定義したbackend-config.jsonに記載の通りにディレクトリを作って、そこに CloudFormation のテンプレートを作成するよう。

3. (Optional) Reference existing parameters

必要なら dependsOnで依存関係を追加してという内容。

4. Enable custom resources

amplify env checkout <current-env-name>をする必要があるとのこと。やらないと amplify pushで反映されないとかだろうか?

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