Merge pull request #1563 from william-richard/target-group-cloudformation-name
Correctly generate resource name for target groups when using cloudformation
This commit is contained in:
commit
15bc8f5f95
3 changed files with 137 additions and 13 deletions
|
|
@ -96,6 +96,7 @@ NAME_TYPE_MAP = {
|
|||
"AWS::ElasticBeanstalk::Application": "ApplicationName",
|
||||
"AWS::ElasticBeanstalk::Environment": "EnvironmentName",
|
||||
"AWS::ElasticLoadBalancing::LoadBalancer": "LoadBalancerName",
|
||||
"AWS::ElasticLoadBalancingV2::TargetGroup": "Name",
|
||||
"AWS::RDS::DBInstance": "DBInstanceIdentifier",
|
||||
"AWS::S3::Bucket": "BucketName",
|
||||
"AWS::SNS::Topic": "TopicName",
|
||||
|
|
@ -244,6 +245,22 @@ def resource_name_property_from_type(resource_type):
|
|||
return NAME_TYPE_MAP.get(resource_type)
|
||||
|
||||
|
||||
def generate_resource_name(resource_type, stack_name, logical_id):
|
||||
if resource_type == "AWS::ElasticLoadBalancingV2::TargetGroup":
|
||||
# Target group names need to be less than 32 characters, so when cloudformation creates a name for you
|
||||
# it makes sure to stay under that limit
|
||||
name_prefix = '{0}-{1}'.format(stack_name, logical_id)
|
||||
my_random_suffix = random_suffix()
|
||||
truncated_name_prefix = name_prefix[0:32 - (len(my_random_suffix) + 1)]
|
||||
# if the truncated name ends in a dash, we'll end up with a double dash in the final name, which is
|
||||
# not allowed
|
||||
if truncated_name_prefix.endswith('-'):
|
||||
truncated_name_prefix = truncated_name_prefix[:-1]
|
||||
return '{0}-{1}'.format(truncated_name_prefix, my_random_suffix)
|
||||
else:
|
||||
return '{0}-{1}-{2}'.format(stack_name, logical_id, random_suffix())
|
||||
|
||||
|
||||
def parse_resource(logical_id, resource_json, resources_map):
|
||||
resource_type = resource_json['Type']
|
||||
resource_class = resource_class_from_type(resource_type)
|
||||
|
|
@ -258,15 +275,12 @@ def parse_resource(logical_id, resource_json, resources_map):
|
|||
if 'Properties' not in resource_json:
|
||||
resource_json['Properties'] = dict()
|
||||
if resource_name_property not in resource_json['Properties']:
|
||||
resource_json['Properties'][resource_name_property] = '{0}-{1}-{2}'.format(
|
||||
resources_map.get('AWS::StackName'),
|
||||
logical_id,
|
||||
random_suffix())
|
||||
resource_json['Properties'][resource_name_property] = generate_resource_name(
|
||||
resource_type, resources_map.get('AWS::StackName'), logical_id)
|
||||
resource_name = resource_json['Properties'][resource_name_property]
|
||||
else:
|
||||
resource_name = '{0}-{1}-{2}'.format(resources_map.get('AWS::StackName'),
|
||||
logical_id,
|
||||
random_suffix())
|
||||
resource_name = generate_resource_name(resource_type, resources_map.get('AWS::StackName'), logical_id)
|
||||
|
||||
return resource_class, resource_json, resource_name
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -124,10 +124,7 @@ class FakeTargetGroup(BaseModel):
|
|||
|
||||
elbv2_backend = elbv2_backends[region_name]
|
||||
|
||||
# per cloudformation docs:
|
||||
# The target group name should be shorter than 22 characters because
|
||||
# AWS CloudFormation uses the target group name to create the name of the load balancer.
|
||||
name = properties.get('Name', resource_name[:22])
|
||||
name = properties.get('Name')
|
||||
vpc_id = properties.get("VpcId")
|
||||
protocol = properties.get('Protocol')
|
||||
port = properties.get("Port")
|
||||
|
|
@ -437,7 +434,7 @@ class ELBv2Backend(BaseBackend):
|
|||
def create_target_group(self, name, **kwargs):
|
||||
if len(name) > 32:
|
||||
raise InvalidTargetGroupNameError(
|
||||
"Target group name '%s' cannot be longer than '22' characters" % name
|
||||
"Target group name '%s' cannot be longer than '32' characters" % name
|
||||
)
|
||||
if not re.match('^[a-zA-Z0-9\-]+$', name):
|
||||
raise InvalidTargetGroupNameError(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue