diff --git a/moto/cloudformation/parsing.py b/moto/cloudformation/parsing.py index 6b655983..c4059a06 100644 --- a/moto/cloudformation/parsing.py +++ b/moto/cloudformation/parsing.py @@ -252,6 +252,10 @@ def generate_resource_name(resource_type, stack_name, logical_id): 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()) diff --git a/tests/test_elbv2/test_elbv2.py b/tests/test_elbv2/test_elbv2.py index c5c42538..b58345fd 100644 --- a/tests/test_elbv2/test_elbv2.py +++ b/tests/test_elbv2/test_elbv2.py @@ -1584,5 +1584,5 @@ def test_create_target_groups_through_cloudformation(): # and one named MyTargetGroup assert len([tg for tg in target_group_dicts if tg['TargetGroupName'] == 'MyTargetGroup']) == 1 assert len( - [tg for tg in target_group_dicts if tg['TargetGroupName'].startswith('test-stack-test')] + [tg for tg in target_group_dicts if tg['TargetGroupName'].startswith('test-stack')] ) == 2