[英]add S3 trigger on a Lambda function with cloudformation yaml
So I have this issue that I would love to:所以我有这个问题,我很想:
This is something which has to be done in YAML这是必须在 YAML 中完成的事情
I have no clue how to set this up... What I've managed to so far, is to create an s3 bucket which worked perfectly, now I just need to attach it as a trigger to the lambda function.我不知道如何设置它......到目前为止,我已经设法创建一个完美运行的 s3 存储桶,现在我只需将它作为触发器附加到 lambda function 即可。
It's pretty easy to set up in the AWS console but I just don't have much experience with YAML.在 AWS 控制台中设置非常容易,但我对 YAML 没有太多经验。 So I have no idea how to set this up correctly, and the file is really sensitive so Its a pain in the ass:-).
所以我不知道如何正确设置它,而且文件非常敏感,所以它很痛苦:-)。
Wished end result with yaml: yaml 的希望最终结果:
I have built a small template, tested.我已经建立了一个小模板,经过测试。
The template are to该模板是
YML version YML 版本
---
AWSTemplateFormatVersion: '2010-09-09'
Description: This template is to create all resources for Config Service Api
Parameters:
LambdaArtifactBucketName:
Type: String
Default: befit-artifact
S3BucketName:
Type: String
Default: befit-test-s3
Resources:
ExampleS3:
Type: AWS::S3::Bucket
DependsOn: ExampleInvokePermission
Properties:
BucketName: !Ref S3BucketName
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:Put
Filter:
S3Key:
Rules:
- Name: suffix
Value: txt
Function: !GetAtt [ ExampleLambdaFunction, Arn]
ExampleInvokePermission:
Type: AWS::Lambda::Permission
DependsOn: ExampleLambdaFunction
Properties:
FunctionName:
Fn::GetAtt:
- ExampleLambdaFunction
- Arn
Action: lambda:InvokeFunction
Principal: s3.amazonaws.com
SourceArn:
Fn::Sub: arn:aws:s3:::${S3BucketName}
ExampleLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket:
Ref: LambdaArtifactBucketName
S3Key: emailnotification-1.0.0.jar
FunctionName: example-lambda-function
Handler: com.xxx.Example::handleRequest
Role: arn:aws:iam::057351434671:role/lambda_sqs
Runtime: java8
Timeout: '300'
MemorySize: 512
Outputs:
S3BucketSecureURL:
Value: !Join ['', ['https://', !GetAtt [ExampleS3, DomainName]]]
Description: Name of S3 bucket
Json version Json版
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template is to create all resources for Config Service Api",
"Parameters": {
"LambdaArtifactBucketName": {
"Type": "String",
"Default": "befit-artifact"
},
"S3BucketName": {
"Type": "String",
"Default": "befit-test-s3"
}
},
"Resources": {
"ExampleS3": {
"Type": "AWS::S3::Bucket",
"DependsOn": "ExampleInvokePermission",
"Properties": {
"BucketName": null,
"NotificationConfiguration": {
"LambdaConfigurations": [
{
"Event": "s3:ObjectCreated:Put",
"Filter": {
"S3Key": {
"Rules": [
{
"Name": "suffix",
"Value": "txt"
}
]
}
},
"Function": null
}
]
}
}
},
"ExampleInvokePermission": {
"Type": "AWS::Lambda::Permission",
"DependsOn": "ExampleLambdaFunction",
"Properties": {
"FunctionName": {
"Fn::GetAtt": [
"ExampleLambdaFunction",
"Arn"
]
},
"Action": "lambda:InvokeFunction",
"Principal": "s3.amazonaws.com",
"SourceArn": {
"Fn::Sub": "arn:aws:s3:::${S3BucketName}"
}
}
},
"ExampleLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "LambdaArtifactBucketName"
},
"S3Key": "emailnotification-1.0.0.jar"
},
"FunctionName": "example-lambda-function",
"Handler": "com.xxx.Example::handleRequest",
"Role": "arn:aws:iam::057351434671:role/lambda_sqs",
"Runtime": "java8",
"Timeout": "300",
"MemorySize": 512
}
}
},
"Outputs": {
"S3BucketSecureURL": {
"Value": null,
"Description": "Name of S3 bucket"
}
}
}
After the template ran, the output will be模板运行后,output 将是
Thanks,谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.