【问题标题】:Assigning a launch configuration to an Auto-Scaling Group using CDK使用 CDK 将启动配置分配给 Auto-Scaling 组
【发布时间】:2021-11-10 07:31:54
【问题描述】:

注意:这里的代码是 Go,但很高兴看到任何 CDK 语言的答案。

在 AWS CDK 中,您可以创建启动配置:

// Create the launch configuration
lc := awsautoscaling.NewCfnLaunchConfiguration(
    stack,
    jsii.String("asg-lc"),
    &awsautoscaling.CfnLaunchConfigurationProps{
        ...
    },
)

但是 Auto-Scaling Group 的 props 中没有明显的参数或函数可以附加它。

我已设置更新策略:

UpdatePolicy: awsautoscaling.UpdatePolicy_RollingUpdate,

我想要做的是能够在 AMI 配置发生更改时在 CI 系统中调用自动刷新:

aws autoscaling start-instance-refresh --cli-input-json file://asg-refresh.json

问题在于,启动配置似乎是在首次创建堆栈时自动创建的,并且在更新时不会更改并且值不正确(AMI ID 已过时)。

有没有办法使用 CDK 定义/刷新启动配置以更新 AMI ID?这是 UI 中的一个简单更改。

【问题讨论】:

    标签: amazon-cloudformation aws-cdk autoscaling amazon-ami launch-configuration


    【解决方案1】:

    如果您使用 L2 AutoScalingGroup 构造,您可以在更新 AMI 后运行 cdk deploy,它应该会为您启动一个新的。同样使用此构造,为您创建启动配置。你真的不需要担心它。

                IMachineImage image = MachineImage.Lookup(new LookupMachineImageProps()
                {
                    Name = "MY-AMI", // this can be updated on subsequent deploys
                });
    
                
                AutoScalingGroup asg = new AutoScalingGroup(this, $"MY-ASG", new AutoScalingGroupProps()
                {
                    AllowAllOutbound = false,
                    AssociatePublicIpAddress = false,
                    AutoScalingGroupName = $"MY-ASG",
                    Vpc = network.Vpc,
                    VpcSubnets = new SubnetSelection() { Subnets = network.Vpc.PrivateSubnets },
                    MinCapacity = 1,
                    MaxCapacity = 2,
                    MachineImage = image, 
                    InstanceType = new InstanceType("m5.xlarge"),
                    SecurityGroup = sg,
                    UpdatePolicy = UpdatePolicy.RollingUpdate(new RollingUpdateOptions()
                    {
                        
                    }),
                });
    

    【讨论】:

    • 谢谢,这让我走上了正确的道路。我实际上并不需要管理启动配置。
    猜你喜欢
    • 2020-03-31
    • 2013-10-13
    • 2020-08-13
    • 2017-04-20
    • 1970-01-01
    • 2016-12-08
    • 2015-05-08
    • 2023-04-05
    • 2018-11-01
    相关资源
    最近更新 更多