Search code examples
amazon-web-servicesaws-cliaws-code-deployaws-codepipelineblue-green-deployment

Specify AWS CodeDeploy Target instances in AWS CodePipeline for Blue/Green deployment


I'm trying to create a CodePipeline to deploy an application to EC2 instances using Blue/Green Deployment.

My Deployment Group looks like this:

aws deploy update-deployment-group \
        --application-name MySampleAppDeploy \
        --deployment-config-name CodeDeployDefault.AllAtOnce \
        --service-role-arn arn:aws:iam::1111111111:role/CodeDeployRole \
        --ec2-tag-filters Key=Stage,Type=KEY_AND_VALUE,Value=Blue \
        --deployment-style deploymentType=BLUE_GREEN,deploymentOption=WITH_TRAFFIC_CONTROL \
        --load-balancer-info targetGroupInfoList=[{name="sample-app-alb-targets"}] \
        --blue-green-deployment-configuration file://configs/blue-green-deploy-config.json \
        --current-deployment-group-name MySampleAppDeployGroup

blue-green-deploy-config.json

{
    "terminateBlueInstancesOnDeploymentSuccess": {
        "action": "KEEP_ALIVE",
        "terminationWaitTimeInMinutes": 1
    },
    "deploymentReadyOption": {
        "actionOnTimeout": "STOP_DEPLOYMENT",
        "waitTimeInMinutes": 1
    },
    "greenFleetProvisioningOption": {
        "action": "DISCOVER_EXISTING"
    }
}

I'm able to create a blue/green deployment manually using this command, it Works! :

aws deploy create-deployment \
    --application-name MySampleAppDeploy \
    --deployment-config-name CodeDeployDefault.AllAtOnce \
    --deployment-group-name MySampleAppDeployGroup \

    # I can specify the Target Instances here
    --target-instances file://configs/blue-green-target-instances.json \
    --s3-location XXX

blue-green-target-instances.json

{
    "tagFilters": [
      {
        "Key": "Stage",
        "Value": "Green",
        "Type": "KEY_AND_VALUE"
      }
    ]
}

Now, In my CodePipeline Deploy Stage, I have this:

{
     "name": "Deploy",
     "actions": [
         {
             "inputArtifacts": [
             {
                 "name": "app"
             }
             ],
             "name": "Deploy",
             "actionTypeId": {
                "category": "Deploy",
                "owner": "AWS",
                "version": "1",
                "provider": "CodeDeploy"
             },
             "outputArtifacts": [],
             "configuration": {
                "ApplicationName": "MySampleAppDeploy",
                "DeploymentGroupName": "MySampleAppDeployGroup"

/* How can I specify Target Instances here? */

             },
             "runOrder": 1
         }
     ]
 }

All EC2 instances are tagged correctly and everything works as expected when using CodeDeploy via the command line, I'm missing something about how AWS CodePipeline works in this case.

Thanks


Solution

  • You didn't mention which error you get when you invoke the pipeline? Are you getting this error:

    "The deployment failed because no instances were found in your green fleet"

    Taking this assumption, since you are using manual tagging in your CodeDeploy configuration, this is not going to work to deploy using Blue/Green with manual tags as CodeDeploy expects to see a tagSet to find the "Green" instances and there is no way to provide this information via CodePipeline.

    To workaround this, please use the 'Copy AutoScaling' option for implementing Blue/Green deployments in CodeDeploy using CodePipeline. See Step 10 here [1]

    Another workaround is that you can create lambda function that is invoked as an action in your CodePipeline. This lambda function can be used to trigger the CodeDeploy deployment where you specify the target-instances with the value of the green AutoScalingGroup. You will then need to make describe calls at frequent intervals to the CodeDeploy API to get the status of the deployment. Once the deployment has completed, your lambda function will need to signal back to the CodePipeline based on the status of the deployment.

    Here is an example which walks through how to invoke an AWS lambda function in a pipeline in CodePipeline [2].

    Ref:

    [1] https://docs.aws.amazon.com/codedeploy/latest/userguide/applications-create-blue-green.html

    [2] https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html