"Stack with id X does not exist" 成功删除 sls 后所有 sls 命令

"Stack with id X does not exist" on all sls commands after successful sls remove

成功后 sls remove 所有 sls 命令都失败并显示

Stack with id X does not exist

已检查堆栈、附加堆栈和 S3 部署存储桶已删除。尝试删除 .serverless/,但没有帮助。

事实证明,我的 serverless.yml 中的违规位是 ${cf:${self:service}-${self:provider.stage}.ServiceEndpoint}。显然 ${cf:...} 如果主堆栈不存在,serverless.yml 中的内容(或至少是那种特殊情况)就会失败,即你还没有部署。

我无法确定这是 sls 错误还是我应该更清楚。

问题是 ${cf:...} 语法需要现有 CloudFormation 堆栈的输出,而当您尚未部署项目时,堆栈及其输出尚不存在。

如果您需要从 "current" 堆栈内部访问该输出,您应该查看无服务器如何定义输出(此示例来自我的一个项目):

"ServiceEndpoint":{
  "Description": "URL of the service endpoint",
  "Value": {"Fn::Join":["", [
    "https://",
    {"Ref":"ApiGatewayRestApi"},
    ".execute-api.eu-central-1.",
    {"Ref":"AWS::URLSuffix"},"/dev"]]}
}

您可以在需要的地方使用相同的语法 "generate" 您自己的堆栈中的值,用 ${self:provider.region}${self:provider.stage} 等无服务器变量替换动态部分,或者您的项目选择使用的任何东西来代替它们。例如,将其添加到 Lambda 环境中:

provider:
  environment:
    SERVICE_ENDPOINT: {"Fn::Join":["", [
      "https://",
      {"Ref":"ApiGatewayRestApi"},
      ".execute-api.${self:provider.region}.",
      {"Ref":"AWS::URLSuffix"},
      "/${self:provider.stage}"]]}

在我的例子中,我手动删除了 cloudformation 堆栈并 运行 sls deploy -s <stage> --force 它起作用了。