2

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 Answer 1

1

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"))
)
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks. Yes I'm currently following that method, but I don't want to hardcode my URL. I'm creating the API using the cdk, so I have an api_stack.py that runs apigw.HttpApi(...), and I'm then taking that instance's property url and trying to pass it into HttpOrigin. The problem is that the url value looks like this ${Token[TOKEN.622]}.execute-api.eu-west-2.${Token[AWS.URLSuffix.8]}/ (https:// prefix manually removed).
I presume these Tokens are a way of waiting for AWS to resolve values, though they're new to me.
It also seems it would be very possible using the console, though I want to keep it all in code.
Here's an example of a dynamically passed url (though they're using RestApi). I presume they don't have Token issues.
Sorry, I couldn't understand your concerns can you please explain a bit more clearly?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.