- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.3k
Description
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.
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.
- 👋 I may be able to implement this feature request
-  ⚠️ This feature might incur a breaking change
This is a 🚀 Feature Request