0Pricing
Serverless Backend with AWS Lambda & API Gateway · レッスン

AWS Secrets Managerによるシークレット保護

Lambdaに認証情報をハードコードするのはやめましょう。AWS Secrets Managerを使ってAPIキーやデータベースのパスワードを安全に保存、ローテーション、取得する方法を学びます。

「AWS Secrets Managerによるシークレット保護」はCoddyKit上の無料Serverless Backend with AWS Lambda & API Gatewayレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはServerless Backend with AWS Lambda & API Gateway学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Not Hardcode Secrets?

Embedding API keys or DB passwords in your Lambda code or environment variables is risky: anyone with read access can see them, and rotating them means a redeploy.

AWS Secrets Manager centralizes secrets, encrypts them, and supports automatic rotation.

What Secrets Manager Stores

Common secrets include:

  • Database credentials
  • Third-party API keys
  • OAuth tokens
  • Encryption keys

Each secret is encrypted at rest with AWS KMS.

Creating a Secret

Secrets are stored as key/value JSON. You can create one from the CLI.

aws secretsmanager create-secret \
  --name prod/db/credentials \
  --secret-string '{"username":"admin","password":"S3cr3t!"}'

Granting Lambda Access

Your Lambda execution role needs permission to read the secret. Scope it to the exact ARN, not a wildcard.

{
  "Effect": "Allow",
  "Action": "secretsmanager:GetSecretValue",
  "Resource": "arn:aws:secretsmanager:us-east-1:123:secret:prod/db/credentials-*"
}

Retrieving a Secret in Code

Use the AWS SDK to fetch the secret value at runtime.

const { SecretsManager } = require("@aws-sdk/client-secrets-manager");
const sm = new SecretsManager();
const res = await sm.getSecretValue({ SecretId: "prod/db/credentials" });
const creds = JSON.parse(res.SecretString);

Caching Secrets

Calling Secrets Manager on every invocation adds latency and cost. Fetch once outside the handler so the value is reused across warm invocations.

let cached;
async function getCreds() {
  if (!cached) {
    const r = await sm.getSecretValue({ SecretId: "prod/db/credentials" });
    cached = JSON.parse(r.SecretString);
  }
  return cached;
}

Automatic Rotation

Secrets Manager can rotate credentials on a schedule using a rotation Lambda. For supported databases (RDS), AWS provides a ready-made rotation function so passwords change without code edits.

Secrets vs Parameter Store

SSM Parameter Store also stores config and SecureString values, and is cheaper. Use Parameter Store for plain config; use Secrets Manager when you need built-in rotation and cross-account sharing.

Encryption with KMS

Every secret is encrypted with a KMS key. Use the default AWS-managed key for simplicity, or a customer-managed key (CMK) for fine-grained access control and audit.

Auditing Access

Every GetSecretValue call is logged to CloudTrail. Review these logs to detect unexpected access and prove compliance.

Best Practices

Keep secrets safe:

  • Never log the secret value
  • Scope IAM to the exact secret ARN
  • Enable rotation for long-lived credentials
  • Cache across warm invocations, not in source control

Quick Check

Test your Secrets Manager knowledge.

Recap

You learned to secure credentials:

  • Store secrets in Secrets Manager, never in code
  • Grant least-privilege IAM to the exact ARN
  • Fetch with the SDK and cache across warm invocations
  • Enable automatic rotation and audit via CloudTrail

よくある質問

「AWS Secrets Managerによるシークレット保護」レッスンは無料ですか?

はい。「AWS Secrets Managerによるシークレット保護」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Serverless Backend with AWS Lambda & API Gatewayコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

「AWS Secrets Managerによるシークレット保護」で何を学びますか?

Lambdaに認証情報をハードコードするのはやめましょう。AWS Secrets Managerを使ってAPIキーやデータベースのパスワードを安全に保存、ローテーション、取得する方法を学びます。 ブラウザで直接実行するハンズオンコードでServerless Backend with AWS Lambda & API Gatewayを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Serverless Backend with AWS Lambda & API Gatewayを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのServerless Backend with AWS Lambda & API Gatewayは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「AWS Secrets Managerによるシークレット保護」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このServerless Backend with AWS Lambda & API Gatewayレッスンでコードを書いて実行できますか?

はい。すべてのServerless Backend with AWS Lambda & API Gatewayレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. IAMロールと権限
  2. API Gatewayオーソライザー
  3. VPCによるLambdaの保護
  4. AWS Secrets Managerによるシークレット保護
← Serverless Backend with AWS Lambda & API Gatewayに戻る