https://pulumi.com logo
#aws
Title
# aws
f

future-yak-43516

01/22/2020, 8:57 PM
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

handsome-truck-95168

01/22/2020, 10:22 PM
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

future-yak-43516

01/23/2020, 5:17 AM
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

miniature-musician-31262

01/23/2020, 9:02 PM
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

future-yak-43516

01/25/2020, 8:16 PM
Excellent - will try this
10 Views