lbolanos
lbolanos

Reputation: 21

"Please specify a stack name using --stack or persist it in samconfig.toml" when deploying with Serverless Framework

I’m using the Serverless Framework (sls) to deploy an AWS Lambda function. When I run:

sls deploy --stage dev

I get this error:

✖ Error: Please specify a stack name using the --stack option, or persist it in the samconfig.toml file.

Context:

Debug Logs:
The error traces to getCfnConfig in the Serverless Framework, with the MISSING_STACK_NAME code.

Question:
How do I resolve this stack name error, and why did it suddenly appear despite having a service defined?

What I’ve tried:

Upvotes: 0

Views: 28

Answers (1)

lbolanos
lbolanos

Reputation: 21


Answer

This error occurs when the Serverless Framework (or an integrated tool like AWS SAM) cannot resolve a CloudFormation stack name. Here’s how to fix it:


1. Ensure service and stage Are Properly Configured

Add stage under the provider in serverless.yml. The stack name is auto-generated as ${service}-${stage}:

service: py-events
provider:
  name: aws
  stage: dev  # 👈 Required for stack name resolution
  region: us-east-1  # Optional but recommended

2. Explicitly Define the Stack Name (Optional)

Override the default stack name in serverless.yml:

provider:
  name: aws
  stackName: py-events-${sls:stage}  # e.g., "py-events-dev"

3. Remove Conflicting SAM Files (If Not Using SAM)

If you’re not using AWS SAM:

  • Delete template.yaml (SAM’s config file).
  • Remove samconfig.toml (if present).

The Serverless Framework will then use its own stack naming logic.


4. If Using AWS SAM

If you are using SAM (e.g., for other resources):

  1. Create/update samconfig.toml in your project root:
    version = 0.1
    [default.deploy.parameters]
    stack_name = "py-events-dev"
    region = "us-east-1"
    
  2. Deploy with SAM instead of sls:
    sam deploy --config-file samconfig.toml
    

Why Did This Happen Suddenly?

  • SAM Integration Conflict: If a template.yaml or samconfig.toml file was added recently, the Serverless Framework may default to SAM’s deployment logic, which requires explicit stack names.
  • AWS SDK/CLI Update: A recent update might have changed how SAM and Serverless Framework interact.

Final Fix (Serverless Framework Only)

  1. Ensure serverless.yml has service, stage, and region:
    service: py-events
    provider:
      name: aws
      stage: dev
      region: us-east-1
    
  2. Remove SAM-related files (template.yaml, samconfig.toml).
  3. Deploy:
    sls deploy --stage dev
    

This resolves the stack name ambiguity by forcing the Serverless Framework to use its internal naming convention.


Verify the Fix

After applying the solution, check the AWS CloudFormation console for a stack.


References:


Upvotes: 0

Related Questions