aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.91k forks source link

Attach Subnet to AutoScaleGroup #4604

Closed fogfish closed 5 years ago

fogfish commented 5 years ago

AWS AutoScaling Groups is a tool to provision EC2 instances for various workload patterns. AWS CDK allows developers to specify vpcSubnets subnets selection (meaning existing one) but do not allow to create Subnet and ASG within same stack.

Use Case

This issue is continuation of use-cases defined in #4586. There is a need to provision backend infrastructure within own subnet. There is a stack that deploys extension to existing VPC.

const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {/* ... */})

const subnet1 = new ec2.PrivateSubnet(this, 'BE1', {
  availabilityZone: vpc.availabilityZones[0],
  // ...
})

const subnet2 = new ec2.PrivateSubnet(this, 'BE2', {
  availabilityZone: vpc.availabilityZones[1],
  // ...
})

const nodes = new asg.AutoScalingGroup(this, 'Nodes', {
   vpcSubnets: {/* impossible to pass reference to subnet1 and subnet2 */}
   // ...
})

It is impossible to associate ASG to subnets. There is only one ugly work around - you have to declare the subnets in own stack and then import to ASG using subnetGroupName. However, workaround defeats purpose of layers design (see #4586).

Proposed Solution

Option 1: brute-force design Easy to design, implement, impacts only AutoScalingGroup. I do not like it.

Enhance the property type vpcSubnets: SubnetSelection | string[]. Then modify following code to either pick subnets id as-is or query data from vpc.

https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts#L488

Option 2: support explicit subnet definition with SubnetSelection

Give possibility to explicitly declare subnets ID as part of the subnet selection query

interface SubnetSelection {
   /**
   * Select the subnet group with the given ids
   */
   readonly subnetIds?: ISubnet[];
}

then support this attribute at VPC.selectSubnets(...), which acts roughly as transform function which maps ISubnet to SelectedSubnets.

I'd recommend a second approach, it allows to support subnet assignment to other classes as well. Not only too ASG.


This is a :rocket: Feature Request

rix0rrr commented 5 years ago

You seem to be asking for selection of individual subnets, is that right?

fogfish commented 5 years ago

Yes, for selection of individual subnets, which are created within same template as ASG. What do you think about option 2? I do have a strong feeling that it suites other use-cases.

hoegertn commented 5 years ago

I love option 2. I need the case where I want to launch an ASG in only one zone. This could be solved with the same approach.