Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get the list of ec2 instances under a deployment group using aws command

  • 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

like image 215
Sandeepan Nath Avatar asked Oct 19 '22 09:10

Sandeepan Nath


2 Answers

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.

like image 107
Rodrigo Murillo Avatar answered Nov 04 '22 21:11

Rodrigo Murillo


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.

like image 24
whileone Avatar answered Nov 04 '22 21:11

whileone