【发布时间】:2017-10-01 22:14:55
【问题描述】:
我想使用无服务器应用程序模型 (SAM) 和 CloudFormation 创建一个简单的 lambda 函数,该函数在 S3 存储桶中创建对象时触发(例如 thescore-cloudfront-trial)。如何启用从 S3 存储桶到 Lambda 函数的触发器?下面是我的 python3 boto3 代码。
region = 'us-east-1'
import boto3
test_lambda_template = {
'AWSTemplateFormatVersion': '2010-09-09',
'Transform': 'AWS::Serverless-2016-10-31',
'Resources': {
'CopyS3RajivCloudF': {
'Type': 'AWS::Serverless::Function',
'Properties': {
"CodeUri": 's3://my-tmp/CopyS3Lambda',
"Handler": 'lambda.handler',
"Runtime": 'python3.6',
"Timeout": 300,
"Role": 'my_existing_role_arn'
},
'Events': {
'Type': 'S3',
'Properties': {
"Bucket": "thescore-cloudfront-trial",
"Events": 's3:ObjectCreated:*'
}
}
},
'SrcBucket': {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": 'thescore-cloudfront-trial',
}
}
}
}
conf = config.get_aws_config('development')
client = aws.client(conf, 'cloudformation', region_name=region)
response = client.create_change_set(
StackName='RajivTestStack',
TemplateBody=json.dumps(test_lambda_template),
Capabilities=['CAPABILITY_IAM'],
ChangeSetName='a',
Description='Rajiv ChangeSet Description',
ChangeSetType='CREATE'
)
response = client.execute_change_set(
ChangeSetName='a',
StackName='RajivTestStack',
)
【问题讨论】:
-
你考虑过阅读the docs吗?
-
1.使用 AWS 控制台在您的 S3 存储桶中添加触发器。 2.使用boto3添加你的事件:boto3.readthedocs.io/en/latest/reference/services/…
-
@mootmoot:我已经有 python 脚本在做你列出的事情:)。我想搬到 SAM。
标签: amazon-web-services amazon-s3 aws-lambda amazon-cloudformation boto3