1. Enabling CDN for content delivery with GCP's Cloud CDN

    TypeScript

    To enable CDN for content delivery with Google Cloud Platform (GCP)'s Cloud CDN, you'll need to create a backend service or a backend bucket, which will be used as the origin of your content for the CDN. The CDN will cache HTTP(S) Load Balanced content from these backends at edge locations to provide users with faster access by serving content from locations that are closer to them.

    In this program, we'll create a backend bucket on GCP, enable Cloud CDN for that bucket, and configure the CDN settings. We'll be using the google-native.compute/v1.BackendBucket resource because it specifically allows you to set CDN policies and integrate it with Cloud CDN. We choose google-native over gcp because google-native provides direct access to Google Cloud APIs using Pulumi, and this example matches closer to the direct usage patterns of Google's API.

    Here's how you can write a Pulumi program in TypeScript to accomplish this:

    1. Create the Bucket: First, we create a GCP storage bucket that will hold the static assets you want to serve.
    2. Configure the Backend Bucket: Next, we set up a backend bucket.
    3. Enable CDN: We enable the CDN by setting the enableCdn property and configure the CDN policy (optional).
    import * as gcp from "@pulumi/gcp"; // Create a GCP storage bucket. const storageBucket = new gcp.storage.Bucket("my-bucket", { location: "US", }); // Create a backend bucket. This bucket will act as an origin for the CDN. const backendBucket = new gcp.compute.BackendBucket("my-backend-bucket", { bucketName: storageBucket.name, // Reference the storage bucket created above enableCdn: true, // Enable Cloud CDN // Optionally, configure CDN policies cdnPolicy: { signedUrlCacheMaxAgeSec: "7200", // Signed URLs cached for 2 hours cacheMode: 'CACHE_ALL_STATIC', // Cache static content }, // Description is optional but recommended for documenting infrastructure description: "An example backend bucket for Cloud CDN", }); export const bucketUrl = storageBucket.url; // Export the storage bucket URL export const backendBucketUrl = backendBucket.selfLink; // Export the backend bucket URL

    In this program:

    • We used the @pulumi/gcp package, which provides the Google Cloud resources.
    • We created a Cloud Storage bucket using gcp.storage.Bucket. This is where your static files are stored for hosting.
    • Then we defined a gcp.compute.BackendBucket which represents a Cloud Storage bucket as a backend service for an HTTP(S) load balancer.
    • We enabled Cloud CDN by setting the property enableCdn to true.
    • Optionally, we set a cdnPolicy to customize the caching and signed URLs. You can define other properties within cdnPolicy to tailor the CDN to your needs.
    • Lastly, we exported the URLs of the Cloud Storage bucket and the backend bucket, which can be useful for further configuration or for reference in other parts of your infrastructure.

    By running this Pulumi program with pulumi up, it will provision these resources in your GCP project. Pulumi provides verbose output describing the changes it intends to make, and after your confirmation, will proceed to make those changes. Once applied, the CDN will be ready to serve cached content from the backend bucket, enhancing the delivery speed for global users.