0Pricing
Serverless Backend with AWS Lambda & API Gateway · บทเรียน

การปกป้องข้อมูลลับด้วย AWS Secrets Manager

หยุดฝังค่าเข้าสู่ระบบไว้ในโค้ดของ Lambda เรียนรู้วิธีจัดเก็บ หมุนเวียน และดึงคีย์ API กับรหัสผ่านฐานข้อมูลอย่างปลอดภัยด้วย AWS Secrets Manager

การปกป้องข้อมูลลับด้วย AWS Secrets Manager เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การปกป้องข้อมูลลับด้วย AWS Secrets Manager”

หยุดฝังค่าเข้าสู่ระบบไว้ในโค้ดของ Lambda เรียนรู้วิธีจัดเก็บ หมุนเวียน และดึงคีย์ API กับรหัสผ่านฐานข้อมูลอย่างปลอดภัยด้วย AWS Secrets Manager คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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. การรักษาความปลอดภัย Lambda ด้วย VPC
  4. การปกป้องข้อมูลลับด้วย AWS Secrets Manager
← กลับไปที่ Serverless Backend with AWS Lambda & API Gateway