AWS CodeDeploy Hook After Finishing Deployment to All Instances? - amazon-web-services

I have AWS CodeDeploy deploying to a Deployment Group that targets an AutoScalingGroup of EC2 instances that can have between min and max number of instances.
CodeDeploy hooks can be specified on individual instances to launch scripts on those instances at various stages of the deployment process.
Is there a way to launch a script, Lambda function, etc... after CodeDeploy successfully finishes deploying to the final instance in the ASG? In other words, is there an "All Done With Everything" hook that I can use? How are others tackling and solving this problem?

If you're using codepipeline, how about adding another stage after code deploy?
Or you can also trigger SNS topic with AWS CodeDeploy about deployment status as well.
Here: https://docs.aws.amazon.com/codedeploy/latest/userguide/monitoring-cloudwatch-events.html

Related

CodeDeploy does not work with ASG warm pool

I recently tried to set up a warm pool for an ASG to reduce the launch time of scaling out. But it turns out the instance launched from the warm pool got stuck at the 'pending:wait' status and did not finish the deployment which should be done by CodeDeploy. At the same time, if an instance is launched from the ASG directly, it works well.
After contacting AWS support, I added a aws autoscaling complete-lifecycle-action command in the last line of the userdata to manually complete the CodeDeploy managed lifecycle hook. Also, I creted a Lambda Function triggered by a EventBridge rule that send aws autoscaling complete-lifecycle-action request to forcely make instances go into "inservice" status after launched from the warm pool. However, this method only makes the instance be in the "inservice" status but the deploymemt actually still not finished. The CodeDeploy activity history shows that the deployment is stuck and no Event is processed. When I remoted into the instance to have a look, I found the codedeploy agent is not even installed.
AWS support admits that the warm pool does not offically support CodeDeploy. However, he also confirms that these two services should work well with some extra works. Does any have a thought about this? Thanks in advance.

How to deploy an application to EC2 instances(with Autoscaling) using Jenkins?

I have a Jenkins job and custom-built pipeline already functioning to deploy my Java application using Jboss to an EC2 server in AWS(by using the static IP of the instance). Now I plan to enable the Autoscaling feature for my EC2 instance.
What would be the best practices to make continuous deployments to these scaled EC2 instances maintaining the application's stability?
Here is the workflow I follow using Jenkins.
Create a Jenkins Pipeline that does a build and pushes to Amazon S3 - In an artifact bucket
Create a CodeDeploy application that deploys to the autoscaling group (not tag)
Create a CodePipeline application that polls for the S3 object, and when receved triggers CodeDeploy.
The great thing about CodeDeploy when attached to autoscaling groups is it creates a lifecycle hook. Whenever a new instance is deployed to the autoscaling group it will automatically deploy the latest version of code.

AWS Beanstalk Restarts Instance

I have created a pipeline using AWS Codepipeline, Github, Jenkins and AWS Elastic Beanstalk (Docker) running a nodejs application. Everytime a build is triggered in AWS Codepipeline and deployment done on the Elastic Beanstalk instance, it's corresponding EC2 instance is terminated and another one created afresh and we only want the app to be deployed without termination of EC2 instance. What could be the cause for termination on every build/deployed?
how many instances do you have in your beanstalk and what deployment method are you using: All at Once, Rolling, Rolling with an Additional Batch or Immutable?
With these responses, we can continue the research.
I switched to Immutable deployment and stopped experiencing the issue as explained here: Difference between rolling, rolling with additional batch and immutable deployments in AWS?
Turns out that Rolling deployments can cause the timeouts especially that I had a single instance needed

AWS CodeDeploy deployment tracking

I would like to know if it's possible to track the deployment status of CodeDeploy by using CLI.
Currently, I'm using Bamboo to trigger the CodeDeploy deployment by CLI using: aws deploy create-deployment ... My Bamboo plan will show green the moment the deployment is triggered instead of checking if the actual deployment is succeeded. Is there a way to let Bamboo/command line verify if the actual deployment was successfully deployed?
Many thanks!
Your create-deployment will return a Deployment ID. Use that in aws deploy get-deployment --deployment-id XXX to see the status and info of the deployment:
http://docs.aws.amazon.com/cli/latest/reference/deploy/get-deployment.html
You can use aws deploy wait deployment-successful --deployment-id XXX to wait for completion:
http://docs.aws.amazon.com/cli/latest/reference/deploy/wait/deployment-successful.html.
Suggest you use the AWS Code Deployment Task to manage these deployments. It will manage the entire process, and will report on actual deployment status.
With the AWS CodeDeploy task for Bamboo you can deploy applications to
EC2 instances automatically, reliably, and rapidly. Additionally, AWS
CodeDeploy keeps track of the whole deployment process.
See https://confluence.atlassian.com/bamboo/using-the-aws-codedeploy-task-750396059.html

Multiple Aws CodeDeploy applications in a newly added instance

I think i've done something wrong while designing my aws infrastructure.
Actually i have one autoscaling group with one ec2 instance.
On this instance there are 6 laravel projects that are associated to 6 applications in aws CodeDeploy, so when i want to update the version i simply update using codedeploy.
Issues comes when the autoscaling group adds instances to the group, all my codedeploy applications are deployed to the newly created instance and it fails with this message:
One or more lifecycle events did not run and the deployment was unsuccessful. Possible causes include:
(1) Multiple deployments are attempting to run at the same time on an instance;
So... what's the best way to get this to work ?
AWS recommends associating a single deployment group to an ASG and consolidate deployments to a single deployment for proper scale out. Each deployment group associates a lifecycle hook with ASG through which ASG will notify deployment-group when scale-out events occur. Parallel deployments (in your case 6) will be prone to codedeploy timeouts (5 -60 min) and codedeploy agent running on ec2 can take one command at time.
If each of your app takes less time (<60 mins), you may want to consolidate them to a single application and deploy via codedeploy hooks. Else would suggest to use different asg for app.
Refer: https://aws.amazon.com/blogs/devops/under-the-hood-aws-codedeploy-and-auto-scaling-integration/
list lifecycle hooks:
aws autoscaling describe-lifecycle-hooks --auto-scaling-group-name <asg_name> --region <region>
If launch of new ec2 goes infinite loop of terminate and launch,you can remove lifecycle hooks
aws autoscaling delete-lifecycle-hook --lifecycle-hook-name <lifecycleName> --auto-scaling-group-name <asg_name> --region <region>