简体   繁体   English

在Auto Scaling扩展事件期间从Code Deploy挂钩读取AWS EC2标记

[英]Reading AWS EC2 tag from Code Deploy hook during Auto Scaling scale-in event

I'm really enjoying working with AWS Code Deploy integrated with Auto Scaling, but am struggling with an issue around reading tags during a scale-in event. 我真的很喜欢使用与Auto Scaling集成的AWS Code Deploy,但是在扩展事件期间遇到有关读取标签的问题。

I have setup my Auto Scaling Group to tag any new instances created with a tag name 'Environment'. 我已经设置了Auto Scaling组以标记任何使用标记名称“环境”创建的新实例。 Then as part of my Code Deploy after-install script I read the tag and use it to conditionally configure my apps. 然后,作为我的Code Deploy安装后脚本的一部分,我读取了标签并使用它有条件地配置我的应用程序。 This works perfectly if I deploy a revision to existing instances in an auto scale group. 如果我将修订部署到自动伸缩组中的现有实例,则此方法非常有效。 However during a scale-in event it seems that the tags are not assigned to the new EC2 instance before Code Deploy is called from Auto Scaling, so my after-install configuration fails. 但是,在扩展事件期间,在从Auto Scaling调用Code Deploy之前,似乎未将标签分配给新的EC2实例,因此我的安装后配置失败。

Any ideas on a way around this? 有什么解决办法吗? Can I force the tags to be associated with the EC2 instance earlier in the pipeline? 是否可以在管道中更早地将标签与EC2实例相关联?

Then as part of my Code Deploy after-install script I read the tag and use it to conditionally configure my apps 然后,作为我的Code Deploy安装后脚本的一部分,我读取了标签并使用它有条件地配置我的应用

Instead of using tags you can try using the environment variables exposed by CodeDeploy during the deployment lifecycle events. 您可以尝试使用CodeDeploy在部署生命周期事件期间公开的环境变量来代替使用标签。 Please check out https://blogs.aws.amazon.com/application-management/post/Tx1PX2XMPLYPULD/Using-CodeDeploy-Environment-Variables for more information. 请查看https://blogs.aws.amazon.com/application-management/post/Tx1PX2XMPLYPULD/Using-CodeDeploy-Environment-Variables了解更多信息。

Thanks, 谢谢,
Surya. 苏里亚

You have to add custom life cycle hook in your auto-scaling group and work with them within CodeDeploy . 您必须在自动缩放组中添加自定义生命周期挂钩 ,并在CodeDeploy中使用它们。 Remember that the order of hook execution is not predictive. 请记住,挂钩执行的顺序不是可预测的。

We had the exact same issue regarding CodeDeploy and custom lifecycle hooks. 关于CodeDeploy和自定义生命周期挂钩,我们遇到了完全相同的问题。 The tags were not generated until AFTER the lifecycle completed, which was too late as we wanted to do a CodeDeploy during the time the lifecycle was pending. 直到生命周期结束后才生成标签,这太晚了,因为我们想在生命周期未决期间进行CodeDeploy。

Our solution was to build a userdata script what would allow the instance to tag itself at launch time. 我们的解决方案是构建一个userdata脚本,该脚本将允许实例在启动时对其自身进行标记。 The script is installed into each AMI, and accepts two parameters: Environment and Function. 该脚本已安装到每个AMI中,并接受两个参数:Environment和Function。

<script>
PowerShell -ExecutionPolicy Bypass -NoProfile -File c:\tools\server_userdata.ps1 --function Reg -environment production
</script>

So we can attach that userdata to a Launch Configuration, and the instance tags itself on launch. 因此,我们可以将该用户数据附加到启动配置, 实例在启动时对其本身进行标记。 As soon as the instance is stable, but not yet through its lifecycle we can do a CodeDeploy successfully. 一旦实例稳定,但尚未通过其生命周期,我们就可以成功完成CodeDeploy。

We had to use a Role policy to give the instance permission to list and create tags: 我们必须使用角色策略来授予实例列出和创建标签的权限:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeTags",
                "ec2:CreateTags",
                "ec2:DeleteTags"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

The userdata script is generic and parameter driven, so we can launch any instance with any environment and function setting. userdata脚本是通用的并且由参数驱动,因此我们可以在任何环境和功能设置下启动任何实例。 The same approach would work in Linux of course. 当然,相同的方法也可以在Linux中使用。

This approach solved the problem you are describing. 这种方法解决了您正在描述的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM