简体   繁体   English

如何为 Cloudfront 创建 HttpOrigin 以使用 Lambda function url?

[英]How do I create a HttpOrigin for Cloudfront to use a Lambda function url?

Trying to setup a Cloudfront behaviour to use a Lambda function url with code like this:尝试设置Cloudfront行为以使用Lambda function url代码如下:

    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: functionUrl object 在不同的堆栈中创建并传递到cloudformation堆栈,定义如下所示:

    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 .据推测,这是因为functionUrl.url的计算结果类似于https://xxx.lambda-url.ap-southeast-2.on.aws/ (注意https:// ),而HttpOrigin参数应该只是域名像xxx.lambda-url.ap-southeast-2.on.aws

I can't just write code to hack the url up though (ie functionUrl.url.replace("https://", "") ) because when my code executes, the value or the url property is a token like ${Token[TOKEN.350]} .我不能只编写代码来破解 url(即functionUrl.url.replace("https://", "") ),因为当我的代码执行时,值或url属性是${Token[TOKEN.350]}

The function url is working properly: if I hard-code the HttpOrigin to the function url's value (ie like xxx.lambda-url.ap-southeast-2.on.aws ) - it works fine. function url 工作正常:如果我将 HttpOrigin 硬编码为HttpOrigin url 的值(即像xxx.lambda-url.ap-southeast-2.on.aws )- 它工作正常。

How do I use CDK code to setup the reference from Cloudfront to the function url?如何使用 CDK 代码设置从 Cloudfront 到 function url 的引用?

I'm using aws-cdk version 2.21.1.我正在使用aws-cdk版本 2.21.1。


There is an open issue to add support: https://github.com/aws/aws-cdk/issues/20090有一个开放的问题来添加支持: https://github.com/aws/aws-cdk/issues/20090

UseCloudFormation Intrinsic Functions to parse the url string:使用CloudFormation Intrinsic Functions解析 url 字符串:

cdk.Fn.select(2, cdk.Fn.split('/', functionUrl.url));

// -> 7w3ryzihloepxxxxxxxapzpagi0ojzwo.lambda-url.us-east-1.on.aws

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在lambda function中使用动态资源URL - How to use dynamic resource URL in lambda function Cloudfront:转发主机 header 到 lambda function URL origin - Cloudfront: Forward host header to lambda function URL origin 通过单个 lambda function 将s3中托管的文件内容解压到多个cloudfront url - Unzip file content hosted in s3 to multiple cloudfront url through a single lambda function 如何在我的 Lambda Function 中使用带有回调返回步骤 Function 的 Heartbeat? - How do I use Heartbeat with a Callback Return Step Function in my Lambda Function? Terraform 中的可选 CloudFront Lambda 函数关联 - Optional CloudFront Lambda function association in Terraform 如何通过 S3 和 Lambda@Edge 从部署在 Cloudfront 上的 Nextjs API 抓取 Request URL - How to grab Request URL from Nextjs API deployed on Cloudfront through S3 and Lambda@Edge 如何在云端 lambda@Edge Function 中访问 S3 存储桶 object? - How access S3 bucket object in cloudfront lambda@Edge Function? 如何使用 AWS CLI 创建 AWS Lambda 函数? - How can I create an AWS Lambda function using the AWS CLI? 如何为 seaborn 创建 AWS Lambda 层? - How do I create an AWS Lambda layer for seaborn? 如何找到 lambda function 的端点 API? - How do I find the API endpoint of a lambda function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM