Search code examples
amazon-web-servicesaws-cloudformationserverless-framework

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


After successfull sls remove all sls commands fail with

Stack with id X does not exist

Checked that stack, additional stacks and S3 deployment bucket were deleted. Tried removing .serverless/, but it didn't help.


Solution

  • The problem is that the ${cf:...} syntax requires the output of an existing CloudFormation stack and when you have not yet deployed the project, the stack and its outputs do not exist yet.

    If you need to access that output from inside the "current" stack, you should look at how the output is defined by Serverless (this example is from one of my projects):

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

    You can use the same syntax to "generate" that value in your own stack in places where you need it, replacing the dynamic parts with Serverless variables like ${self:provider.region} and ${self:provider.stage}, or whatever your project has chosen to use instead of them. For example, to add it to the Lambda environment:

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