Using Cloudfront with an HTTP API Origin

659 Views Asked by At

I want to redirect HTTP traffic to HTTPS. So I want to put my app, which runs on an API Gateway, behind Cloudfront. But I want to use aws_cdk.aws_cloudfront to launch my Cloudfront instance.

self.distribution = cloudfront.Distribution(
    self,
    construct_id,
    default_behaviour=cloudfront.BehaviorOptions(
        origin=cloudfront_origins.RestApiOrigin(api),
        ...

This would be my ideal implementation. But my api is of type HttpApi; I am using aws_apigatewayv2_alpha.

Is there any way I can use an HttpApi as the origin of my Cloudfront distribution, using aws_cloudfront?

1

There are 1 best solutions below

12
On

You can create Origins from any HTTP endpoint like below, given the domain name, and optionally, other origin properties.

# Creates a distribution from an HTTP endpoint
cloudfront.Distribution(self, "myDist",
    default_behavior=cloudfront.BehaviorOptions(origin=origins.HttpOrigin("www.example.com"))
)