gpt4 book ai didi

amazon-web-services - 配置 Cloudformation 以使用 Cloudfront 和 S3 向 GET/subdirectoryserve/subdirectory/index.html 发出请求

转载 作者:行者123 更新时间:2023-12-03 07:43:31 26 4
gpt4 key购买 nike

我有一个 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 不能为空。

最佳答案

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);
};

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

选中此项以设置 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

关于amazon-web-services - 配置 Cloudformation 以使用 Cloudfront 和 S3 向 GET/subdirectoryserve/subdirectory/index.html 发出请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58757497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com