Search code examples
amazon-web-servicesdeploymentaws-code-deployamazon-lightsail

Setup CodePipeline - Deploy step has a status of failed


When setting up CodeDeploy following this tutorial, under the last step of Setup CodePipeline

Deploy step has a status of failed

If I click in Details under Failed

Action execution failed

If I click in Link to execution details

The deployment failed because no instances were found for your deployment group. Check your deployment group settings to make sure the tags for your Amazon EC2 instances or Auto Scaling groups correctly identify the instances you want to deploy to, and then try again.

Link to execution details

This was most likely due to a problem in locations - sometimes it was used N. Virginia and other times London.

  • IAM - does not require region selection.
  • S3 - does not require region selection.
  • LightSail instance - London, Zone A (eu-west-2a)
  • CodeDeploy application - N. Virginia (us-east-1)
  • CodeDeploy pipeline - London, Zone A (eu-west-2a)

So, I went to CodeDeploy, deleted the application in N. Virginia and created one with its deployment group in London. Then, created a new pipeline there in London.

The deployment failed this time too but with a different reason

The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.

deployment failed because too many individual instances failed deployment

In order to fix that, I've deleted both pipelines and created a new one. Since I had to change the role, I used AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailDemoPipeline but this got me into troubles in the moment of Creating the pipeline

Could not create role AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailDemoPipeline

Could not create role

So, I changed it to AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailDemoP. Then, this got me into another problem

A policy called AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailDemoPipeline already exists. Duplicate names are not allowed.

Duplicate names are not allowed

So, I went back and changed the Pipeline name to CodeDeployLightsailDemoPi and the Role name to AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailDemoPi.

This got me into the same

The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.

I decided to wait 30 minutes and create a new pipeline giving it the name CodeDeployLightsailPipeline and the role name AWSCodePipelineServiceRole-eu-west-2-CodeDeployLightsailPipelin but the same problem happened.

This is what the information present in Deployment ID > View events

View Events

According to Marcin, I should have found the appspec.yml

version: 0.0
os: linux
files:
  - source: /index.html
    destination: /var/www/html/
hooks:
  BeforeInstall:
    - location: scripts/install_dependencies
      timeout: 300
      runas: root
    - location: scripts/start_server
      timeout: 300
      runas: root
  ApplicationStop:
    - location: scripts/stop_server
      timeout: 300
      runas: root

In it, it mentions destination: /var/www/html/ but in the instance there's no such /var/www/html path

No such path as /var/www/html

So, I've created these two folders in it

SSH and folders

and restarted the deployment

Restart deployment

but the it failed just like before.

How can I get the Deploy status to succeeded?


Edit

According to the documentation, this error code shows HEALTH_CONSTRAINTS. So, I'm now reading:


Solution

  • Based on the comments:

    In an attempt to reproduce the issue, I deployed the application CodeDeployGitHubDemo used by the OP on my own Amazon Linux 2 instances with CodeDeploy. I could verified that the CodeDeployGitHubDemo and its appspec.yml files are correct and work as expected.

    Since the OP is using CodePipline (CP), it was suggest to go one step back and focus on deployment using CodeDeploy (CD) only, without added complexity of using combination of CP + CD. This allows to focus only on CD issues and should simplify finding the root cause of the problems. Once standalone CD deployment works, the next step is to work on incorporating CP in the deployment process, knowing exactly how CD should be setup to work.


    More specifically, this is what was done

    1. In IAM console at https://console.aws.amazon.com/iam/

      1.1. Deleted one role (CodeDeployServiceRole).

      1.2. Created a service role.

    2. In S3 console at https://console.aws.amazon.com/s3/

      2.1. Emptied and deleted one bucket (tiagocodedeploylightsailbucket).

      2.2. Created a new bucket in EU London (eu-west-2).

    3. Back into the IAM console at https://console.aws.amazon.com/iam/

      3.1. Deleted one policy (CodeDeployS3BucketPolicy).

      3.2. Created a new policy.

    4. Stay in the IAM console at https://console.aws.amazon.com/iam/

      4.1. Delete one user (LightSailCodeDeployUser)

      4.2. Created a new user (Note that it needs to have a different name or else would get this problem).

    5. Navigate to the Lightsail home page at https://lightsail.aws.amazon.com/

      5.1. Deleted previous instance (codedeploy).

      5.2. Created one new instance with Amazon Linux (Amazon_Linux_1) (Note that if used Amazon Linux 2 then would reach this problem),

    using the script

    mkdir /etc/codedeploy-agent/
    
    mkdir /etc/codedeploy-agent/conf
    
    cat <<EOT >> /etc/codedeploy-agent/conf/codedeploy.onpremises.yml
    
    ---
    
    aws_access_key_id: ACCESS_KEY
    
    aws_secret_access_key: SECRET_KEY
    
    iam_user_arn: arn:aws:iam::525221857828:user/GeneralUser
    
    region: eu-west-2
    
    EOT
    
    wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install
    
    chmod +x ./install
    
    sudo ./install auto
    
    1. SSH in both instances

      6.1. Verify if codedeploy-agent is running (sudo service codedeploy-agent status)

    2. In PowerShell with AWS CLI on your local machine

      7.1.

    Run

    aws deploy register-on-premises-instance --instance-name Amazon_Linux_1 --iam-user-arn arn:aws:iam::525221857828:user/GeneralUser --region eu-west-2
    

    Then

    aws deploy add-tags-to-on-premises-instances --instance-names Amazon_Linux_1 --tags Key=Name,Value=CodeDeployLightsailDemo --region eu-west-2
    

    Then

    aws deploy list-on-premises-instances --region eu-west-2
    

    In the end, got as output

    {
        "instanceNames": [
            "Amazon_Linux_1",
            "codedeploy"
        ]
    }
    
    1. Navigate to the CodeDeploy console at https://eu-west-2.console.aws.amazon.com/codesuite/codedeploy/start?region=eu-west-2#

      8.1. Delete the application (CodeDeployLightsailDemo).

      8.2. Create a new application with a new deployment group (CodeDeployLightsailDemoDeploymentGroup).

    2. Navigate to the CodePipeline console at https://eu-west-2.console.aws.amazon.com/codesuite/codepipeline/start?region=eu-west-2

      9.1. Delete the pipeline (CodeDeployLightsailDemoPipeline).

      9.2. Create a new pipeline using "New service role".

    It worked