算機(jī)工具類-CI和CD工具Skills】aws-serverless 技能)
在AWS上構(gòu)建生產(chǎn)就緒的無(wú)服務(wù)器應(yīng)用程序的專門技能。涵蓋Lambda函數(shù)、API Gateway、DynamoDB、SQS/SNS事件驅(qū)動(dòng)模式、SAM/CDK部署和冷啟動(dòng)優(yōu)化。技能概述aws-serverless 技能提供了在AWS上構(gòu)建生產(chǎn)就緒的無(wú)服務(wù)器應(yīng)用程序的專家模式和最佳實(shí)踐。它涵蓋了Lambda函數(shù)、API Gateway、DynamoDB、SQS/SNS事件驅(qū)動(dòng)模式、SAM/CDK部署和冷啟動(dòng)優(yōu)化等關(guān)鍵領(lǐng)域,幫助開發(fā)者構(gòu)建高性能、可靠的無(wú)服務(wù)器架構(gòu)。下載地址:agentic-awesome-skills/skills/aws-serverless at main · sickn33/agentic-awesome-skills · GitHub主要功能Lambda函數(shù)模式: 提供Node.js和Python的Lambda處理程序模式,包含錯(cuò)誤處理和最佳實(shí)踐API Gateway集成: REST API和HTTP API與Lambda的集成模式DynamoDB操作: 高效的DynamoDB數(shù)據(jù)訪問(wèn)模式和最佳實(shí)踐事件驅(qū)動(dòng)模式: SQS/SNS事件驅(qū)動(dòng)架構(gòu)模式SAM/CDK部署: 使用SAM和CDK進(jìn)行基礎(chǔ)設(shè)施即代碼部署冷啟動(dòng)優(yōu)化: 減少冷啟動(dòng)時(shí)間和優(yōu)化性能的策略核心原則正確調(diào)整內(nèi)存和超時(shí)大小(在優(yōu)化之前測(cè)量)為延遲敏感的工作負(fù)載最小化冷啟動(dòng)為Java/.NET函數(shù)使用SnapStart對(duì)于簡(jiǎn)單用例,優(yōu)先使用HTTP API而不是REST API為失敗設(shè)計(jì),使用DLQ和重試保持部署包小使用環(huán)境變量進(jìn)行配置使用關(guān)聯(lián)ID實(shí)現(xiàn)結(jié)構(gòu)化日志記錄Lambda處理程序模式Node.js Lambda處理程序// Initialize outside handler (reused across invocations)const { DynamoDBClient } require(aws-sdk/client-dynamodb);const { DynamoDBDocumentClient, GetCommand } require(aws-sdk/lib-dynamodb);const client new DynamoDBClient({});const docClient DynamoDBDocumentClient.from(client);// Handler functionexports.handler async (event, context) {context.callbackWaitsForEmptyEventLoop false;try {const body typeof event.body string ? JSON.parse(event.body) : event.body;const result await processRequest(body);return {statusCode: 200,headers: {Content-Type: application/json,Access-Control-Allow-Origin: *},body: JSON.stringify(result)};} catch (error) {console.error(Error:, JSON.stringify({error: error.message,stack: error.stack,requestId: context.awsRequestId}));return {statusCode: error.statusCode || 500,headers: {Content-Type: application/json},body: JSON.stringify({error: error.message || Internal server error})};}};Python Lambda處理程序import jsonimport osimport loggingimport boto3from botocore.exceptions import ClientErrorlogger logging.getLogger()logger.setLevel(logging.INFO)dynamodb boto3.resource(dynamodb)table dynamodb.Table(os.environ[TABLE_NAME])def handler(event, context):try:body json.loads(event.get(body, {})) if isinstance(event.get(body), str) else event.get(body, {})result process_request(body)return {statusCode: 200,headers: {Content-Type: application/json,Access-Control-Allow-Origin: *},body: json.dumps(result)}except ClientError as e:logger.error(fDynamoDB error: {e.response[Error][Message]})return error_response(500, Database error)except json.JSONDecodeError:return error_response(400, Invalid JSON)except Exception as e:logger.error(fUnexpected error: {str(e)}, exc_infoTrue)return error_response(500, Internal server error)最佳實(shí)踐在處理程序外部初始化客戶端(在溫暖調(diào)用之間重用)始終返回正確的API Gateway響應(yīng)格式使用結(jié)構(gòu)化JSON進(jìn)行日志記錄,以便CloudWatch Insights在錯(cuò)誤日志中包含請(qǐng)求ID以便跟蹤API Gateway集成模式REST API和HTTP API與Lambda的集成,適用于構(gòu)建由Lambda支持的REST API,需要HTTP端點(diǎn)的函數(shù)。SAM模板示例AWSTemplateFormatVersion: 2010-09-09Transform: AWS::Serverless-2016-10-31Globals:Function:Runtime: nodejs20.xTimeout: 30MemorySize: 256Environment:Variables:TABLE_NAME: !Ref ItemsTableResources:HttpApi:Type: AWS::Serverless::HttpApiProperties:StageName: prodCorsConfiguration:AllowOrigins:- *AllowMethods:- GET- POST- DELETEAllowHeaders:- *注意事項(xiàng)僅在任務(wù)明確匹配上述范圍時(shí)使用此技能不要將輸出作為環(huán)境特定驗(yàn)證、測(cè)試或?qū)<覍彶榈奶娲啡绻鄙偎璧妮斎搿?quán)限、安全邊界或成功標(biāo)準(zhǔn),請(qǐng)停止并請(qǐng)求澄清