-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flag previous generation instance types (#2267)
* flag previous generation instance types #2213 https://aws.amazon.com/ec2/previous-generation/ https://aws.amazon.com/rds/previous-generation/ https://aws.amazon.com/elasticache/previous-generation/ https://docs.aws.amazon.com/opensearch-service/latest/developerguide/supported-instance-types.html https://leaderboard.vantage.sh/
- Loading branch information
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/cfnlint/rules/resources/PreviousGenerationInstanceType.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
import re | ||
from cfnlint.rules import CloudFormationLintRule | ||
from cfnlint.rules import RuleMatch | ||
|
||
|
||
class PreviousGenerationInstanceType(CloudFormationLintRule): | ||
id = 'I3100' | ||
shortdesc = 'Checks for legacy instance type generations' | ||
description = 'New instance type generations increase performance and decrease cost' | ||
source_url = 'https://aws.amazon.com/ec2/previous-generation/' | ||
tags = ['resources', 'ec2', 'rds', 'elasticcache', 'elasticsearch'] | ||
|
||
def match(self, cfn): | ||
matches = [] | ||
for resource_type, property_type in [ | ||
('AWS::AutoScaling::LaunchConfiguration', 'InstanceType'), | ||
('AWS::EC2::CapacityReservation', 'InstanceType'), | ||
('AWS::EC2::Host', 'InstanceType'), | ||
('AWS::EC2::Instance', 'InstanceType'), | ||
('AWS::RDS::DBInstance', 'DBInstanceClass'), | ||
('AWS::ElastiCache::CacheCluster', 'CacheNodeType'), | ||
('AWS::ElastiCache::GlobalReplicationGroup', 'CacheNodeType'), | ||
('AWS::ElastiCache::ReplicationGroup', 'CacheNodeType'), | ||
]: | ||
for resource_name, resource in cfn.get_resources([resource_type]).items(): | ||
if isinstance(resource.get('Properties', {}).get(property_type, ''), str): | ||
if re.search(r'([cmr][1-3]|cc2|cg1|cr1|g2|hi1|hs1|i2|t1)\.', resource.get('Properties', {}).get(property_type, '')): | ||
matches.append(RuleMatch(['Resources', resource_name, property_type], 'Upgrade previous generation instance type: ' + resource.get('Properties').get(property_type))) | ||
|
||
for resource_type, top_level_property_type, property_type in [ | ||
('AWS::EC2::EC2Fleet', 'FleetLaunchTemplateOverridesRequest', 'InstanceType'), | ||
('AWS::EC2::LaunchTemplate', 'LaunchTemplateData', 'InstanceType'), | ||
('AWS::EC2::SpotFleet', 'SpotFleetLaunchSpecification', 'InstanceType'), | ||
('AWS::OpenSearchService::Domain', 'ClusterConfig', 'InstanceType'), | ||
('AWS::Elasticsearch::Domain', 'ElasticsearchClusterConfig', 'InstanceType'), | ||
]: | ||
for resource_name, resource in cfn.get_resources([resource_type]).items(): | ||
if isinstance(resource.get('Properties', {}).get(top_level_property_type, {}).get(property_type, ''), str): | ||
if re.search(r'([cmr][1-3]|cc2|cg1|cr1|g2|hi1|hs1|i2|t1)\.', resource.get('Properties', {}).get(top_level_property_type, {}).get(property_type, '')): | ||
matches.append(RuleMatch(['Resources', resource_name, top_level_property_type, property_type], 'Upgrade previous generation instance type: ' + resource.get('Properties').get(top_level_property_type).get(property_type))) | ||
return matches |
23 changes: 23 additions & 0 deletions
23
test/fixtures/templates/bad/previous_generation_instances.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Resources: | ||
Domain: | ||
Type: AWS::Elasticsearch::Domain | ||
Instance: | ||
Type: AWS::EC2::Instance | ||
Properties: | ||
InstanceType: m1.small | ||
ImageId: ami-abc | ||
DBInstance: | ||
Type: AWS::RDS::DBInstance | ||
Properties: | ||
DBInstanceClass: db.t1.micro | ||
CacheCluster: | ||
Type: AWS::ElastiCache::CacheCluster | ||
Properties: | ||
CacheNodeType: cache.r3.large | ||
Engine: memcached | ||
NumCacheNodes: 1 | ||
Domain2: | ||
Type: AWS::Elasticsearch::Domain | ||
Properties: | ||
ElasticsearchClusterConfig: | ||
InstanceType: m3.medium.elasticsearch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
test/unit/rules/resources/test_previous_generation_instance_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from cfnlint.rules.resources.PreviousGenerationInstanceType import PreviousGenerationInstanceType # pylint: disable=E0401 | ||
from .. import BaseRuleTestCase | ||
|
||
|
||
class TestPreviousGenerationInstanceType(BaseRuleTestCase): | ||
def setUp(self): | ||
super(TestPreviousGenerationInstanceType, self).setUp() | ||
self.collection.register(PreviousGenerationInstanceType()) | ||
|
||
def test_file_positive(self): | ||
self.helper_file_positive() | ||
|
||
def test_file_negative(self): | ||
self.helper_file_negative('test/fixtures/templates/bad/previous_generation_instances.yaml', 4) |