David Vaynshteyn
David Vaynshteyn

Reputation: 11

AWS Cloudfront createInvalidation

I am attempting to perform CloudFront invalidation using the following code:

var cloudfront = new AWS.CloudFront({s3BucketEndpoint: <String Bucketname>});
        var params = {
            DistributionId: <String ID>,
            InvalidationBatch: {
                CallerReference: 'Cloudfront Invalidation',
                Paths: {
                    Quantity: 1,
                    Items: [
                        '/*'
                    ]
                }
            }
        };
        cloudfront.createInvalidation(params, function(err, data){
            if (err) console.log(err, err.stack); // an error occurred
            else     console.log(data);           // successful response
        });

However, I get no response in my createInvalidation function from err or data. The documentation for the AWS SDK states you should get a positive/negative response, but nothing is returned and no invalidation is performed.

Upvotes: 1

Views: 1208

Answers (2)

MonitorAllen
MonitorAllen

Reputation: 1

The parameter "callerreference" must be unique, which allows awss3 to avoid you submitting the same request and can be used as a key timestamp

Upvotes: 0

Brian LeBlanc
Brian LeBlanc

Reputation: 31

It might be your CallerReference, in the JavaScript aws-sdk docs (docs.aws.amazon.com) it says that it needs to uniquely identify the invalidation request.

I stumbled upon this question trying to do the same thing as you're doing and I was able to get the createInvalidation working with the following code:

var cloudfront = new AWS.CloudFront();

var params = {
  DistributionId: <String ID>,
  InvalidationBatch: {
    CallerReference: Date.now().toString(),
    Paths: {
      Quantity: 1,
      Items: [
        '/*'
      ]
    }
  }
};

cloudfront.createInvalidation(params, function(err, data) {
  if (err) console.log(err, err.stack);
  else     console.log('Data: ' + JSON.stringify(data));
});

The use of the Date.now().toString() gives a timestamp string that will be unique to when this code is run.

With the above code, I got an output of (after pretty formatting the json):

Data: {
  "Location": "https://cloudfront.amazonaws.com/2017-03-25/distribution/<String ID>/invalidation/<String ID>",
  "Invalidation": {
    "Id": "<String ID>",
    "Status": "InProgress",
    "CreateTime": "<Timestamp>",
    "InvalidationBatch": {
      "Paths": {
        "Quantity": 1,
        "Items": [
          "/*"
        ]
      },
      "CallerReference": "<Timestamp>"
    }
  }
}

Upvotes: 3

Related Questions