【问题标题】:Configure Cloudformation to have requests to GET /subdirectory serve /subdirectory/index.html using Cloudfront & S3配置 Cloudformation 以使用 Cloudfront 和 S3 对 GET /subdirectory 服务 /subdirectory/index.html 的请求
【发布时间】:2020-03-04 13:14:03
【问题描述】:

我有一个 Cloudformation 模板,用于设置 AWS::CloudFront::Distribution 和 AWS::S3::Bucket。不幸的是,对 GET /subdirectory 的请求以 403 响应。如何配置 Cloudformation 模板以让 GET /subdirectory 服务 /subdirectory/index.html?

我的 Cloudfront 配置如下:

  CloudFrontDistribution:
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Aliases:
          - !FindInMap [Domain, !Ref Stage, Domain]
        ViewerCertificate:
          AcmCertificateArn: !Ref Cert
          SslSupportMethod: sni-only
        CustomErrorResponses:
        - ErrorCode: 403 # not found
          ResponseCode: 404
          ResponsePagePath: !Ref ErrorPagePath
        DefaultCacheBehavior:
          AllowedMethods:
          - GET
          - HEAD
          - OPTIONS
          CachedMethods:
          - GET
          - HEAD
          - OPTIONS
          Compress: true
          DefaultTTL: 3600 # in seconds
          ForwardedValues:
            Cookies:
              Forward: none
            QueryString: false
          MaxTTL: 86400 # in seconds
          MinTTL: 60 # in seconds
          TargetOriginId: s3origin
          ViewerProtocolPolicy: redirect-to-https
        DefaultRootObject: !Ref DefaultRootObject
        Enabled: true
        HttpVersion: http2
        Origins:
        - DomainName: !GetAtt 'S3Bucket.DomainName'
          Id: s3origin
          S3OriginConfig:
            OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}'
        PriceClass: 'PriceClass_All'

除了对 GET /subdirectory 的请求之外,一切正常。

我也试过了:

        - DomainName: !GetAtt 'S3Bucket.RegionalDomainName'
          Id: s3origin
          S3OriginConfig:
            OriginProtocolPolicy: http-only

但是我在AWS::CloudFormation::Stack 上收到了错误Property TemplateURL cannot be empty.

【问题讨论】:

标签: amazon-web-services amazon-s3 amazon-cloudformation amazon-cloudfront aws-sam


【解决方案1】:

Please check this docs, you can find an example like:

'use strict';

 const querystring = require('querystring');

 exports.handler = (event, context, callback) => {
     const request = event.Records[0].cf.request;

     /**
      * Reads query string to check if S3 origin should be used, and
      * if true, sets S3 origin properties.
      */

     const params = querystring.parse(request.querystring);

     if (params['useS3Origin']) {
         if (params['useS3Origin'] === 'true') {
             const s3DomainName = 'my-bucket.s3.amazonaws.com';

             /* Set S3 origin fields */
             request.origin = {
                 s3: {
                     domainName: s3DomainName,
                     region: '',
                     authMethod: 'none',
                     path: '',
                     customHeaders: {}
                 }
             };
             request.headers['host'] = [{ key: 'host', value: s3DomainName}];
         }
     }

    callback(null, request);
};

您可以使用请求中的查询字符串将其更改为指向正确的path

勾选此项设置CloudFormation, there is an example on how to set up the trigger

Resources:
    CFDistribution:
        Type: AWS::CloudFront::Distribution
        Properties:
          DistributionConfig:
            Enabled: 'true'
            Comment: !Sub '${Stage} - CI/CD for Lambda@Edge'
            Aliases:
              - !FindInMap [AliasMap, !Ref Stage, Alias]
            Origins:
              -
                Id: MyOrigin
                DomainName: aws.amazon.com
                CustomOriginConfig:
                  HTTPPort: 80
                  OriginProtocolPolicy: match-viewer
            DefaultCacheBehavior:
              TargetOriginId: MyOrigin
              LambdaFunctionAssociations:
                - 
                  EventType: origin-request
                  LambdaFunctionARN: !Ref LambdaEdgeFunctionSample.Version

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多