2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

aws-sdkを用いてAWS SecretManagerからシークレットを取得する(Node.js TypeScript)

Posted at

Node.js&TypeScript&lambda環境、aws-sdkを用いて、AWS SecretManagerからシークレットを取得する方法を解説します。

1. aws-sdkをinstall

npm install aws-sdk

2. コードを記述

referSecrets.ts
import * as AWS from "aws-sdk";
export const referSecrets = async () => {
  try {
    const secretsManager = new AWS.SecretsManager({
      region: "ap-northeast-1",
    })
    const response = await secretsManager.getSecretValue({
      SecretId: SECRET_NAME,
    }).promise()
    return JSON.parse(response.SecretString)
  } catch(err) {
    return JSON.stringify({err}, null, 2)
  }
}

注意点

  • requireではなく、importを使用する。
    • typeが使用でき、vscodeがメソッドや型を推論してくれます。
  • SECRET_NAMEには、シークレット名を記述
  • iamroleなどの設定は省いています。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?