簡體   English   中英

aws-cdk 將 Lambda 函數與 CloudFront Web 分配相關聯

[英]aws-cdk Associate Lambda Function with CloudFront Web Distribution

我正在嘗試使用aws-cdk創建 CloudFront Web Distribution。 我能夠成功創建 Web 發行版,但我還無法弄清楚如何關聯 lambda 函數。

下面是我用於創建 CloudFront Web 分配的 Typescript aws-cdk 代碼片段。 我刪除了一些不相關的代碼。

new cloudfront.CloudFrontWebDistribution(this, 'RetsFilesCDN', {
        originConfigs: [
            {
                s3OriginSource: {
                    originAccessIdentity: cfAccess, /* A CfnCloudFrontOriginAccessIdentity object created in earlier code */
                    s3BucketSource: files /* S3 bucket created in earlier code */
                },
                behaviors: [
                    {
                        compress: true,
                        defaultTtlSeconds: 172800,
                        isDefaultBehavior: true,
                        maxTtlSeconds: 31536000,
                        minTtlSeconds: 0
                    }
                ]
            }
        ]
    });

我試圖讓它生成的 CloudFormation 代碼是這樣的:

RetsFilesCDNCFDistribution6F414E1A:
Type: AWS::CloudFront::Distribution
Properties:
  DistributionConfig:
    CacheBehaviors:
      []
    Comment: CDN for files from the Real Estate RETS services that BranchCMS
      integrates with
    DefaultCacheBehavior:
      AllowedMethods:
        - GET
        - HEAD
      CachedMethods:
        - GET
        - HEAD
      Compress: true
      DefaultTTL: 172800
      ForwardedValues:
        Cookies:
          Forward: none
        QueryString: false
      MaxTTL: 259200
      MinTTL: 172800
      LambdaFunctionAssociations:
        - EventType: origin-response
          LambdaFunctionARN: lambdaFunctionArnHere
      TargetOriginId: origin1
      ViewerProtocolPolicy: redirect-to-https
    DefaultRootObject: index.html
    Enabled: true
    HttpVersion: http2
    IPV6Enabled: true
    Origins:
      - DomainName:
          Fn::GetAtt:
            - RetsFilesC9F78E92
            - DomainName
        Id: origin1
        S3OriginConfig:
          OriginAccessIdentity:
            Fn::Join:
              - ""
              - - origin-access-identity/cloudfront/
                - Ref: RetsFilesAccess
    PriceClass: PriceClass_100
    ViewerCertificate:
      AcmCertificateArn: arn:aws:acm:us-east-1:666445282096:certificate/25d4967c-c29a-4d11-983f-86d709769372
      SslSupportMethod: sni-only

我似乎無法生成的確切部分是:

LambdaFunctionAssociations:
   - EventType: origin-response
     LambdaFunctionARN: lambdaFunctionArnHere

預先感謝您的幫助。

我不確定這是否是最好的技術,但以下對我有用。

 import cdk = require('@aws-cdk/cdk'); import cloudfront = require('@aws-cdk/aws-cloudfront'); import lambda = require('@aws-cdk/aws-lambda'); export class MyStack extends cdk.Stack { constructor(parent: cdk.App, name: string, props?: cdk.StackProps) { super(parent, name, props); // Create the Lambda function const lambdaFunc = new lambda.Function(this, 'MyLambda', { YOUR_LAMBDA_PROPERTIES: HERE }); // Create the CloudFront Web Distribution const cf = new cloudfront.CloudFrontWebDistribution(this, 'MyCDN', { YOUR_CLOUDFRONT_PROPERTIES: HERE }); /** * THIS IS THE BEGINNING OF THE SOLUTION */ // Get the CloudFront Distribution object to add the LambdaFunctionAssociations to const cfDist = cf.findChild('CFDistribution') as cloudfront.CfnDistribution; // Manually add the LambdaFunctionAssociations by adding an override cfDist.addOverride('Properties.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations', [{ EventType: 'origin-response', LambdaFunctionARN: lambdaFunc.functionArn + ':2' }]); /** * END OF SOLUTION */ } }

您可以使用此屬性將 lambda 函數與您的雲前端緩存行為相關聯

import { CfnDistribution.CacheBehaviorProperty } from '@aws-cdk/aws-cloudfront';
CfnDistribution.CacheBehaviorProperty.LambdaFunctionAssociations

有關更多信息,請單擊此鏈接

是否可以將新創建​​的函數(lamda@edge / cloudfront 函數)與現有的 cloudfront 分布相關聯? 上面的代碼似乎只適用於新創建的發行版。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM