AWS Cloudfront Invalidation: aws: error: the following arguments are required: --invalidation-batch

awscli
nodejs
aws
circle.yml

#1

Hi, I’m at a lose as to why my cloudfront Invalidation is not working. I’ve tried all the suggestions found on the board but to no avail.

My setup is Angular on Node hosted on S3 with Cloudfront

When utilising Circle 1.0 I had no issues invalidating Cloudfront, but now that I’ve moved to Circle 2.0 it no longer works.

version: 2
jobs:
# The build job
build:
    working_directory: ~/example
    docker:
        - image: circleci/node:latest
    steps:
        # Checkout the code from the branch into the working_directory
        - checkout
        # Log the current branch
        - run:
            name: Show current branch
            command: echo ${CIRCLE_BRANCH}
        - run:
            name: Who Am i
            command: whoami
        - run:
            name: Path
            command: echo ${PATH}

    # Restore local dependencies from cache
    - restore_cache:
        keys: dependency-cache-{{ checksum "yarn.lock" }}
    # Install project dependencies
    - run:
        name: Install local dependencies
        command: yarn install

    # Cache local dependencies if they don't exist
    - save_cache:
        key: dependency-cache-{{ checksum "yarn.lock" }}
        paths:
            - ~/.cache/yarn
            - ./node_modules

    # Build the source code
    - run:
        name: Angular Build
        command: yarn ng -- build

    # Cache the dist folder for the deploy job
    - save_cache:
        key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
        paths:
            - dist
# The deploy job
deploy:
    working_directory: ~/example
    docker:
        - image: circleci/node:latest
    steps:
        # Log the current branch
        - run:
            name: Show current branch
            command: echo ${CIRCLE_BRANCH}
        # Restore cache from the build job which contains the
        # dist folder that needs to be deployed
        - restore_cache:
            key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
        # Install AWS cli
        - run:
            name: Install aws cli
            command:
               sudo apt-get -y -qq install awscli
        # Set the signature version for the S3 auth
        - run:
            name: Setting Signature Version 4 for S3 Request Authentication
            command: aws configure set default.s3.signature_version s3v4

# ACTIVATE CLOUD FRONT CLI
- run:
    name: activate cloud front
    command: aws configure set preview.cloudfront true
            aws configure set preview.create-invalidation true



# Deploy to the S3 bucket corresponding to the current branch
- run:
    name: Deploy to S3
    command: |
        if [ "${CIRCLE_BRANCH}" == "integration" ]; then
            echo "Start S3 Sync"
            aws s3 sync dist s3://example.com --cache-control "max-age=3153600" --exclude ".git/*" --exclude "*.sh" --exclude "node_modules/*" --region ap-southeast-2
            echo "sync done "
       
        elif [ "${CIRCLE_BRANCH}" == "master" ]; then
     #       bash s3deploy-master.sh
        fi

  # Invalidate Cloudfront
        - run:
            name: Invalidate Cloudfront
            command: aws cloudfront create-invalidation --distribution-id ABC123 --paths *

workflows:
version: 2
# The build and deploy workflow
build_and_deploy:
    jobs:
        - build
        # The deploy job will only run on the filtered branches and
        # require the build job to be successful before it starts
        - deploy:
            requires:
                - build
            filters:
                branches:
                    only:
                        - integration
                        - master

#2

If you’d like your YAML file to be readable here, render the whole document with code formatting. Readers will appreciate it.

To do this in Markdown, select the whole document in the editor, and click the </> code icon. Presently it is partly formatted because a four-space indent is the syntax for code formatting. It needs that across the whole file (for both of them). Would you make this change now?


#3

Thanks for that.


#4

I think the problem is in your YAML formatting - you’re using the multiline approach but the parser does not understand what you mean.

I think this is what you’re aiming for (notwithstanding the excessive space indentation - two or four is enough):

# ACTIVATE CLOUD FRONT CLI
- run:
    name: activate cloud front
    command: |
            aws configure set preview.cloudfront true
            aws configure set preview.create-invalidation true

#5

I’ve updated the file and also removed some of the padding, but I’m still getting in the deploy log

#!/bin/bash -eo pipefail

aws cloudfront create-invalidation --distribution-id abc123 --paths /*
usage: aws [options] [parameters]
aws: error: the following arguments are required: --invalidation-batch
Exited with code 2

The revised file is as follows:

version: 2
jobs:
    # The build job
    build:
      working_directory: ~/example
      docker:
        - image: circleci/node:latest
      steps:
        # Checkout the code from the branch into the working_directory
        - checkout
        # Log the current branch
        - run:
            name: Show current branch
            command: echo ${CIRCLE_BRANCH}
        - run:
            name: Who Am i
            command: whoami
        - run:
            name: Path
            command: echo ${PATH}

        # Restore local dependencies from cache
        - restore_cache:
            keys: dependency-cache-{{ checksum "yarn.lock" }}
        # Install project dependencies
        - run:
            name: Install local dependencies
            command: yarn install

        # Cache local dependencies if they don't exist
        - save_cache:
            key: dependency-cache-{{ checksum "yarn.lock" }}
            paths: |
                 ~/.cache/yarn
                 ./node_modules

        # Build the source code
        - run:
            name: Angular Build
            command: yarn ng -- build

        # Cache the dist folder for the deploy job
        - save_cache:
            key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
            paths:
                - dist

# The deploy job
    deploy:
      working_directory: ~/example
      docker:
        - image: circleci/node:latest
      steps:
        # Log the current branch
        - run:
            name: Show current branch
            command: echo ${CIRCLE_BRANCH}
        # Restore cache from the build job which contains the
        # dist folder that needs to be deployed
        - restore_cache:
            key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
        # Install AWS cli
        - run:
            name: Install aws cli
            command:
               sudo apt-get -y -qq install awscli
        # Set the signature version for the S3 auth
        - run:
            name: Setting Signature Version 4 for S3 Request Authentication
            command: aws configure set default.s3.signature_version s3v4

        # ACTIVATE CLOUD FRONT CLI
        - run:
            name: activate cloud front
            command: |
                    aws configure set preview.cloudfront true
                    aws configure set preview.create-invalidation true


        # Deploy to the S3 bucket corresponding to the current branch
        - run:
            name: Deploy to S3
            command: |
                if [ "${CIRCLE_BRANCH}" == "integration" ]; then
                    echo "Start S3 Sync"
                    aws s3 sync dist s3://example.com --cache-control "max-age=3153600" --exclude ".git/*" --exclude "*.sh" --exclude "node_modules/*" --region ap-southeast-2
                    echo "sync done "
                #    bash s3deploy-integration.sh
                elif [ "${CIRCLE_BRANCH}" == "master" ]; then
             #       bash s3deploy-master.sh
                fi

     # Invalidate Cloudfront
        - run:
            name: Invalidate Cloudfront
            command: aws cloudfront create-invalidation --distribution-id ABC123 --paths /\*



workflows:
  version: 2
  # The build and deploy workflow
  build_and_deploy:
      jobs:
          - build
          # The deploy job will only run on the filtered branches and
          # require the build job to be successful before it starts
          - deploy:
              requires:
                  - build
              filters:
                  branches:
                      only:
                          - integration
                          - master

#6

I don’t use this AWS feature, but it seems that you are not supplying a required parameter.


#7

The cloudfront command worked when using Circle v1.0


#8

Perhaps that was an earlier version of the command? Why not just try supplying the parameter it is requesting?


#9

I’ve tried that and performed multiple other suggestions on this forum. I’m hoping someone that has experienced this and is familiar with Cloudfront would be able to assist.


#10

OK, good stuff. I suggest you make a new post (in this thread) with your latest configuration attempt(s), so that people do not need to ask exactly what new things you’ve tried.


AWS Cloudfront Invalidation: aws: error: the following arguments are required: –invalidation-batch
#11

@kontact00 What ended up being your solution in the end?

I found this similarly related issue: AWS CLI + Cloudfront Invalidations


#12

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.