gpt4 book ai didi

aws-cloudformation - Serverless.js 和 Lambda@Edge : specifying origin

转载 作者:行者123 更新时间:2023-12-03 07:38:44 25 4
gpt4 key购买 nike

我正在尝试创建一个具有两个源(均为 S3 存储桶)的 CloudFront 发行版(使用 Serverless.js)。有一个为公共(public)网站提供服务的默认源,以及以 /attachments 开头的路径的源。第二个源有一个查看者响应 Lambda@Edge 函数来处理一些身份验证。

当前的实现是使用一些自制脚本和 JSON CloudFormation 模板进行部署的,该模板可以正常工作。然而,我正在尝试摆脱那些特殊的脚本,并在 Serverless.js 上进行标准化(它至少具有作为标准工具的好处)。

但是,我在这里很难让 Serverless.js 屈服于我的意愿。这是我的 serverless.yml 文件的 resouces 部分(无论如何,相关位):

resources:
Resources:
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
// all the usual properties...PriceClass, Enabled, etc omitted for brevity
DefaultCacheBehavior:
TargetOriginId: AppBucket
Origins:
- DomainName: foo-attachments.s3.amazonaws.com
Id: AttachmentsBucket
- DomainName: foo-app.s3.amazonaws.com
Id: AppBucket

该部分工作正常,并且原点创建正确。附加viewer-response函数是事情开始出现问题的地方:

functions:
viewerRequest:
handler: viewerRequest.authorize
events:
- cloudFront:
eventType: viewer-response
pathPattern: '*-resources/*'
origin: ????????

official documentation似乎表明您使用 URI 指定来源,例如 s3://foo-attachments.s3.amazonaws.com。当我尝试这样做时,我收到了 CF 错误,原因很清楚。如果我查看实际生成的更新模板 (./serverless/foo-template-update-stack.json),这就是我看到的内容(仅相关位):

  "CloudFrontDistribution": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"Origins": [
{
"Id": "AttachmentsBucket",
"DomainName": "foo-attachments.s3.amazonaws.com"
}
],
"CacheBehaviors": [
"TargetOriginId": "s3/foo-attachments.s3.amazonaws.com"

注意损坏的 TargetOriginId。它应该AttachmentsBucket(至少这在旧的自制脚本中可以工作)。请注意,它还会破坏 URI,这很奇怪,因为这似乎是它所期望的。

我已经尝试过:

  • 在函数中指定整个第二个源(同时出现 Serverless.js 和 CloudFormation 错误)
  • 使用AttachmentsBucket; Serverless.js 提示,更新模板中写入的是 custom/NullAttachmentsBucket (显然它需要一个 URI)
  • 还有六件事我不记得了。

Serverless.js 将使此部署更加惯用 - 我喜欢它处理更新 Lambda@Edge 函数的繁琐方面 - 但我无法让它执行我想要的操作!

最佳答案

我使用这个无服务器插件将 Cloudfront 分发资源与 lambda 函数链接 Serverless Plugin: Support CloudFront Lambda@Edge

这对我有用

service: image-resizer--delivery-data-6

provider:
name: aws
runtime: nodejs18.x
region: us-east-1

functions:
authenticateFunction:
handler: authenticate/index.handler
lambdaAtEdge:
distribution: 'MyCloudFrontDistribution'
eventType: 'origin-request'

resizeImageFunction:
handler: resize-image/index.handler
lambdaAtEdge:
distribution: 'MyCloudFrontDistribution'
eventType: 'origin-response'
resources:
Resources:
ImagesS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.s3Bucket}

MyCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
AllowedMethods:
- HEAD
- DELETE
- POST
- GET
- OPTIONS
- PUT
- PATCH
TargetOriginId: ImagesS3BucketOrigin
ViewerProtocolPolicy: redirect-to-https
Origins:
- DomainName: bucket.s3.amazonaws.com
Id: ImagesS3BucketOrigin
S3OriginConfig:
OriginAccessIdentity:
Fn::Join:
- ''
-
- 'origin-access-identity/cloudfront/'
- Ref: cloudfrontoriginaccessidentity

BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ImagesS3Bucket
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: '*'
Action:
- s3:GetObject
- s3:PutObject
Resource: arn:aws:s3:::${self:custom.s3Bucket}/*
Condition:
StringEquals:
aws:SourceArn:
- !Join
- ''
- - 'arn:aws:cloudfront::ACCOUNT_NUMBER:distribution/'
- !GetAtt MyCloudFrontDistribution.Id

cloudfrontoriginaccessidentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "Cloudfront dist's OAI"

plugins:
- '@silvermine/serverless-plugin-cloudfront-lambda-edge'

custom:
s3Bucket: S3BUCKETNAME

关于aws-cloudformation - Serverless.js 和 Lambda@Edge : specifying origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75342712/

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