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?
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 anetworkInterfaces
property. Were you talking about going down to the cfn level and manually setting it like this: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