{"record":{"id":"6b9378fef3442cf4","repo":"heygen-com/hyperframes","slug":"lambda-sam-deploy-exited-with-code-result-stat","errorCode":null,"errorMessage":"[lambda] sam deploy exited with code ${result.status ?? \"unknown\"}\\nIf a prior attempt left a stack in ROLLBACK_COMPLETE, CloudFormation can't reuse it. Delete it before retrying:\\n  aws cloudformation delete-stack --stack-name aws-sam-cli-managed-default --region ${opts.region}\\n  aws cloudformation delete-stack --stack-name ${opts.stackName} --region ${opts.region}\\n(the first is SAM's managed artifacts stack from --resolve-s3; the second is the render stack).","messagePattern":"\\[lambda\\] sam deploy exited with code (.+?)\\\\nIf a prior attempt left a stack in ROLLBACK_COMPLETE, CloudFormation can't reuse it\\. Delete it before retrying:\\\\n  aws cloudformation delete-stack --stack-name aws-sam-cli-managed-default --region (.+?)\\\\n  aws cloudformation delete-stack --stack-name (.+?) --region (.+?)\\\\n\\(the first is SAM's managed artifacts stack from --resolve-s3; the second is the render stack\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/lambda/sam.ts","lineNumber":109,"sourceCode":"    \"--stack-name\",\n    opts.stackName,\n    \"--region\",\n    opts.region,\n    \"--resolve-s3\",\n    \"--capabilities\",\n    \"CAPABILITY_IAM\",\n    \"--no-confirm-changeset\",\n    \"--no-fail-on-empty-changeset\",\n    \"--parameter-overrides\",\n    ...paramOverrides,\n  ];\n  if (opts.awsProfile) {\n    args.push(\"--profile\", opts.awsProfile);\n  }\n  const samDir = join(opts.repoRoot, \"examples\", \"aws-lambda\");\n  const result = spawnSync(\"sam\", args, { cwd: samDir, stdio: opts.stdio ?? \"inherit\" });\n  if (result.status !== 0) {\n    throw new Error(\n      `[lambda] sam deploy exited with code ${result.status ?? \"unknown\"}\\n` +\n        `If a prior attempt left a stack in ROLLBACK_COMPLETE, CloudFormation can't reuse it. ` +\n        `Delete it before retrying:\\n` +\n        `  aws cloudformation delete-stack --stack-name aws-sam-cli-managed-default --region ${opts.region}\\n` +\n        `  aws cloudformation delete-stack --stack-name ${opts.stackName} --region ${opts.region}\\n` +\n        `(the first is SAM's managed artifacts stack from --resolve-s3; the second is the render stack).`,\n    );\n  }\n}\n\n/** Run `sam delete` non-interactively. */\nexport function samDelete(opts: {\n  repoRoot: string;\n  stackName: string;\n  region: string;\n  awsProfile?: string;\n  stdio?: \"inherit\" | \"pipe\";\n}): void {","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/heygen-com/hyperframes/blob/c2996c8626135db5253519359d8a063d3bafad8d/packages/cli/src/commands/lambda/sam.ts#L91-L127","documentation":"Thrown by samDeploy() in packages/cli/src/commands/lambda/sam.ts:108 after `sam deploy` (spawnSync) exits non-zero. HyperFrames runs SAM non-interactively with --resolve-s3, --no-confirm-changeset, --no-fail-on-empty-changeset, so a non-zero exit is a real failure. The message specifically calls out ROLLBACK_COMPLETE: a prior failed deploy leaves the CloudFormation stack in a state where it cannot be updated, only deleted and recreated.","triggerScenarios":"samDeploy where the underlying `sam deploy` exits != 0: insufficient IAM perms, a stack stuck in ROLLBACK_COMPLETE from a prior attempt, region/profile mismatch, invalid parameter overrides (ChromeSource/ReservedConcurrency/LambdaMemoryMb), missing managed-artifacts bucket, or template syntax errors.","commonSituations":"First deploy failed (e.g. quota/perm) and left the stack in ROLLBACK_COMPLETE; wrong --region or --aws-profile; CAPABILITY_IAM not granted; SAM CLI version that needs different flags; conflicting resources already in AWS.","solutions":["Read the SAM stderr/stdout above the error — the real CloudFormation failure reason is streamed there (this message only wraps the exit code).","If the stack is in ROLLBACK_COMPLETE, run the two `aws cloudformation delete-stack` commands printed in the message, wait for DELETE_COMPLETE, then retry.","Verify the AWS profile/region have permission to create IAM roles, S3 buckets, Lambda, and Step Functions.","Confirm parameter overrides are valid for the template (e.g. chromeSource is 'sparticuz' or 'chrome-headless-shell')."],"exampleFix":"# before\nhyperframes lambda deploy   # exits non-zero\n# after\naws cloudformation delete-stack --stack-name aws-sam-cli-managed-default --region us-east-1\naws cloudformation delete-stack --stack-name <stackName> --region us-east-1\n# wait for DELETE_COMPLETE, then\nhyperframes lambda deploy","handlingStrategy":"try-catch","validationCode":"import { execFileSync } from 'node:child_process';\n\nfunction stackIsRollbackComplete(name: string, region: string): boolean {\n  try {\n    const out = execFileSync('aws', [\n      'cloudformation', 'describe-stacks', '--stack-name', name,\n      '--region', region, '--query', 'Stacks[0].StackStatus', '--output', 'text',\n    ], { encoding: 'utf-8' }).trim();\n    return out === 'ROLLBACK_COMPLETE';\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  samDeploy(opts);\n} catch (error) {\n  if (/ROLLBACK_COMPLETE/.test(String(error))) {\n    // delete both stacks as the message instructs, wait for DELETE_COMPLETE, then retry once\n    await deleteStuckStacks(opts);\n    samDeploy(opts);\n  } else {\n    throw error;\n  }\n}","preventionTips":["Stream SAM stderr to logs so the wrapped exit code has the real CF reason next to it.","Before deploy, check the stack status; if ROLLBACK_COMPLETE, delete it programmatically first.","Verify IAM permissions and parameter overrides in a dry validation step (sam validate / cfn-lint) before the real deploy.","Use --no-fail-on-empty-changeset (already set) so idempotent re-deploys don't trip this."],"tags":["aws","lambda","deployment","cloudformation","sam"],"backgroundTag":null,"analyzedSha":"c2996c8626135db5253519359d8a063d3bafad8d","analyzedAt":"2026-08-12T22:18:56.877Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}