Amplify Logger から CloudWatchLogsに送信する の補足、或いはAmplifyで未ログイン状態でもAWSリソースを操作したいときの覚書

Amplifyを組み込んだアプリケーションからCloudWatchLogsにログイベントを送信するには amplify add auth で認証関連の機能を組み込んでログ送信用のIAMロールを作る必要があると書いた。

jacoyutorius.hatenablog.com

amplify add authを実行すると対話的に認証設定ができるのだけど、デフォルトの設定だけでは未ログインのときにはAWS側の操作ができない状態になるので このコマンドによって生成されたamplify ディレクトリ配下のファイルを一箇所書き換えてあげる必要がある。

認証機能をつけない場合はだいたい適当に答える感じでOK。

ddd4143c63e0:/workspaces/amplify-dev# amplify add auth
Using service: Cognito, provided by: awscloudformation
 
 The current configured provider is Amazon Cognito. 
 
 Do you want to use the default authentication and security configuration? Default configuration
 Warning: you will not be able to edit these selections. 
 How do you want users to be able to sign in? Email
 Do you want to configure advanced settings? No, I am done.
Successfully added auth resource amplifyapp locally

Some next steps:
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud
$  tree amplify 
amplify
├── backend
│   ├── amplify-meta.json
│   ├── auth
│   │   └── amplifyapp1122334455
│   │       ├── amplifyapp1122334455-cloudformation-template.yml
│   │       └── parameters.json   # これをいじる

この parameters.jsonallowUnauthenticatedIdentitiestrue に書き換える。

{
    "identityPoolName": "amplifyapp1122334455_identitypool_00aabbcc",
    "allowUnauthenticatedIdentities": true, 
    "resourceNameTruncated": "amplifyapp1122334455",
    "userPoolName": "amplifyapp1122334455_userpool_00aabbcc",
    "autoVerifiedAttributes": [
        "email"
    ],

この変更をしてから amplify push を実施すると以下のようにフェデレーテッドアイデンティティが作成される。

f:id:jacoyutorius:20211104054355j:plain
認証されていないIDのところ

これでログインしていなくてもAWS側のリソースを扱うことができるようになる。

もしAuth関連のリソースが作成済の場合は amplify update auth を実行し、Walkthrough all the auth configurations を選んで

Allow unauthenticated logins? (Provides scoped down permissions that you can control via AW S IAM)Yes にする。

その他

どのファイルで設定しているのかわからなくてdiffで頑張って調べました。

f:id:jacoyutorius:20211104055714j:plain