Adding an existing security group CloudFormation EC2 template

0

Instead of having to set ingress and egress rules, how do I reference existing EC2 security groups in a CloudFormation Template?

   Resources:
      EC2Instance:
        Type: AWS::EC2::Instance
        Properties:
          InstanceType:
            Ref: InstanceType
          SecurityGroups:
          - Ref: InstanceSecurityGroup
          KeyName:
            Ref: KeyName
          ImageId:
            Fn::FindInMap:
            - AWSRegionArch2AMI
            - Ref: AWS::Region
            - Fn::FindInMap:
              - AWSInstanceType2Arch
              - Ref: InstanceType
              - Arch
      InstanceSecurityGroup:
        Type: AWS::EC2::SecurityGroup
        Properties:
          GroupDescription: Existing Groups
          SecurityGroupIds:
          - Ref: sg-12345
          - Ref: sg-12312

 
  SecurityGroupIngress:
  - IpProtocol: tcp
    FromPort: 80
    ToPort: 80
    CidrIp: 0.0.0.0/0
  SecurityGroupEgress:
  - IpProtocol: tcp
    FromPort: 80
    ToPort: 80
    CidrIp: 0.0.0.0/0
asked 5 years ago3445 views
2 Answers
1
Accepted Answer

Hi,
To refer to existing Security Groups, you can add them straight into your EC2 Resource Properties, under SecurityGroupIds.

Resources:
    EC2Instance:
        Type: AWS::EC2::Instance
        Properties:
            InstanceType:
                Ref: InstanceType
            SecurityGroupIds:
              - sg-12345
              - sg-12312
            KeyName: 
                Ref: KeyName
            ImageId: 
                Fn::FindInMap:
                - AWSRegionArch2AMI
                - Ref: AWS::Region
                - Fn::FindInMap:
                  - AWSInstanceType2Arch
                  - Ref: InstanceType
                  - Arch

Hope this helps!
-randy

answered 5 years ago
0

Yes, perfect. Thanks!

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions