Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudFormation use existing security group

I want to use existing security group on cloudformation template. Now I have template that create 2 SG,

 "InstanceMember1": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "SubnetId": {
          "Ref": "privateSubnetA"
        },
        "SecurityGroupIds": [
          {
            "Ref": "MongoSg"
          },
          {
            "Ref": "mongoTrafficSG"
          } 
        ],
    }
}

"MongoSg": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "MongoDB security group",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": "22",
            "ToPort": "22",
            "SourceSecurityGroupId": {
              "Ref": "bastionSG"
            }
          }
        ],
        "VpcId": "%%vpc-id%%",
      }
}
}

Now I want to add to the instance exist security group id, any advice?

like image 672
cfircoo Avatar asked Jan 13 '16 11:01

cfircoo


People also ask

How do you generate a CloudFormation from an existing resource?

Create a stack from existing resources using the AWS Management Console. Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose Create stack, and then choose With existing resources (import resources).

Can CloudFormation update existing resource?

AWS CloudFormation updates the resource without disrupting operation of that resource and without changing the resource's physical ID. For example, if you update certain properties on an AWS::CloudTrail::Trail resource, AWS CloudFormation updates the trail without disruption.

Can I use same security group in different VPC?

A security group can be used only in the VPC for which it is created. For information about the permissions required to create security groups and manage security group rules, see Manage security groups and Manage security group rules.


1 Answers

you can just go ahead and specify the security group name: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-securitygroups

 "InstanceMember1": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "SubnetId": {
          "Ref": "privateSubnetA"
        },
        "SecurityGroups": [ "mysuperawesomealreadyexistinggroup"],
    }
}
like image 184
Mircea Avatar answered Sep 21 '22 00:09

Mircea