# CodeDeploy

## Icon

![CodeDeploy Icon](https://icon.icepanel.io/AWS/svg/Developer-Tools/CodeDeploy.svg)

## About

* A deployment service that automates application deployment.
* This service can automatically initiate rollback in case of failed deployments or trigger CloudWatch Alarm.
* `appsec.yml` file defines how deployments happens.
* Provides gradual control on deployments.

### CodeDeploy Agent

* The agent is responsible to deploy artifacts onto servers like EC2 instances.
* As a prerequisite this agent should be installed in EC2 instance.
* **The EC2 instance must have sufficient permissions to access `S3` to get deployment bundles**.

### Deployment Group

* [Deployment Group](https://docs.aws.amazon.com/codedeploy/latest/userguide/deployment-groups.html) allows to group deployments settings in one place.
* It consists of multiple deployments.

### Rollbacks and Redeploys

* Rollback is a redeploy a previously deployed revision of your application.
* This can be done **automatically** i.e., when a failure occurs based on some CloudWatch Alarm or **Manually**.
* Disable rollbacks - Do not perform rollbacks for this deployment.
* When rollback is performed, `CodeDeploy` performs a new deployment with last known good revision i.e., its not a restored version.

### appSec.yml

* [This YAML file](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure.html) consists of all the deployment instruction needed to deploy the application using `CodeDeploy`.
* This file should be present in the root of project.
* [Life cycle hooks](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html) based on type of deployment can be configured in this file.
* This life cycle hooks provides a way to verify deployment after each deployment phase.

## Integrations

### EC2/On-premises platform

* `CodeDeploy` allows to deploy to EC2 instances & on-premises servers.
* It allows to perform in-place deployments _(for EC2 instances or ASG deployments)_ or blue-green deployments _(for ASG deployments, must be using an ELB)_.
* This requires CodeDeploy Agent to run on target instances.
*   Deployment speed could be defined.

    | Deployment Strategy | Description                                             |
    | ------------------- | ------------------------------------------------------- |
    | `AllAtOnce`         | Will have most downtime                                 |
    | `HalfAtATime`       | Reduces capacity by `50%` and does in-place deployments |
    | `OneAtATime`        | Slowest, lowest availability impact                     |
    | Custom              | Define your own rate of deployment                      |

### Lambda Platform

* `CodeDeploy` helps to automate traffic shifts for Lambda aliases.
* Feature is integrated with [SAM](04_codedeploy) framework.
*   This traffic shift can be done using different strategies.

    | Strategy Type | Strategy Names                                                                                             | Description                                                                             |
    | ------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
    | `Linear`      | <p><code>LambdaLinear10PercentEvery3Minutes</code><br><code>LambdaLinear10PercentEvery10Minutes</code></p> | Grow traffic every N minutes untill 100%                                                |
    | `Canary`      | <p><code>LambdaCanary10Percent5Minutes</code><br><code>LambdaCanary10Percent30Minutes</code></p>           | Try `X` percent for N minutes then 100%                                                 |
    | `AllAtOnce`   | -                                                                                                          | No time to test the updated version, directly switch all traffic to _specified_ version |

### ECS Platform

* This platform helps to automate the deployment of new ECS task definition.
* Supports only Blue/Green deployments.
* Application load balancer is required to do the traffic shift.
* The traffic shift strategy is same as strategies mentioned for Lambda Platform.
