Skip to content

Commit 2d95a80

Browse files
RihojJames Ray
andauthored
aws_elasticache_replication_group (#143)
* aws_elasticache_replication_group - fixes #140 Add tests fir aws_elasticache_replication_group # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Mon Jul 19 15:34:06 2021 -0400 # # On branch aws_elasticache_replication_group # Changes to be committed: # modified: docs/rules/aws_elasticache_cluster_default_parameter_group.md # new file: docs/rules/aws_elasticache_replication_group_default_parameter_group.md # new file: rules/aws_elasticache_replication_group_default_parameter_group.go # new file: rules/aws_elasticache_replication_group_default_parameter_group_test.go # new file: rules/aws_elasticache_replication_group_invalid_type.go # new file: rules/aws_elasticache_replication_group_invalid_type_test.go # * aws_elasticache_replication_group - Fix variable, and registered to provider * aws_elasticache_replication_group - Add to readme * aws_elasticache_replication_group - Add docs for rule and register with provider * aws_elasticache_replication_group - Add missing docs * aws_elasticache_replication_group - Add previous type rule for replication group Co-authored-by: James Ray <[email protected]>
1 parent ef81b2c commit 2d95a80

14 files changed

+622
-4
lines changed

docs/rules/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ These rules warn of possible errors that can occur at `terraform apply`. Rules m
2020
|aws_elasticache_cluster_invalid_parameter_group|Disallow using invalid parameter group|||
2121
|aws_elasticache_cluster_invalid_security_group|Disallow using invalid security groups|||
2222
|aws_elasticache_cluster_invalid_subnet_group|Disallow using invalid subnet group|||
23-
|aws_elasticache_cluster_invalid_type|Disallow using invalid node type|||
23+
|[aws_elasticache_cluster_invalid_type](aws_elasticache_cluster_invalid_type)|Disallow using invalid node type|||
24+
|[aws_elasticache_replication_group_invalid_type](aws_elasticache_replication_group_invalid_type)|Disallow using invalid node type|||
2425
|aws_elb_invalid_instance|Disallow using invalid instances|||
2526
|aws_elb_invalid_security_group|Disallow using invalid security groups|||
2627
|aws_elb_invalid_subnet|Disallow using invalid subnets|||
@@ -51,6 +52,8 @@ These rules enforce best practices and naming conventions:
5152
|[aws_db_instance_default_parameter_group](aws_db_instance_default_parameter_group.md)|Disallow using default DB parameter group||
5253
|[aws_elasticache_cluster_previous_type](aws_elasticache_cluster_previous_type.md)|Disallow using previous node types||
5354
|[aws_elasticache_cluster_default_parameter_group](aws_elasticache_cluster_default_parameter_group.md)|Disallow using default parameter group||
55+
|[aws_elasticache_replication_group_previous_type](aws_elasticache_replication_group_previous_type.md)|Disallow using previous node types||
56+
|[aws_elasticache_replication_group_default_parameter_group](aws_elasticache_replication_group_default_parameter_group.md)|Disallow using default parameter group||
5457
|[aws_instance_previous_type](aws_instance_previous_type.md)|Disallow using previous generation instance types||
5558
|[aws_iam_policy_document_gov_friendly_arns](aws_iam_policy_document_gov_friendly_arns.md)|Ensure `iam_policy_document` data sources do not contain `arn:aws:` ARN's||
5659
|[aws_iam_policy_gov_friendly_arns](aws_iam_policy_gov_friendly_arns.md)|Ensure `iam_policy` resources do not contain `arn:aws:` ARN's||

docs/rules/README.md.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ These rules warn of possible errors that can occur at `terraform apply`. Rules m
2020
|aws_elasticache_cluster_invalid_parameter_group|Disallow using invalid parameter group|✔|✔|
2121
|aws_elasticache_cluster_invalid_security_group|Disallow using invalid security groups|✔|✔|
2222
|aws_elasticache_cluster_invalid_subnet_group|Disallow using invalid subnet group|✔|✔|
23-
|aws_elasticache_cluster_invalid_type|Disallow using invalid node type||✔|
23+
|[aws_elasticache_cluster_invalid_type](aws_elasticache_cluster_invalid_type)|Disallow using invalid node type||✔|
24+
|[aws_elasticache_replication_group_invalid_type](aws_elasticache_replication_group_invalid_type)|Disallow using invalid node type||✔|
2425
|aws_elb_invalid_instance|Disallow using invalid instances|✔|✔|
2526
|aws_elb_invalid_security_group|Disallow using invalid security groups|✔|✔|
2627
|aws_elb_invalid_subnet|Disallow using invalid subnets|✔|✔|
@@ -51,6 +52,8 @@ These rules enforce best practices and naming conventions:
5152
|[aws_db_instance_default_parameter_group](aws_db_instance_default_parameter_group.md)|Disallow using default DB parameter group|✔|
5253
|[aws_elasticache_cluster_previous_type](aws_elasticache_cluster_previous_type.md)|Disallow using previous node types|✔|
5354
|[aws_elasticache_cluster_default_parameter_group](aws_elasticache_cluster_default_parameter_group.md)|Disallow using default parameter group|✔|
55+
|[aws_elasticache_replication_group_previous_type](aws_elasticache_replication_group_previous_type.md)|Disallow using previous node types|✔|
56+
|[aws_elasticache_replication_group_default_parameter_group](aws_elasticache_replication_group_default_parameter_group.md)|Disallow using default parameter group|✔|
5457
|[aws_instance_previous_type](aws_instance_previous_type.md)|Disallow using previous generation instance types|✔|
5558
|[aws_iam_policy_document_gov_friendly_arns](aws_iam_policy_document_gov_friendly_arns.md)|Ensure `iam_policy_document` data sources do not contain `arn:aws:` ARN's||
5659
|[aws_iam_policy_gov_friendly_arns](aws_iam_policy_gov_friendly_arns.md)|Ensure `iam_policy` resources do not contain `arn:aws:` ARN's||

docs/rules/aws_elasticache_cluster_default_parameter_group.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ $ tflint
2626
Notice: "default.redis3.2" is default parameter group. You cannot edit it. (aws_elasticache_cluster_default_parameter_group)
2727
2828
on template.tf line 9:
29-
9: parameter_group_name = "default.redis3.2" // default paramete group!
30-
29+
9: parameter_group_name = "default.redis3.2" // default parameter group!
30+
3131
```
3232

3333
## Why
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# aws_elasticache_cluster_invalid_type
2+
3+
Disallow using invalid type.
4+
5+
## Example
6+
7+
```hcl
8+
resource "aws_elasticache_cluster" "default" {
9+
node_type = "cache.t3.mini" // invalid type!
10+
engine_version = "6.x"
11+
maintenance_window = "thu:02:30-thu:03:30"
12+
num_cache_nodes = 1
13+
apply_immediately = false
14+
parameter_group_name = "custom.redis6.x.cluster.on"
15+
port = 6379
16+
cluster_id = "cluster_id"
17+
snapshot_retention_limit = 1
18+
subnet_group_name = aws_elasticache_subnet_group.private.name
19+
security_group_ids = [aws_security_group.redis_service.id]
20+
}
21+
```
22+
23+
```
24+
$ tflint
25+
1 issue(s) found:
26+
27+
Warning: "cache.t3.mini" is an invalid node type. (aws_elasticache_cluster_invalid_type)
28+
29+
on template.tf line 5:
30+
2: node_type = "cache.t3.mini" // invalid type!
31+
32+
```
33+
34+
## Why
35+
36+
Apply will fail. (Plan will succeed with the invalid value though)
37+
38+
## How To Fix
39+
40+
Select valid type according to the [document](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html#CacheNodes.SupportedTypesByRegion)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# aws_elasticache_replication_group_default_parameter_group
2+
3+
Disallow using default parameter group.
4+
5+
## Example
6+
7+
```hcl
8+
resource "aws_elasticache_replication_group" "redis" {
9+
cluster_id = "app"
10+
engine = "redis"
11+
engine_version = "3.2.4"
12+
maintenance_window = "sun:00:00-sun:06:00"
13+
node_type = "cache.m4.large"
14+
num_cache_nodes = 1
15+
port = 6379
16+
parameter_group_name = "default.redis3.2" // default paramete group!
17+
subnet_group_name = "app-subnet-group"
18+
security_group_ids = ["${aws_security_group.redis.id}"]
19+
}
20+
```
21+
22+
```
23+
$ tflint
24+
1 issue(s) found:
25+
Notice: "default.redis3.2" is default parameter group. You cannot edit it. (aws_elasticache_replication_group_default_parameter_group)
26+
on template.tf line 9:
27+
9: parameter_group_name = "default.redis3.2" // default parameter group!
28+
```
29+
30+
## Why
31+
32+
You can modify parameter values in a custom parameter group, but you can't change the parameter values in a default parameter group.
33+
34+
## How To Fix
35+
36+
Create a new parameter group, and change the `parameter_group_name` to that.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# aws_elasticache_replication_group_invalid_type
2+
3+
Disallow using invalid type.
4+
5+
## Example
6+
7+
```hcl
8+
resource "aws_elasticache_replication_group" "default" {
9+
node_type = "cache.t3.mini" // invalid type!
10+
at_rest_encryption_enabled = true
11+
automatic_failover_enabled = true
12+
engine_version = "6.x"
13+
maintenance_window = "thu:02:30-thu:03:30"
14+
apply_immediately = false
15+
parameter_group_name = "custom.redis6.x.cluster.on"
16+
port = 6379
17+
replication_group_description = " "
18+
replication_group_id = "replication_group_id"
19+
snapshot_retention_limit = 1
20+
subnet_group_name = aws_elasticache_subnet_group.private.name
21+
security_group_ids = [aws_security_group.redis_service.id]
22+
23+
cluster_mode {
24+
replicas_per_node_group = 1
25+
num_node_groups = 2
26+
}
27+
}
28+
```
29+
30+
```
31+
$ tflint
32+
1 issue(s) found:
33+
34+
Warning: "cache.t3.mini" is an invalid node type. (aws_elasticache_replication_group_invalid_type)
35+
36+
on template.tf line 5:
37+
2: node_type = "cache.t3.mini" // invalid type!
38+
39+
```
40+
41+
## Why
42+
43+
Apply will fail. (Plan will succeed with the invalid value though)
44+
45+
## How To Fix
46+
47+
Select valid type according to the [document](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html#CacheNodes.SupportedTypesByRegion)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# aws_elasticache_replication_group_previous_type
2+
3+
Disallow using previous node types.
4+
5+
## Example
6+
7+
```hcl
8+
resource "aws_elasticache_replication_group" "default" {
9+
node_type = "cache.t1.micro" // previous node type!
10+
at_rest_encryption_enabled = true
11+
automatic_failover_enabled = true
12+
engine_version = "6.x"
13+
maintenance_window = "thu:02:30-thu:03:30"
14+
apply_immediately = false
15+
parameter_group_name = "custom.redis6.x.cluster.on"
16+
port = 6379
17+
replication_group_description = " "
18+
replication_group_id = "replication_group_id"
19+
snapshot_retention_limit = 1
20+
subnet_group_name = aws_elasticache_subnet_group.private.name
21+
security_group_ids = [aws_security_group.redis_service.id]
22+
23+
cluster_mode {
24+
replicas_per_node_group = 1
25+
num_node_groups = 2
26+
}
27+
}
28+
```
29+
30+
```
31+
$ tflint
32+
1 issue(s) found:
33+
34+
Warning: "cache.t1.micro" is previous generation node type. (aws_elasticache_replication_group_previous_type)
35+
36+
on template.tf line 6:
37+
2: node_type = "cache.t1.micro" // previous node type!
38+
39+
```
40+
41+
## Why
42+
43+
Previous node types are inferior to current generation in terms of performance and fee. Unless there is a special reason, you should avoid to use these ones.
44+
45+
## How To Fix
46+
47+
Select a current generation node type according to the [upgrade paths](https://aws.amazon.com/elasticache/previous-generation/).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package rules
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
hcl "github.com/hashicorp/hcl/v2"
8+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
9+
"github.com/terraform-linters/tflint-ruleset-aws/project"
10+
)
11+
12+
// AwsElastiCacheReplicationGroupDefaultParameterGroupRule checks whether the cluster use default parameter group
13+
type AwsElastiCacheReplicationGroupDefaultParameterGroupRule struct {
14+
resourceType string
15+
attributeName string
16+
}
17+
18+
// NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule returns new rule with default attributes
19+
func NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule() *AwsElastiCacheReplicationGroupDefaultParameterGroupRule {
20+
return &AwsElastiCacheReplicationGroupDefaultParameterGroupRule{
21+
resourceType: "aws_elasticache_replication_group",
22+
attributeName: "parameter_group_name",
23+
}
24+
}
25+
26+
// Name returns the rule name
27+
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Name() string {
28+
return "aws_elasticache_replication_group_default_parameter_group"
29+
}
30+
31+
// Enabled returns whether the rule is enabled by default
32+
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Enabled() bool {
33+
return true
34+
}
35+
36+
// Severity returns the rule severity
37+
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Severity() string {
38+
return tflint.NOTICE
39+
}
40+
41+
// Link returns the rule reference link
42+
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Link() string {
43+
return project.ReferenceLink(r.Name())
44+
}
45+
46+
var defaultElastiCacheReplicationParameterGroupRegexp = regexp.MustCompile("^default")
47+
48+
// Check checks the parameter group name starts with `default`
49+
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Check(runner tflint.Runner) error {
50+
return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
51+
var parameterGroup string
52+
err := runner.EvaluateExpr(attribute.Expr, &parameterGroup, nil)
53+
54+
return runner.EnsureNoError(err, func() error {
55+
if defaultElastiCacheParameterGroupRegexp.Match([]byte(parameterGroup)) {
56+
runner.EmitIssueOnExpr(
57+
r,
58+
fmt.Sprintf("\"%s\" is default parameter group. You cannot edit it.", parameterGroup),
59+
attribute.Expr,
60+
)
61+
}
62+
return nil
63+
})
64+
})
65+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_AwsElastiCacheReplicationGroupDefaultParameterGroup(t *testing.T) {
11+
cases := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "default.redis3.2 is default parameter group",
18+
Content: `
19+
resource "aws_elasticache_replication_group" "cache" {
20+
parameter_group_name = "default.redis3.2"
21+
}`,
22+
Expected: helper.Issues{
23+
{
24+
Rule: NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule(),
25+
Message: "\"default.redis3.2\" is default parameter group. You cannot edit it.",
26+
Range: hcl.Range{
27+
Filename: "resource.tf",
28+
Start: hcl.Pos{Line: 3, Column: 28},
29+
End: hcl.Pos{Line: 3, Column: 46},
30+
},
31+
},
32+
},
33+
},
34+
{
35+
Name: "application3.2 is not default parameter group",
36+
Content: `
37+
resource "aws_elasticache_replication_group" "cache" {
38+
parameter_group_name = "application3.2"
39+
}`,
40+
Expected: helper.Issues{},
41+
},
42+
}
43+
44+
rule := NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule()
45+
46+
for _, tc := range cases {
47+
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
48+
49+
if err := rule.Check(runner); err != nil {
50+
t.Fatalf("Unexpected error occurred: %s", err)
51+
}
52+
53+
helper.AssertIssues(t, tc.Expected, runner.Issues)
54+
}
55+
}

0 commit comments

Comments
 (0)