https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudfront/index.html
Install and import the CloudFront client
npm i @aws-sdk/client-cloudfront
import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront"
Create a CloudFront object
const cloudfrontDistributionId = process.env.CLOUDFRONT_DISTRIBUTION_ID
const cloudfront = new CloudFrontClient({
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretAccessKey,
}
});
Create an invalidation command
const cfCommand = new CreateInvalidationCommand({
DistributionId: cloudfrontDistributionId,
InvalidationBatch: {
CallerReference: post.imageName,
Paths: {
Quantity: 1,
Items: [
"/" + post.imageName
]
}
}
})
const response = await cloudfront.send(cfCommand)