How can I lookup and modify a Cloudfront distribution using the CDK?

0

How do I lookup a cloudfront distribution using the CDK? The only option I see is

    const distribution = cf.CloudFrontWebDistribution.fromDistributionAttributes(stack, "DataApiDistribution", {
      domainName: "...",
      distributionId: "...",
    });

Neither domainName and distributionId is known by me in this context, so how can I solve this? Things I do know about my distribution:

  • What tags it has,
  • what domain it is exposed as in route53

When I have the distribution, Id like to add more origins to it

import * as cf from "aws-cdk-lib/aws-cloudfront";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as route53Targets from "aws-cdk-lib/aws-route53-targets";


export default function DataAPI({ stack, app }: StackContext) {
  const host =
  stack.stage === "prod"
    ? "data.mydomain"
    : stack.stage === "dev"
      ? "data.dev.mydomain"
      : "data." + stack.stage + ".dev.mydomain";

  if (app.region === "eu-north-1") {
    // Create CloudFront distribution
    const distribution = new cf.CloudFrontWebDistribution(stack, "DataApiDistribution", {
      originConfigs: [
        {
          s3OriginSource: {
            s3BucketSource: myBucketInEUNorth1,
          },
          behaviors: [{ isDefaultBehavior: true, viewerProtocolPolicy: cf.ViewerProtocolPolicy.REDIRECT_TO_HTTPS }],
        },
      ],
    });
    const hostedZone = route53.HostedZone.fromLookup(stack, "domain", {
      domainName: app.stage === "prod" ? "mydomain" : "dev.mydomain",
    });

    const endpoint = new route53.ARecord(stack, "DataApiAliasRecord", {
      zone: hostedZone,
      target: route53.RecordTarget.fromAlias(new route53Targets.CloudFrontTarget(distribution)),
      recordName: host,
    });
    // Output CloudFront distribution URL
    stack.addOutputs({
      CloudFrontURL: endpoint.domainName,
    });

    return {
      URL: endpoint.domainName,
    };
  } else {
    // Here I need to lookup the CloudFront distribution created in eu-north-1 and add origins to it

    return null
  }
}
1 Answer
1

Hey there! Are you trying to add origins to the distribution that you're creating in the second code block you provided? If that's the case, you can either list more than one originConfig object when defining the distribution or simply refer back to the distribution variable you assigned the new distribution to, then use the addBehavior method like this:

distribution.addBehavior(pathPattern, origin, behaviorOptions)

If you're trying to find and modify a distribution that has been created outside of the context of your CDK script, you would need to use ListDistributionsCommand() and UpdateDistributionCommand() from the SDK instead.

Hope this helps out!

AWS
segrace
answered a month ago
profile picture
EXPERT
reviewed a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions