AWS CloudFormation: How to get subnet list from VPC?
Asked Answered
W

2

10

In CloudFormation, I'm creating a VPC, two EC2 instances, and an Elasticache in front of them. In the template, I'm trying to add the elasticache to the vpc. The problem's happening in creating the AWS::Elasticache::SubnetGroup

    "CacheSubnetGroup" : {
      "Type" : "AWS::ElastiCache::SubnetGroup",
      "Properties" : {
        "Description" : "Subnets available for the ElastiCache Cluster",
        "SubnetIds" : [ ... ]
      }
    },

I do not want to ask the user to input the subnet list as suggested here because I'm assuming the user doesn't know what a subnet is. Is there any function similar to { "Fn::GetAtt" : ["myVpc", "SubnetList"] }?

edit After jarmod's response, I'm creating the subnets, vpc, and everything else. But one problem still remains. I can launch the EC2's in the created VPC, but the instances get created and in the middle on initializing the instance shuts down and new instances are spun up. This cycle goes on until I delete the cf stack. Here's the part where I think the problem is originating:

"WebServerGroup" : {
  "Type" : "AWS::AutoScaling::AutoScalingGroup",
  "Properties" : {
    "VPCZoneIdentifier" : [{ "Ref" : "InstanceSubnet1" }, { "Ref" : "InstanceSubnet2" }, { "Ref" : "InstanceSubnet3" }, { "Ref" : "InstanceSubnet4" }],
    "LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
    "MinSize" : "1",
    "MaxSize" : "4",
    ...
  }
}
Wellspoken answered 30/5, 2015 at 3:31 Comment(1)
If you check the stack events, what error do the instance creation print?Warnerwarning
K
2

If your template created the VPC then presumably your template also created the subnets for that VPC. Can't you just populate SubnetIds from the individual subnet IDs for each subnet you created?

Something like this:

"SubnetIds" : [ {"Ref":"mysubnet1"}, {"Ref":"mysubnet2"} ]
Kalinda answered 30/5, 2015 at 11:55 Comment(2)
Okay this makes sense. I'm still a noob and thought default subnets were created when a VPC gets created. So I've created my subnets and everything's working fine except for my AutoScalingGroup. My EC2's get created in the VPC, but they start up and in the middle initializing they shut down and all new EC2 instances get created. This goes on until I delete the CF stack. I'm not sure why this is happening and any help would be awesome. I've updated the post with the part where I think the problem is originating.Wellspoken
Auto Scaling Groups are designed to replace unhealthy instances with new, healthy instances. I would assume that your instances appear to be unhealthy, whether they are or not, hence they fail the health check and are terminated and replaced by the Auto Scaling Group. You'll need to understand if instance status checks are failing or if it's ELB health checks that are failing (assuming you use an ELB). For more, see docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/….Kalinda
S
11

There are three typical ways of handling this situation (in my preferred order):

  1. If the subnets will only be used by this stack, create them as part of the stack and use ref.

  2. If the subnets will be used by several stacks, create them in a separate stack, define them as outputs, do a describe-stack on the defining stack to get the values, and then pass them on to this stack as parameters.

  3. If the subnets are created elsewhere (outside of CloudFormation), just pass them in as parameters.

If you really want to use all subnets from a VPC, which I wouldn't recommend in case new ones are created in the future for other purposes, then you can always do describe-subnets and filter on VpcId do get your list.

Slosberg answered 30/5, 2015 at 13:16 Comment(2)
Thanks for the reply. Yeah, noob mistake, I thought default subnets were created when I create a VPC but that's not the case. But a new problem came up and I've updated my post explaining it.Wellspoken
@Slosberg How to describe-subnets and filter on VpcId in cloudformation parameters section?Rubierubiginous
K
2

If your template created the VPC then presumably your template also created the subnets for that VPC. Can't you just populate SubnetIds from the individual subnet IDs for each subnet you created?

Something like this:

"SubnetIds" : [ {"Ref":"mysubnet1"}, {"Ref":"mysubnet2"} ]
Kalinda answered 30/5, 2015 at 11:55 Comment(2)
Okay this makes sense. I'm still a noob and thought default subnets were created when a VPC gets created. So I've created my subnets and everything's working fine except for my AutoScalingGroup. My EC2's get created in the VPC, but they start up and in the middle initializing they shut down and all new EC2 instances get created. This goes on until I delete the CF stack. I'm not sure why this is happening and any help would be awesome. I've updated the post with the part where I think the problem is originating.Wellspoken
Auto Scaling Groups are designed to replace unhealthy instances with new, healthy instances. I would assume that your instances appear to be unhealthy, whether they are or not, hence they fail the health check and are terminated and replaced by the Auto Scaling Group. You'll need to understand if instance status checks are failing or if it's ELB health checks that are failing (assuming you use an ELB). For more, see docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/….Kalinda

© 2022 - 2024 — McMap. All rights reserved.