Erik Craigo
Erik Craigo

Reputation: 451

Can't create resource in Cloudformation as name is already taken

I'm trying to set up a tech stack through cloudformation. I've had to deploy it many times as it's difficult to piece together, and am currently stuck on one thing. I have the following resource defined which I constantly need to rename, as when I delete my stack then go to recreate it, I receive an error stating that the resource already exists by that name. This is especially confusing as I can't find the resource being listed anywhere in my AWS console ( I'm in the correct region ). Please see below, any advice would be appreciated.

Thanks,

Erik

  DBSecurityGroupTwentyFour:
Type: AWS::EC2::SecurityGroup
Properties:
  GroupName: DBSecurityGroupTwentyFour
  GroupDescription: Security group for NGINX container
  SecurityGroupIngress:
    - IpProtocol: tcp
      FromPort: 3306
      ToPort: 3306
      CidrIp: 0.0.0.0/0

Upvotes: 0

Views: 288

Answers (2)

Marcin
Marcin

Reputation: 238199

You can also just skip GroupName: DBSecurityGroupTwentyFour:

DBSecurityGroupTwentyFour:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: Security group for NGINX container
    SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 3306
        ToPort: 3306
        CidrIp: 0.0.0.0/0

This way CFN will auto-generate a name for the SG, and you don't have to worry about naming it.

Upvotes: 2

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6329

In these case I usually add the stack name in the field to preserve the uniqueness.

Example:

  DBSecurityGroupTwentyFour:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: !Sub ${AWS::StackName}DBSecurityGroupTwentyFour
      GroupDescription: !Sub ${AWS::StackName} - Security group for NGINX container
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 3306
          ToPort: 3306
          CidrIp: 0.0.0.0/0

Upvotes: 2

Related Questions