How to access CloudFront Distribution with cdklocal

I’m using cdk to deploy my AWS stacks and using cdklocal as a wrapper for localstack. So far it has been working great but I’m not sure how to find my CloudFront Distribution. Below is my cdk code for creating a CloudFront Distribution:

const originAccessIdentity = new OriginAccessIdentity(this, 'OriginAccessIdentity');
bucket.grantRead(originAccessIdentity);

new Distribution(this, 'Distribution', {
  defaultRootObject: 'index.html',
  defaultBehavior: {
    origin: new S3Origin(bucket, { originAccessIdentity }),
  },
});

When I run cdklocal deploy it seems to work just find and if I run cdklocal synth I can see that the distribution is in the template. However, what is the URL for accessing the distribution?

1 Like

When using CDK (or in general CloudFormation) you can use CloudFormation outputs to export these values which should then work against both LocalStack and AWS.

In your case you can extend your CDK stack with:

new cdk.CfnOutput(this, 'CloudFrontDistributionDomainOutput', {
    value: distribution.distributionDomainName
})

where distribution is the Distribution construct you’ve used above.

When you deploy this stack cdk should print the resolved value for this output.

You can also look up the endpoint by using the AWS cli (or awslocal). You might have to export the distribution ID then though since it’s randomly generated. Alternatively if you don’t have too many you can just find out the ID by running awslocal cloudfront list-distributions

awslocal cloudfront get-distribution --id <your-distribution-id>

You can also find more information about CloudFront in LocalStack here: CloudFront | Docs

3 Likes