How do I assign a created SecurityGroup to an ELB from CloudFormation?

Question:

I’ve got a CloudFormation script that generates a SecurityGroup and an ELB; I’m trying to reference the SecurityGroup in the ELB creation; here’s the resources bit:

Unfortunately, this fails with:

So how can I reference ELBSecurityGroup for use as a property in the ELB creation?

Thanks!

Answer:

As my CloudFormation script is all done within a VPC, I figured out what the problem was – I was creating the Security group, but not specifying the VpcId for it.

Security groups appear to be either normal security groups, or VPC security groups; if you do { "Ref": "MySecurityGroup" } on a normal one, you get the security group name, but not the ID. If you do { "Ref": "MySecurityGroup" } on a VPC one, you get back the sg-abcdef id, which is what is required for the ELB security group parameter.

So the full answer is:

This all works perfectly (provided everything you’re doing is within your VPC) and in my configuration, will successfully limit access to whatever OfficeIP is set to.

Leave a Reply