Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acm certificate lifecycle create before destroy rule #202

Merged
merged 2 commits into from
Dec 2, 2021

Conversation

AleksaC
Copy link
Member

@AleksaC AleksaC commented Nov 24, 2021

Official docs for aws_acm_certificate recommend setting create_before_destroy = true in lifecycle block of this resource since without doing this terraform will be unable to delete a certificate that's in use when trying to replace it and will timeout. I had this problem a few days ago and thought it would be a useful rule to lint.

Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice rule!

// NewAwsAcmCertificateLifecycleRule returns new rule with default attributes
func NewAwsAcmCertificateLifecycleRule() *AwsAcmCertificateLifecycleRule {
return &AwsAcmCertificateLifecycleRule{
// TODO: Write resource type and attribute name here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all TODO comments.


// Severity returns the rule severity
func (r *AwsAcmCertificateLifecycleRule) Severity() string {
return tflint.ERROR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule is about best practices, so I think warning is appropriate for severity.

func (r *AwsAcmCertificateLifecycleRule) Check(runner tflint.Runner) error {
return runner.WalkResources("aws_acm_certificate", func(resource *configs.Resource) error {
if !resource.Managed.CreateBeforeDestroy {
if err := runner.EmitIssue(r, "lifecycle {\n create_before_destroy = true\n} needs to be set for `aws_acm_certificate`", resource.DeclRange); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is good to understand, but personally, I would like to avoid messages containing newlines as much as possible. This is because some formatters (e.g. -f compact) that are supposed to be parsed by regexp may be broken.

return project.ReferenceLink(r.Name())
}

// Check checks ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please edit the comment about this method.

End: hcl.Pos{Line: 2, Column: 38},
},
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to have a test case with create_before_destroy = true.

@@ -0,0 +1,49 @@
# aws_acm_certificate_lifecycle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AleksaC
Copy link
Member Author

AleksaC commented Nov 30, 2021

Thanks for the feedback, I'll fix all the stuff you mentioned shortly

@AleksaC AleksaC force-pushed the acm-certificate-lifecycle branch 2 times, most recently from 035a48e to 1885874 Compare November 30, 2021 17:55
@AleksaC
Copy link
Member Author

AleksaC commented Nov 30, 2021

Everything should be fixed now. I also spotted two broken links in the docs so I fixed them as well. Let me know if you wish me to move them to a separate pull request.

@@ -48,6 +48,7 @@ These rules enforce best practices and naming conventions:

|Rule|Description|Enabled by default|
| --- | --- | --- |
|[aws_acm_certificate_lifecycle](aws_acm_certificate_lifecycle.md)|Disallow adding `aws_acm_certificate` resource without setting `create_before_destroy = true` in `lifecycle` block |✔|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Error: lifecycle {
create_before_destroy = true
} needs to be set for `aws_acm_certificate` (aws_acm_certificate_lifecycle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you could change this message as well.

@AleksaC AleksaC force-pushed the acm-certificate-lifecycle branch from 1885874 to 7c94597 Compare December 1, 2021 18:08
@AleksaC
Copy link
Member Author

AleksaC commented Dec 1, 2021

It's fixed now. I had trouble running go generate since aws sdk submodule wasn't included and my editor was showing it as an empty directory so I didn't realize it was a submodule until I looked at it on github.

Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants