Serverless Backend with AWS Lambda & API Gateway · Lekcja

Ochrona sekretów za pomocą AWS Secrets Manager

Przestań umieszczać dane uwierzytelniające bezpośrednio w kodzie Lambda. Naucz się bezpiecznie przechowywać, rotować i pobierać klucze API oraz hasła do baz danych za pomocą AWS Secrets Manager.

Lekcja 4 z 413 kroki

Ochrona sekretów za pomocą AWS Secrets Manager to bezpłatna lekcja Serverless Backend with AWS Lambda & API Gateway na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless Backend with AWS Lambda & API Gateway, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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
Bezpłatny start

Ucz się Serverless Backend with AWS Lambda & API Gateway dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Ochrona sekretów za pomocą AWS Secrets Manager” jest bezpłatna?

Tak — pełny tekst „Ochrona sekretów za pomocą AWS Secrets Manager” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless Backend with AWS Lambda & API Gateway, przejdź na CoddyKit PRO. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.

Co nauczysz się w „Ochrona sekretów za pomocą AWS Secrets Manager”?

Przestań umieszczać dane uwierzytelniające bezpośrednio w kodzie Lambda. Naucz się bezpiecznie przechowywać, rotować i pobierać klucze API oraz hasła do baz danych za pomocą AWS Secrets Manager. Ćwiczysz Serverless Backend with AWS Lambda & API Gateway z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Serverless Backend with AWS Lambda & API Gateway?

Nie wymagamy żadnego doświadczenia. Serverless Backend with AWS Lambda & API Gateway w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Ochrona sekretów za pomocą AWS Secrets Manager”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Serverless Backend with AWS Lambda & API Gateway?

Tak. Każda lekcja Serverless Backend with AWS Lambda & API Gateway zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Role i uprawnienia IAM
  2. Autoryzatory API Gateway
  3. Zabezpieczanie Lambda za pomocą VPC
  4. Ochrona sekretów za pomocą AWS Secrets Manager
← Powrót do Serverless Backend with AWS Lambda & API Gateway