Bitbucket Pipelines Failed to fetch revision while trying to build a Blue/Green flow
Asked by Jose Pablo Sanchez:

I'm trying to configure a simple CI/CD flow using AWS and Bitbucket Pipelines, everything is working as should, but when the deployment to CodeDeploy comes in, it stops with an error message.

This is the diagram of my setup:
> Build and push Docker Image to ECR(Ok) -\> Deploy to ECS(Ok) -\> Upload to AWS CodeDeploy(Ok) -\> Deploy to AWS CodeDeploy(Fails)

As you can see, it's a pretty straightforward setup, so I think that this simplicity makes it a bit harder to understand where the problem can be.

#### Complete error message: {#toc-hId-149285074}

? Failed to fetch revision..  
An error occurred (RevisionDoesNotExistException) when calling the GetApplicationRevision operation: No application revision was found for revision.

#### Error message with DEBUG enabled: {#toc-hId--1658169389}

```
DEBUG: Response body:
b'{"__type":"RevisionDoesNotExistException","message":"No application revision found for revision."}'
DEBUG: Response headers: {'x-amzn-RequestId': '2bcb3131-aa53-4ce1-a84b-4c86059e9d12', 'Date': 'Sun, 03 Jul 2022 22:01:46 GMT', 'Content-Type': 'application/x-amz-json-1.1', 'Content-Length': '98'}
DEBUG: Response body:
b'{"__type":"RevisionDoesNotExistException","message":"No application revision found for revision."}'
DEBUG: Event needs-retry.codedeploy.GetApplicationRevision: calling handler <botocore.retryhandler.RetryHandler object at 0x7f2cfeef5040>
DEBUG: No retry needed.
```

#### The thing I've already tried: {#toc-hId-829343444}

- Review user permissions and services roles.  
- Review all my variables, mostly to verify if everything was in the same region and things like that.  
- Review S3 routes to verify that the files exist and where in the place that they should.  
- Manually replicate the setup step by step using only the AWS Console. (With success)  
- Use DEBUG inside the Upload script, It doesn't show anything helpful.

These are the Upload \& Deploy section of my pipeline.yml

#### \*\*Upload\*\* {#toc-hId--978111019}

```
upload-to-codedeploy: &upload-to-codedeploy
    name: Upload to AWS CodeDeploy
    image: php:8.0-apache
    script: # Test
    - pipe: atlassian/aws-code-deploy:1.1.1
    variables:
    # Common Variables
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID # Optional if already defined in the context.
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY # Optional if already defined in the context.
    AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION # Optional if already defined in the context.
    APPLICATION_NAME: 'dev-test-deploy'
    COMMAND: 'upload' # 'upload' or 'deploy'.
    S3_BUCKET: $S3_BUCKET
    # Upload Variables
    ZIP_FILE: 'appspec.zip'
    BUNDLE_TYPE: 'zip'
    FOLDER: 'pipeline'
    VERSION_LABEL: $S3_BUCKET
    DEBUG: 'true'
```

#### \*\*Deploy\*\* {#toc-hId-1509401814}

```
deploy-to-codedeploy: &deploy-to-codedeploy
    name: Deploy to AWS CodeDeploy
    image: php:8.0-apache
    script: # Test
    - pipe: atlassian/aws-code-deploy:1.1.1
    variables:
    # Common Variables
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID # Optional if already defined in the context.
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY # Optional if already defined in the context.
    AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION # Optional if already defined in the context.
    APPLICATION_NAME: 'dev-test-deploy'
    S3_BUCKET: $S3_BUCKET 
    BUNDLE_TYPE: 'YAML'
    WAIT: 'true'
    # Upload Variables
    ZIP_FILE: 'appspec.yaml'
    # Deploy specific variables
    DEPLOYMENT_GROUP: "dev-test-deploy-group"
    #VERSION_LABEL: 'app-$(date +%Y-%m-%d)-1'
    COMMAND: 'deploy' # 'upload' or 'deploy'.
    IGNORE_APPLICATION_STOP_FAILURES: 'true' 
    FILE_EXISTS_BEHAVIOR: 'OVERWRITE'
    FOLDER: 'pipeline'
    VERSION_LABEL: $S3_BUCKET
    DEBUG: 'true'
```

#### \*\*appspec.yaml\*\* {#toc-hId--298052649}

```
version: 0.0
Resources:
    - TargetService:
    Type: AWS::ECS::Service
Properties:
    TaskDefinition: "arn:aws:ecs:us-east-1:891903962143:task-definition/cb-app-task"
LoadBalancerInfo:
    ContainerName: "php-docker"
    ContainerPort: 80
```

Any help would be welcome, thanks.


Answers:
author: Jose Pablo Sanchez
body: Ok, so I figured out what was wrong, these are the steps that I followed:

1- Like the error says "No application revision found", Codedeploy wasn't capable of finding the .yml file that the process of Upload did, so after reviewing my congifs and making sure that everything was in the correct place(S3), so I did the registration by hand:

```
aws deploy register-application-revision \
                --application-name myapp \
                --description "Revised my app application" \
                --s3-location bucket=mybucket-s3,key=appspec.zip,bundleType=zip
```

Here are two things that are very important, the appspec.zip file and the content of it.

For the content I only used the appspec.yml file, and thats it, thats everything that the .zip need.

#### appspec.yml {#toc-hId-149290872}

```
 version: 0.0

 Resources:

   - TargetService:
        Type: AWS::ECS::Service
        Properties:
          TaskDefinition: "arn:aws:ecs:us-east-1:12345678:task-definition/my-app-task"
          LoadBalancerInfo:
            ContainerName: "docker-image-name"
            ContainerPort: 80
```

2- After reviewing my Upload and Deploy steps, here is the final code of each one  

#### Upload {#toc-hId--1658163591}

> ```
> upload-to-codedeploy: &upload-to-codedeploy
>     name: Upload to AWS CodeDeploy
>     image: php:8.0-apache
>     script: # Test
>     - pipe: atlassian/aws-code-deploy:1.1.1
>       variables:
>         AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID 
>         AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY?
>         AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
>         APPLICATION_NAME: 'dev-test-deploy'
>         COMMAND: 'upload' # 'upload' or 'deploy'.
>         S3_BUCKET: $S3_BUCKET
>         ZIP_FILE: 'appspec.yml'
>         BUNDLE_TYPE: 'YAML' # Lambda and ECS
>         VERSION_LABEL: 'appspec.yml'
> ```

#### Deploy {#toc-hId-829349242}

```
      deploy-to-codedeploy: &deploy-to-codedeploy
          name: Deploy to AWS CodeDeploy
          image: php:8.0-apache
          script: # Test
          - pipe: atlassian/aws-code-deploy:1.1.1
            variables:
              AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
              AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
              AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
              APPLICATION_NAME: 'dev-test-deploy'
              S3_BUCKET: $S3_BUCKET
              BUNDLE_TYPE: 'YAML'
              WAIT: 'true'
              ZIP_FILE: 'appspec.yml'
              DEPLOYMENT_GROUP: 'dev-test-deploy-group' 
              FILE_EXISTS_BEHAVIOR: 'OVERWRITE'
              VERSION_LABEL: 'appspec.yml'
              DEBUG: 'true'       
```

I hope I have been clear, if I can help with something else please create a comment here.  

One important note here, please, always read the docs, they are crucial.


Tags: aws

aws-codedeploy

bitbucket-cloud

bitbucket-pipelines

cloud