Search code examples
javascriptamazon-web-servicestypescriptaws-sdkamazon-cloudfront

aws - CloudFront createInvalidation() method arguments error when using promises


AWS SDK for JavaScript allows to use promises instead of callbacks, when calling the methods of AWS Service classes. Following is an example for S3. (I'm using TypeScript along with the Serverless framework for development)

const s3 = new S3({ apiVersion: '2006-03-01' });

async function putFiles () {
      await s3.putObject({
        Bucket: 'my-bucket',
        Key: `test.js`,
        Body: Buffer.from(file, 'binary') // assume that the file variable was defined above.
      }).promise();
}

The above function works perfectly fine, where we are passing the bucket parameters as the only argument to the method.

But, when I'm trying to do a similar operation by calling the createInvalidation() method on the AWS CloudFront class, it gives me an error saying that the arguments do not match.

Following is my code and the error I get.

const cloudfront = new aws.CloudFront();

async function invalidateFiles() {
      await this.cloudfront.createInvalidation({
        DistributionId: 'xxxxxxxxxxx',
        InvalidationBatch: {
          Paths: {
            Quantity: 1,
            Items: [`test.js`],
          },
        },
      }).promise();
}

Arguements Error

Can someone help with this issue please?


Solution

  • You are missing to pass CallerReference as an argument.

    const cloudfront = new aws.CloudFront();
    async function invalidateFiles() {
        await cloudfront.createInvalidation({
            DistributionId: 'xxxxxxxxxxx',
            InvalidationBatch: {
                CallerReference: `SOME-UNIQUE-STRING-${new Date().getTime()}`,
                Paths: {
                    Quantity: 1,
                    Items: ['test.js'],
                },
            },
        }).promise();
    }