Proteggere i secret con AWS Secrets Manager
Smetta di codificare le credenziali direttamente nel codice Lambda. Scopra come archiviare, ruotare e recuperare in sicurezza chiavi API e password di database usando AWS Secrets Manager.
Proteggere i secret con AWS Secrets Manager è una lezione Serverless Backend with AWS Lambda & API Gateway gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Serverless Backend with AWS Lambda & API Gateway, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Impara Serverless Backend with AWS Lambda & API Gateway con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Proteggere i secret con AWS Secrets Manager» è gratuita?
Sì — il testo completo di «Proteggere i secret con AWS Secrets Manager» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Serverless Backend with AWS Lambda & API Gateway, passa a CoddyKit PRO. Il corso Serverless Backend with AWS Lambda & API Gateway include 4 lezioni in totale.
Cosa imparerò in «Proteggere i secret con AWS Secrets Manager»?
Smetta di codificare le credenziali direttamente nel codice Lambda. Scopra come archiviare, ruotare e recuperare in sicurezza chiavi API e password di database usando AWS Secrets Manager. Eserciti Serverless Backend with AWS Lambda & API Gateway con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Serverless Backend with AWS Lambda & API Gateway?
Non è richiesta alcuna esperienza precedente. Serverless Backend with AWS Lambda & API Gateway su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Proteggere i secret con AWS Secrets Manager»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Serverless Backend with AWS Lambda & API Gateway?
Sì. Ogni lezione Serverless Backend with AWS Lambda & API Gateway include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Ruoli e autorizzazioni IAM
- Authorizer di API Gateway
- Protezione di Lambda con VPC
- Proteggere i secret con AWS Secrets Manager