تحسين التكلفة في البنى عديمة الخوادم
تعلّم أساليب عملية للتحكم في تكلفة التطبيقات عديمة الخوادم على AWS Lambda وخفضها، بدءًا من ضبط الذاكرة ووصولًا إلى خيارات البنية.
تحسين التكلفة في البنى عديمة الخوادم درس مجاني في Serverless AWS Lambda Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Serverless AWS Lambda Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Cost Matters in Serverless
Serverless removes idle-server costs, but it does not remove the need for cost awareness. You pay per request and per GB-second of compute.
Small inefficiencies multiply across millions of invocations, so understanding the pricing model is the first step to controlling spend.
The Lambda Pricing Model
AWS Lambda charges on two axes:
- Requests: a flat price per million invocations
- Duration: billed in GB-seconds (allocated memory multiplied by execution time)
Cutting either duration or allocated memory directly lowers the bill.
Right-Sizing Memory
Memory and CPU scale together in Lambda. Counterintuitively, more memory can be cheaper because the function finishes faster.
Always benchmark several memory settings rather than defaulting to the minimum.
memory | avg duration | GB-seconds
128 MB | 900 ms | 0.1125
512 MB | 220 ms | 0.1100
1024 MB | 120 ms | 0.1200AWS Lambda Power Tuning
The open-source Lambda Power Tuning tool runs your function at many memory levels and charts cost vs speed, helping you pick the optimal setting automatically.
arn: arn:aws:states:us-east-1:123:stateMachine:powerTuning
input: { "powerValues": [128, 256, 512, 1024], "strategy": "cost" }Reducing Cold Starts Cheaply
Cold starts add latency, but Provisioned Concurrency adds cost. Cheaper alternatives:
- Trim deployment package size
- Use lighter runtimes
- Initialize SDK clients outside the handler
Initialize Outside the Handler
Code outside the handler runs once per container and is reused across invocations. Moving heavy setup there avoids paying for it on every call.
const db = new Database();
exports.handler = async (event) => {
return db.query(event.id);
};Choosing the Right Trigger
Event source choice affects cost. Batching with SQS or Kinesis processes many records per invocation, slashing the number of billable requests compared to one-message-per-call patterns.
Graviton2 (arm64) Functions
Switching a Lambda to the arm64 architecture (AWS Graviton2) can cut duration cost by up to 20% with comparable or better performance — often a one-line config change.
Resources:
MyFn:
Type: AWS::Serverless::Function
Properties:
Architectures:
- arm64Watch the Hidden Costs
Compute is rarely the whole bill. Watch for:
- CloudWatch Logs ingestion and storage
- Data transfer out of AWS
- API Gateway request charges
- NAT Gateway fees for VPC-bound functions
Set Log Retention
By default CloudWatch keeps logs forever, quietly accruing storage cost. Set a finite retention policy on every log group.
aws logs put-retention-policy \
--log-group-name /aws/lambda/myFn \
--retention-in-days 14Monitoring Spend with Cost Tools
Tag functions by team or feature and use AWS Cost Explorer and Budgets with alerts. Visibility is what turns one-off tuning into ongoing savings.
Quick Check
Test your cost-optimization knowledge.
Recap
You learned to optimize serverless cost:
- Understand the request + GB-second pricing model
- Right-size memory and use Power Tuning
- Initialize clients outside the handler and batch triggers
- Adopt arm64 and set log retention
- Track spend with tags, Cost Explorer, and Budgets
Cost optimization is a continuous architectural discipline.
الأسئلة الشائعة
هل درس «تحسين التكلفة في البنى عديمة الخوادم» مجاني؟
نعم — نص درس «تحسين التكلفة في البنى عديمة الخوادم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless AWS Lambda Development، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
ماذا ستتعلم في «تحسين التكلفة في البنى عديمة الخوادم»؟
تعلّم أساليب عملية للتحكم في تكلفة التطبيقات عديمة الخوادم على AWS Lambda وخفضها، بدءًا من ضبط الذاكرة ووصولًا إلى خيارات البنية. تتمرن على Serverless AWS Lambda Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Serverless AWS Lambda Development؟
لا تُشترط خبرة سابقة. Serverless AWS Lambda Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تحسين التكلفة في البنى عديمة الخوادم»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Serverless AWS Lambda Development هذا؟
نعم. كل درس في Serverless AWS Lambda Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- عمليات نشر Canary وBlue/Green
- بناء أنظمة serverless مرنة
- الأنماط المعمارية serverless
- تحسين التكلفة في البنى عديمة الخوادم