简体   繁体   中英

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.

I have setup my Auto Scaling Group to tag any new instances created with a tag name 'Environment'. Then as part of my Code Deploy after-install script I read the tag and use it to conditionally configure my apps. 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.

Any ideas on a way around this? Can I force the tags to be associated with the EC2 instance earlier in the pipeline?

Then as part of my Code Deploy after-install script I read the tag and use it to conditionally configure my apps

Instead of using tags you can try using the environment variables exposed by CodeDeploy during the deployment lifecycle events. Please check out https://blogs.aws.amazon.com/application-management/post/Tx1PX2XMPLYPULD/Using-CodeDeploy-Environment-Variables for more information.

Thanks,
Surya.

You have to add custom life cycle hook in your auto-scaling group and work with them within CodeDeploy . Remember that the order of hook execution is not predictive.

We had the exact same issue regarding CodeDeploy and custom lifecycle hooks. 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.

Our solution was to build a userdata script what would allow the instance to tag itself at launch time. The script is installed into each AMI, and accepts two parameters: Environment and 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.

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. The same approach would work in Linux of course.

This approach solved the problem you are describing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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