FCM Push用秘密鍵を SecretManager に登録する

FCM 秘密鍵を取得

  1. FireBaseへログイン
  2. プロジェクトを選択
  3. プロジェクトの設定
  4. サービスアカウント
  5. FireBase Admin SDK > 新しい秘密鍵の生成

FCM 秘密鍵を SecretManager に登録

  1. 取得した秘密鍵を ./fcm.json で配置する
  2. 以下コマンドで登録
aws secretsmanager put-secret-value --secret-id ${SECRET_ID} --secret-string "$(cat<./fcm.json)"

使う

SecretManager を扱い易いようにクラス化する

'use strict';

const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');

const clazz = class Sm {
  constructor() {
    this.client = new SecretsManagerClient({ region: process.env.REGION }); // REGION で環境変数へRegionを設定している前提
  }
  async get(key) {
    const command = new GetSecretValueCommand({ SecretId: key });
    const res = await this.client.send(command);
    return JSON.parse(res.SecretString);
  }

  async getBinary(key) {
    const command = new GetSecretValueCommand({ SecretId: key });
    const res = await this.client.send(command);
    return res.SecretBinary;
  }
};

module.exports = () => {
  return new clazz();
};
// 初期化
const sm = require('./sm/index.js');
const res = await this.sm.get(process.env.SECRET_ID); // SCRET_ID で環境変数へ登録している前提
admin.initializeApp({
  credential: admin.credential.cert(res),
});

// 省略

// Push通知送信
const msg = {
  token,
  notification: {
    title,
    body,
    imageUrl,
  },
  data: {
    notification_foreground: 'true',
  },
};
await admin.messaging().send(msg);

Refs