sum*_*eva 7 amazon-s3 amazon-web-services aws-cloudformation aws-lambda aws-sam
是否可以在单独的 CloudFormation 模板中创建 S3 存储桶和触发的 Lambda。我希望将长时间运行的资源堆栈与 Lambda 等经常更新的资源堆栈分开
当尝试单独创建 Lambda 时,它表示 lambda 事件中定义的存储桶应该在同一模板中定义,并且不能被引用。
S3 事件必须引用同一模板中的 S3 存储桶。
GetFileMetadata:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${targetenv}-lambdaname'
CodeUri: target-file-0.0.1-SNAPSHOT.jar
Handler: LambdaFunctionHandler::handleRequest
Runtime: java8
Timeout: 30
MemorySize: 512
Environment:
Variables:
STAGE: !Sub '${targetenv}'
Events:
S3Event:
Type: S3
Properties:
Bucket:
Ref: MyS3Bucket
Events:
- 's3:ObjectCreated:*'
MyS3Bucket:
Type: 'AWS::S3::Bucket'
DependsOn: BucketPermission
Properties:
BucketName: !Sub 'bucketname-${targetenv}'
Run Code Online (Sandbox Code Playgroud)
2021 年 11 月 21 日,AWS 宣布通过 Amazon EventBridge 提供 S3 事件通知。因此,您可以部署一个具有已启用 EventBridge 集成的 S3 存储桶的堆栈,然后部署具有由特定存储桶的 EventBridge 事件触发的 Lambda 函数的第二个堆栈。
持久化堆栈:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Stack with S3 bucket with EventBridge event notification enabled'
Parameters:
BucketName:
Type: String
Description: 'Name of the bucket to be created'
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
NotificationConfiguration:
EventBridgeConfiguration:
EventBridgeEnabled: true
# Alternatively shorthand config
# EventBridgeConfiguration: {}
Run Code Online (Sandbox Code Playgroud)
应用程序堆栈:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Stack with Lambda for procesing S3 events via EventBridge
Parameters:
BucketName:
Type: String
Description: Name of the bucket to listen events from
Resources:
S3EventProcessor:
Type: AWS::Serverless::Function
Properties:
FunctionName: S3EventListener
Architectures:
- arm64
Runtime: nodejs14.x
Handler: index.handler
InlineCode: |
exports.handler = (event, context) => {
console.log('event:', JSON.stringify(event));
}
Events:
S3EventBridgeRule:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.s3
detail:
bucket:
name:
- !Ref BucketName
Run Code Online (Sandbox Code Playgroud)
通过配置Pattern,您可以过滤事件流以获取更具体的事件,例如Object Create
或Object Deleted
、文件名、文件扩展名等。请在EventBridge 用户指南中查找更多信息
Joh*_*ein -1
该模板正在创建一个存储桶 ( MyS3Bucket
)。
然后,无服务器函数正在引用它:
Bucket:
Ref: MyS3Bucket
Run Code Online (Sandbox Code Playgroud)
如果您想从另一个模板引用该存储桶,您可以从第一个堆栈中导出存储桶名称:
Outputs:
S3Bucket:
Description: Bucket that was created
Value: !Ref MyS3Bucket
Export:
Name: Stack1-Bucket
Run Code Online (Sandbox Code Playgroud)
然后,将其导入到第二个堆栈中:
Bucket:
Fn::ImportValue:
Stack1-Bucket
Run Code Online (Sandbox Code Playgroud)
请参阅:导出堆栈输出值 - AWS CloudFormation
归档时间: |
|
查看次数: |
5374 次 |
最近记录: |