5
  • I have created an dummy application under Codedeploy, then created a deployment group and added one EC2 instance under it.

  • I did not do any deployments yet.

  • I am trying to find the appropriate aws command which can get me the list of EC2 instances under a deployment group. Looking at the commands given in this list, I checked a few like list-deployment-instances. All of them have this parameter deployment-id, which is -

The unique ID of a deployment.

I guess this is generated after doing a deployment. Correct me if I am wrong. Anyways, I did not find any command yet which gives me the list of ec2 instances under a deployment group. Am I missing something?

Backgroud

  • We have multiple EC2 instances behind a load balancer, running on production and some of them may need to be deployed with a different git branch (to test some feature, before rolling out on all instances and this may need to be kept during subsequent deployments).
  • Our DevOps team does not use auto-scaling yet. So, they manually bring up or cut down the instances.
  • During the next deployment, we would need to deploy the appropriate git branch to the group of instances, depending on which git branch they are currently on. So, before that, we would like to update the deployment group using a script which will do so by checking the git revision deployed in each of the instances.

Update - Opening Bounty

We are trying to add a sanity check before initiating deployment, to see if the total number of reachable instances (boxes go down sometimes) currently under a deployment group are less than the number when we checked last time, and if so by what margin?

In the original question, I was thinking about directly finding the instances under a deployment group, but now I realize that instances under a deployment group may not be fixed and now I am thinking of finding the tags under a deployment group and then finding the instances with those tags. I could store the count against every deployment group in a file and use it to compare next time. But, I could not find any AWS CLI command to find the instances with a given tag. I guess something like this should be available because the Code deploy dashboard allows the same.

Note - I know that if some instances are not reachable, then the final result of the deployment is shown as a success and the failed instances can be found from the AWS Code deploy dashboard. But, still, I would like to know if there could be some AWS CLI command or some API available via any SDK using which I could find the number of reachable instances under a deployment group

Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

3 Answers3

2

As you note, there is no easy way to determine the instances that are running from only the deployment group name. I double checked the CLI and you need the deployment group ID to reference the instances currently deployed to.

You should not have to need to get the list of instances to deploy to. That is done by the instance criteria specified in each deployment group. Focus on tagging the instances properly so that CodeDeploy lists the instances you want automatically!

You should have each deployment group tag itself with appropriate tags EG Environment=Testing; GitBranch=ResizeQueue. This can be done from the AWS console, or from an AutoScaling Group so all launch instances have valid tags.

Then in the deployment group, the instances are known by that set of tags, and can be deployed to using that specification. You could deploy the branch update only to those instances.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
  • I did not clearly understand the alternative solution you suggested. I think I need to study more about the tags. Do you think this will work if we use Jenkins to get the code in S3. We were planning to specify the git branch on a Jenkins project (not sure if that is called a project). We will create as many Jenkins projects as there will be deployment groups in codedeploy. Each of them will take care of deploying the necessary branch to the appropriate group of ec2 instances. – Sandeepan Nath May 23 '16 at 15:27
  • Note that different git branches may be needed to be rolled out on any given set of instances. So, we will update the git branch option in Jenkins accordingly. – Sandeepan Nath May 23 '16 at 15:28
  • Yyes we Jenkins and CodeDeploy is works great. All builds in Jenkins should be registered with CodeDeploy. You need to specify these basic tags with all instances: Name, Function, Environment and Owner. Then use the Environment tag to select eligible instances when you do a deployment. What tags are you using to select instances in the CodeDeploy deployment group? – Rodrigo Murillo May 24 '16 at 00:44
  • I am a beginner and am not getting where to specify these tags - Name, Function, Environment and Owner. I don't see any such option in the instances section of the console. – Sandeepan Nath May 24 '16 at 09:40
  • Ok I found where to specify tags to instances. But, what would be the advantage of using that? Do you mean I can specify a tag while deploying and it will deploy to all the instances with that tag? But do I have the option to add/remove tags from instances via aws commands? – Sandeepan Nath May 24 '16 at 11:35
  • I tried the command ec2-modify-instance-attribute but could not use it to tag/untag an instance yet. I also tried to changes the `InstanceInitiatedShutdownBehavior` attribute of an instance as given in http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-ModifyInstanceAttribute.html. But I am getting error `Client.null: null (Service: AmazonEC2; Status Code: 400; Error Code: null; Request ID: null)`. I tried this command - `ec2-modify-instance-attribute i-02d958251c1605f91 --instance-initiated-shutdown-behavior terminate`. – Sandeepan Nath May 24 '16 at 11:37
  • "Ok I found where to specify tags to instances. But, what would be the advantage of using that? " Have you seen how CodeDepoy itself will filter and identify eligible instances with tags? Did you know ASG can assign tags at launch time? Use those features it will solve this problem. – Rodrigo Murillo May 24 '16 at 15:08
  • Thanks for these useful information. I did not know that Codedeploy can filter and identify eligible instances with tags. However, I am still not sure whether this is necessary and sufficient to solve my problem. I guess I need to study more and do some hands on. Secondly, our Dev Ops team is not interested in trying Auto Scaling groups, as of now, because they believe they do not have observed any predictable load patterns based on which they can define the scaling rules. It seems to be a big decision. – Sandeepan Nath May 25 '16 at 11:11
1

A couple of points to add to Rodrigo's answer:

  1. Why there is no API to get instances under a deployment group? We didn't consider instances as part of CodeDeploy resource (They are closer to EC2's resource). Tags and ASGs are considered part of CodeDeploy resource. Another reason is that tagged instances can change and this part is out of CodeDeploy's control: customers can delete/add instances via EC2 (same logic goes to ASG). We don't want to show resources that can change and at the same time out of our control: this will cause confusion to our customers. CodeDeploy will only record instances that were deployed to: at this moment, those instances are part of CodeDeploy resource and they become fixed (that's why we have get-deployment-instance and batch-get-deployment-instances APIs). That being said, a recommended approach is to use EC2 CLI: aws ec2 describe-instances --filters "Name=tag-key,Values=Owner" to get the instances of interest.

  2. It seems like that your service is sensitive to the number of healthy instances before deployment. The adoption of ASG and setting of minimum healthy host in CodeDeploy can be handy: no need for DevOps to perform any manual intervention.

  3. In your update section, you mentioned to add a sanity check before initial deployment. You can set a minimum healthy host count in the CodeDeploy deployment config. If at the time of deployment, it happened that the are not enough instances to deploy to, then the deployment will fast fail: no instance will be touched. If there are enough instances, the deployment will start and the minimum healthy host constraint will be maintained through out the deployment process.

whileone
  • 2,495
  • 3
  • 21
  • 30
  • Thanks. I am trying to define the minimum healthy hosts. Does it work for non-ASG case? I can see only three predefined configs currently (CodeDeployDefault.OneAtATime, CodeDeployDefault.AllAtOnce, CodeDeployDefault.HalfAtATime). It seems I can define some more deployment config using aws cli (`aws deploy create-deployment-config`). But, how will I set this for a deployment group? Will it appear in the AWS console? Still, I guess I will be able to set only one config. What if I want to set `CodeDeployDefault.AllAtOnce` along with minimum healthy instances? – Sandeepan Nath Nov 07 '16 at 07:40
  • From the doc http://docs.aws.amazon.com/codedeploy/latest/userguide/host-health.html, it seems yes, it can be setup for non-ASG case as well. The AWS console allows defining custom deployment config in the step 7 of the "Create Deployment Walkthrough" module. I am not sure if the "deployment configurations" module allows creating custom ones. I can see only the existing, default ones, and am not sure whether I do not have permission to create new. Also, I understood that the concept of `CodeDeployDefault.AllAtOnce` and a specific minimum healthy instances value could be mutually exclusive. – Sandeepan Nath Nov 07 '16 at 13:04
  • @SandeepanNath, CodeDeploy supports custom deployment config. For example, ``aws deploy create-deployment-config --deployment-config-name ThreeQuartersHealthy --minimum-healthy-hosts type=FLEET_PERCENT,value=75`` – whileone Nov 07 '16 at 19:56
0

you could use the following command bellow to search the instance for whatever tag you want.

Example getting instances by CodeDeploy GroupName:

aws ec2 describe-instances --profile CLI_PROFILE --filters Name=tag:aws:autoscaling:groupName,Values=YOURGROUPNAME | grep PublicDnsName