Reputation: 21544
Trying to setup a Cloudfront
behaviour to use a Lambda function url with code like this:
this.distribution = new Distribution(this, id + "Distro", {
comment: id + "Distro",
defaultBehavior: {
origin: new S3Origin(s3Site),
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
additionalBehaviors: {
[`api-prd-v2/*`]: {
compress: true,
originRequestPolicy: originRequestPolicy,
origin: new HttpOrigin(functionUrl.url, {
protocolPolicy: OriginProtocolPolicy.HTTPS_ONLY,
originSslProtocols: [OriginSslPolicy.TLS_V1_2],
}),
allowedMethods: AllowedMethods.ALLOW_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
cachePolicy: apiCachePolicy,
},
The functionUrl
object is created in a different stack and passed in to the cloudformation
stack, the definition looks like:
this.functionUrl = new FunctionUrl(this, 'LambdaApiUrl', {
function: this.lambdaFunction,
authType: FunctionUrlAuthType.NONE,
cors: {
allowedOrigins: ["*"],
allowedMethods: [HttpMethod.GET, HttpMethod.POST],
allowCredentials: true,
maxAge: Duration.minutes(1)
}
});
The above code fails because "The parameter origin name cannot contain a colon".
Presumably, this is because functionUrl.url
evaluates to something like https://xxx.lambda-url.ap-southeast-2.on.aws/
(note the https://
) whereas the HttpOrigin
parameter should just be the domain name like xxx.lambda-url.ap-southeast-2.on.aws
.
I can't just write code to hack the url up though (i.e. functionUrl.url.replace("https://", "")
) because when my code executes, the value or the url
property is a token like ${Token[TOKEN.350]}
.
The function url is working properly: if I hard-code the HttpOrigin
to the function url's value (i.e. like xxx.lambda-url.ap-southeast-2.on.aws
) - it works fine.
How do I use CDK code to setup the reference from Cloudfront to the function url?
I'm using aws-cdk
version 2.21.1.
There is an open issue to add support: https://github.com/aws/aws-cdk/issues/20090
Upvotes: 5
Views: 2460
Reputation: 25759
Use CloudFormation Intrinsic Functions to parse the url string:
cdk.Fn.select(2, cdk.Fn.split('/', functionUrl.url));
// -> 7w3ryzihloepxxxxxxxapzpagi0ojzwo.lambda-url.us-east-1.on.aws
Upvotes: 14