In CDK, how do you enable `associatePublicIpAddress` in an AutoScalingGroup that has a `mixedInstancesPolicy`?

0

I'm using AWS CDK and am trying to enable the associatePublicIpAddress property for an AutoScalingGroup that's using a launch template.

My first attempt was to just set associatePublicIpAddress: true, but I get this error (https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts#L1526-L1528)

// first attempt
new asg.AutoScalingGroup(this, 'ASG', {
  associatePublicIpAddress: true, // here
  minCapacity: 1,
  maxCapacity: 1,
  vpc,
  vpcSubnets: {
    subnetType: SubnetType.PUBLIC,
    onePerAz: true,
    availabilityZones: [availabilityZone],
  },
  mixedInstancesPolicy: {
    instancesDistribution: {
      spotMaxPrice: '1.00',
      onDemandPercentageAboveBaseCapacity: 0,
    },
    launchTemplate: new LaunchTemplate(this, 'LaunchTemplate', {
      securityGroup: this._securityGroup,
      role,
      instanceType
      machineImage,
      userData: UserData.forLinux(),
    }),
    launchTemplateOverrides: [
      {
        instanceType: InstanceType.of(
          InstanceClass.T4G,
          InstanceSize.NANO
        ),
      },
    ],
  },
  keyName,
})
// I hit this error from the CDK
    if (props.associatePublicIpAddress) {
      throw new Error('Setting \'associatePublicIpAddress\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set');
    }

My second attempt was to not set associatePublicIpAddress and see if it gets set automatically because the AutoScalingGroup is in a public availablity zone with an internet gateway. However, it still doesn't provision a public ip address.

Has anyone been able to create an autoscaling group with a mix instance policy and an associated public ip?

1 個回答
0
已接受的答案

You're setting associatePublicIpAddress on the ASG. You need to either set it on the launch template; or change the subnets default behavior to associatePublicIpAddress=true. Just having an IGW doesn't automatically change this setting for the subnet, and the default is false except for the default subnets in your default VPC

The code function you linked is called "verifyNoLaunchConfigPropIsGiven"; meaning those properties were for launch configurations (the older feature that has since been replaced by launch templates)

AWS
已回答 2 年前
profile picture
專家
已審閱 1 個月前
  • Thanks, Shahad! I wasn't aware of the mapPublicIpOnLaunch Subnet property. I think setting that will be ok as these will be the only instances we launch in our public subnets.

    Is it possible to set associatePublicIpAddress=true in the launch template? From looking at the docs (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.LaunchTemplate.html), there doesn't seem to be a networkInterfaces property. Were you talking about going down to the cfn level and manually setting it like this:

          const cfnLaunchTemplate = launchTemplate.node.findChild('Resource') as CfnLaunchTemplate
    
          (cfnLaunchTemplate.launchTemplateData as CfnLaunchTemplate.LaunchTemplateDataProperty).networkInterfaces = {
            subnetId: '',
            associatePublicIpAddress: true
          } as CfnLaunchTemplate.NetworkInterfaceProperty
    
  • Whoops, should have included those details, sorry about that. But yes, exactly correct. For the Launch Template method you would need to directly set the CFN property. However, it sounds like for your usecase the subnet option is much simpler

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南