serverless diff
serverless diff packages the service and compares the result with the deployed stack: which functions' code changed, and what CloudFormation would create, update, replace or remove, IAM changes included. It reads AWS and changes nothing there, so use it to review a change before serverless deploy, for example in a pull-request check. To see the resolved configuration rather than the template, use serverless print.
serverless diff
When the configuration and code match the deployed stack, the command prints:
No function code changes
No infrastructure changes against the deployed stack
Options
serverless diff accepts these options:
| Option | Alias | Value | Default | Required | Description |
|---|---|---|---|---|---|
--package | -p | directory path | Package into package.path, else .serverless | No | Existing package directory to compare. Skips packaging. |
--json | boolean | false | No | Print the result as one JSON object. See JSON output. | |
--stage | -s | string | provider.stage, else dev | No | Stage whose deployed stack to compare with. |
--region | -r | string | provider.region, else us-east-1 | No | AWS Region of that stack. |
--aws-profile | string | provider.profile | No | AWS profile whose credentials the command uses. | |
--config | -c | file path | serverless.yml | No | Service configuration file to use. |
--stack | string | No | For SAM and CloudFormation projects. serverless diff doesn't read it: it compares with provider.stackName, else <service>-<stage>. |
The command also accepts the options every command takes: --param="key=value" (repeatable), --org, --app, --verbose, --debug and --help (-h).
What serverless diff compares
serverless diff reports two independent results:
- Function code. For each function, the SHA-256 of the local zip is compared with the deployed Lambda function's
CodeSha256. Functions deployed from a containerimageand functions withpackage.disable: trueare left out. Functions packaged into the same zip are listed together when that zip changes; withpackage.individually: trueeach function is compared on its own. A service with web apps also gets a web app code result, from the code location in each web app's revision in the local and deployed templates. - CloudFormation template. The packaged template is compared with the deployed stack's processed template, section by section: the template header, IAM statement and security group changes (in tables), parameters, metadata, mappings, conditions, resources, outputs and other sections. Before comparing, both templates go through the normalization the Framework uses to decide whether a deploy is needed, so changes in artifact S3 keys and in the generated API Gateway deployment ID don't show up.
The packaged template comes from --package, else from packaging the service first. When the stack does not exist, the command prints Stack "my-service-dev" does not exist yet — treating all resources as new. and lists every resource as new.
Read the output
serverless diff prints the function code result first. [+] marks a function that the deployed stack doesn't have and [~] a function whose code changed; unchanged functions are not listed:
Function Code
[+] welcome
[~] hello
A service with web apps also gets a Web App Code section, or No web app code changes. A web app's zip is named after its content, so changed code gives the web app's revision a new code location and the deploy creates a new revision. A JavaScript web app that isn't bundled is zipped with the service directory, so editing a shared file lists every such web app that includes it; set package.patterns on each web app to keep only its own files, as Packaging describes.
The template diff follows, with resources marked [+] (create), [-] (remove) or [~] (update) and changed properties nested under each resource. An update that replaces the resource is marked replace, and the property that forces it (requires replacement). IAM statement and security group changes also appear in tables of their own. A summary line ends the output:
Resources: 1 to create, 2 to update, 1 to remove
JSON output
With --json, serverless diff prints one JSON object on standard output and nothing else there:
serverless diff --json
{
"code": {
"changed": ["hello"],
"new": ["welcome"],
"webApps": {
"changed": ["api"],
"new": []
}
},
"summary": {
"create": 4,
"update": 0,
"remove": 4
},
"resources": [
{
"logicalId": "WelcomeLambdaFunction",
"changeType": "create",
"resourceType": "AWS::Lambda::Function"
},
{
"logicalId": "GoodbyeLambdaFunction",
"changeType": "remove",
"resourceType": "AWS::Lambda::Function"
}
]
}
code.changed: functions whose deployedCodeSha256differs from the local zip.code.new: functions the deployed stack doesn't have, or that are in the deployed template but not in Lambda.code.webApps: the same two lists for web apps, present only when the service defineswebApps.summary: the number of resources CloudFormation would create, update and remove.resources: each changed resource, with its logical ID,changeType(create,updateorremove) and resource type.
Functions in neither list are unchanged, or are image or package.disable functions.
Side effects and CI use
-
Without
--package,serverless diffpackages the service first, which writes the package directory asserverless packagedoes. In AWS it only reads:cloudformation:GetTemplateon the stack andlambda:GetFunctionon each deployed function. It is safe to re-run. -
The exit code is
0whether or not there are differences; it is non-zero only when the command can't finish, for example when credentials are missing, the packaged template can't be read, orlambda:GetFunctionis denied. To fail a CI step on resource or function code changes, read the JSON output; changes to other template sections, such asOutputs, appear only in the text output. In this Bash step,pipefailkeeps a failedserverless difffrom passing whenjqreads empty input:set -o pipefail serverless diff --json | jq -e '.summary.create + .summary.update + .summary.remove == 0 and (.code.changed + .code.new | length) == 0' -
The command does not prompt. The diff is printed without colors when
NO_COLORis set, whenFORCE_COLORis0orfalse, or when an AI agent runs the command. -
When
--packagepoints at a directory without the packaged template, the command stops with this error:Packaged CloudFormation template not found at <path>. Run `serverless package` first, or pass `--package <path>` to point at an existing package directory.
Provided lifecycle events
diff:diff
Examples
Compare a stage with its deployed stack
serverless diff --stage prod --region eu-central-1
Compare a package built in an earlier CI step
serverless package --stage prod --package ./artifacts
serverless diff --stage prod --package ./artifacts
Use a custom configuration file
serverless diff --config serverless.prod.yml
Related commands
serverless packagebuilds the package thatserverless diffcompares.serverless deployapplies the changes.serverless printprints the resolved configuration.