Serverless部署报错:模板Resources块存在未解析的资源依赖
This error almost always happens because your API Gateway models aren't properly nested under the provider.apiGateway.models section in your serverless.yml. Serverless needs this explicit configuration to generate the corresponding CloudFormation resources (like ErrorResponseModel) that your function's documentation references.
Step-by-Step Fixes:
Move models to the correct configuration block
Update yourserverless.ymlto wrap all model definitions underprovider.apiGateway.models. Here's how that should look:provider: name: aws runtime: nodejs18.x # Replace with your actual runtime apiGateway: models: - name: "Player" description: "Golf Player" contentType: "application/json" schema: type: object properties: FirstName: type: string description: "Player First Name" LastName: type: string description: "Player Last Name" - name: "Booking" contentType: "application/json" schema: type: object properties: _id: type: string description: "Booking ObjectId" slotId: type: string description: "timeslot ID" startTime: type: string description: "Booking Start Time" endTime: type: string description: "Booking End Time" accountId: type: string description: "Booker Account ID" Players: type: array items: $ref: "{{model: Player}}" - name: "ErrorResponse" contentType: "application/json" schema: type: object properties: message: type: string statusCode: type: numberVerify model name consistency
Double-check that the model names you reference inmethodResponses.responseModels(e.g.,ErrorResponse,Booking) match exactly with thenamevalues in your model definitions (case-sensitive). Your current configuration looks correct here, but typos are easy to miss!Update Serverless Framework (if needed)
Older versions of the Serverless Framework had bugs with API Gateway model handling. Update to the latest stable version to rule this out:npm install -g serverlessClean and redeploy
Sometimes residual CloudFormation stack artifacts cause issues. Clean up the existing stack and redeploy fresh:serverless remove serverless deploy
Why This Works:
When you define models outside of provider.apiGateway.models, Serverless doesn't generate the necessary AWS::ApiGateway::Model resources in the CloudFormation template. But your function's documentation still references these models, leaving CloudFormation unable to find the required resources—hence the "unresolved dependency" error. By moving the models to the correct section, Serverless will automatically create the matching CloudFormation resources, resolving the dependency.
内容的提问来源于stack exchange,提问作者ROKIKOKI

