What would I need to do to invalidate a specific c...
# aws
f
What would I need to do to invalidate a specific cloudfront distribution with Typescript? Or maybe as an alternative, triger the invalidation job automatically after syncing new files to an S3 bucket.
h
The
aws
CLI has a specific command for this, so I just use that. My pulumi job outputs the distribution created/updated and then I invalidate afterwards:
Copy code
aws cloudfront create-invalidation --distribution-id $(pulumi -s xxx stack output distribution) --paths /
(where
distribution
is the ID of the distribution, exported from the stack
xxx
).
f
Thank you for the answer but I'm wondering if there is a Pulumi way of doing this. Of course I know and used the aws command in the past. In fact I'm also using it to manually invalidate my distributions after Pulumi did the rest.
m
It does seem like something Pulumi might handle more directly. But I think the most “Pulumi way” of doing this (at least as of today) would still involve using the AWS SDK, though, since it’s separate from the declaration of the infrastructure. So if you wanted to do something like that from within a Pulumi program, you could do something like:
Copy code
process.on("beforeExit", async (code) => {

    // Only proceed for updates that are also successful.
    if (pulumi.runtime.isDryRun() || code !== 0) {
        return;
    }

    // Use .apply() to get at the generated CloudFront distrubution ID.
    cdn.id.apply(async (id) => {
        const cloudfront = new aws.sdk.CloudFront();
        const result = await cloudfront.createInvalidation({
            DistributionId: id,
            InvalidationBatch: {
                CallerReference: "someref",
                Paths: {
                    Quantity: 1,
                    Items: [
                        "/someitem.html",
                    ],
                },
            },
        })
        .promise();
        
        console.log(result.Invalidation);

        // And then don't forget to actually exit.
        process.exit(0);
    });
});
👍 1
💯 1
f
Excellent - will try this