Error AWS specific parameters and EC2 SecurityGroupIds List String

0 votes

I have a rather annoying issue which I am unable to resolve

The following cut down example works in which I am able to reference a parameter and assign the security groups to my instance via the SecurityGroupIds property:

"Parameters" : {
      "pDefaultSg" : {
        "Description" : "AWS2 VPC default security groups",
        "Type" : "List<AWS::EC2::SecurityGroup::Id>",
        "Default" : "sg-245xxxxx,sg-275xxxxx,sg-235xxxxx" 
      }
    }

    "Resources" : {
      "ec2Instance" : {
        "Type" : "AWS::EC2::Instance",
        "Properties" : {
        "SecurityGroupIds" : { "Ref" : "pDefaultSg" } 
      }
}

The issue begins when I also want to add a second value to the SecurityGroupIds property referencing a security group resource instantiated within the same template:

"Resources" : {
    "ec2Instance" : { ...
        "SecurityGroupIds" : [ { "Ref" : "pDefaultSg" }, { "Fn::GetAtt" : "sgDb", "GroupId" } ],
    ....  

    "sgDb" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : { ...

I am then unable to avoid the following error causing the Cloudformation stack to rollback:

Value of property SecurityGroupIds must be of type List of String

Oct 22, 2018 in AWS by findingbugs
• 4,780 points
7,153 views

1 answer to this question.

0 votes

The issue is that when pDefaultSg is accessed via the Ref intrinsic function it returns a list, therefore your SecurityGroupIds Property looks like

[["sg-245xxxxx","sg-275xxxxx","sg-235xxxxx"],"sg-1234DB"]

The solution is to change your SecurityGroupIds Property to Fn::Join the pDefaultSg List to a comma separated string followed by the sgDb:

"SecurityGroupIds": [ 
  {"Fn::Join": 
    [",", 
      {"Ref": "pDefaultSg"}
    ]
  }, 
  { "Fn::GetAtt" : ["sgDb", "GroupId"] } 
]
answered Oct 22, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
0 answers

Error when creating AWS Budget Actions to automatically stop EC2 and RDS

I use one EC2 instance and one ...READ MORE

Apr 21, 2022 in AWS by Rahul
• 9,680 points
844 views
0 votes
1 answer

Termination Error: Inaccurate AWS EC2 Spot Advisor Interruption Likelihood

You can get a brief by this:- https://aws.amazon.com/ec2/spot/instanc ...READ MORE

answered Oct 15, 2018 in AWS by Priyaj
• 58,020 points
632 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP