Serverless Backend with AWS Lambda & API Gateway · درس

حماية الأسرار باستخدام AWS Secrets Manager

توقف عن تضمين بيانات الاعتماد مباشرة في Lambda. تعلّم كيفية تخزين مفاتيح API وكلمات مرور قواعد البيانات وتدويرها واستردادها بأمان باستخدام AWS Secrets Manager.

الدرس 4 من 413 خطوة

حماية الأسرار باستخدام AWS Secrets Manager درس مجاني في Serverless Backend with AWS Lambda & API Gateway على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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
البدء مجانًا

تعلم Serverless Backend with AWS Lambda & API Gateway مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «حماية الأسرار باستخدام AWS Secrets Manager» مجاني؟

نعم — نص درس «حماية الأسرار باستخدام AWS Secrets Manager» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أدوار IAM وأذوناته
  2. مخوّلو API Gateway
  3. تأمين Lambda باستخدام VPC
  4. حماية الأسرار باستخدام AWS Secrets Manager
← العودة إلى Serverless Backend with AWS Lambda & API Gateway