feat: add --serverless-rules to sam validate --lint
Which issue(s) does this change fix?
This PR adds a new feature and is not associated with a specific issue number.
Why is this change necessary?
The AWS SAM CLI's validate --lint command is useful for validating CloudFormation templates, but currently lacks the ability to apply rules specific to Serverless applications. Serverless applications have different characteristics compared to general CloudFormation templates, requiring additional validation rules tailored to their specific needs.
How does it address the issue?
This PR adds a --serverless-rules option to the sam validate --lint command, allowing users to leverage rules from the cfn-lint-serverless package. This enables users to perform additional validations specific to Serverless applications. When enabled, this option validates Serverless application best practices such as Lambda function memory size, timeout, log retention, API Gateway stage logging, and throttling settings.
What side effects does this change have?
- This change does not affect existing functionality. The
--serverless-rulesoption is only activated when explicitly specified. - If the cfn-lint-serverless package is not installed, users receive a clear error message prompting them to install the package.
- When running in debug mode, additional information about Serverless Rules application and related settings is displayed.
Mandatory Checklist
PRs will only be reviewed after checklist is complete
- [x] Add input/output type hints to new functions/methods
- [ ] Write design document if needed (Do I need to write a design document?)
- [x] Write/update unit tests
- [x] Write/update integration tests
- [x] Write/update functional tests if needed
- [x]
make prpasses - [ ]
make update-reproducible-reqsif dependencies were changed - [x] Write documentation
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Two comments.
-
One thing to check here is if this works with SAM CLI installed without
pip(like installed using the actual installer). I feel like that won't work (because SAM CLI uses their own Python environment, so it won't findcfn-lint-serverlessinstalled). If that's the case (I haven't been able to test it yet to confirm), then this would be a functionality only for a particular set up and not for all SAM CLI customers. We can still launch it like that, but we need to be aware of that, for documentation purposes mainly. -
I feel like it would be a better approach to just pass "extra rules" to
cfn-lint), and users will have to call it like:
sam validate --lint --extra-lint-rules "cfn_lint_serverless.rules"
(I don't know if that's the best name for the parameter, but that's the idea)
We would then keep this part of the code
linter_config["append_rules"] = extra_cfn_lint_config
but we won't do any special checks specifically for cfn-lint-serverless, and it would be more of a general purpose config. (Honestly, I don't know how many other extra "rules" exist for cfn-lint, but I imagine there are others that could be added to the validation).
@valerena
Issue: Enhance --extra-lint-rules Feature in SAM CLI (Addressing Review Feedback)
Description
This PR enhances the --extra-lint-rules feature in SAM CLI, addressing all feedback from valerena.
Addressed Feedback
- Added
multiple=Trueflag to--extra-lint-rulesoption to allow specifying multiple rule modules - Completely removed all code and tests related to the never-officially-released
--serverless-rulesoption - Replaced debug
printstatements with properloggingmodule usage - Changed all comments to English for consistency
- Thoroughly updated test cases to reflect the new implementation
Test Code Improvements
- Removed obsolete
test_serverless_rules_deprecated_with_extra_lint_rulestest - Updated all test cases to handle
extra_lint_rulesas a tuple (supportingmultiple=True) - Removed all references to
serverless_rulesfrom test parameters - Fixed assertions to match the new parameter structure
- Changed Korean test comments to English
Implementation Details
- Users can now use the
--extra-lint-rulesoption multiple times or specify multiple modules separated by commas - Code is cleaner and more maintainable
- Test code now correctly validates the new interface
Related PR
#7950
@valerena Will this PR be merged?