0

我想用无服务器应用模型(SAM)和CloudFormation创建时,在S3存储桶(例如thescore-cloudfront-trial)创建一个对象,它被触发一个简单的lambda函数。如何启用S3存储区到Lambda函数的触发器?以下是我的python3 boto3代码。AWS无服务器应用模式:创建S3事件与λ

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', 
    ) 
+1

你有没有考虑阅读[文档](http://docs.aws.amazon.com/lambda/latest/dg/with-s3.html)? – stdunbar

+0

1.使用AWS控制台在您的S3存储桶中添加一个触发器。 2.使用boto3添加您的活动:http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.put_bucket_notification_configuration – mootmoot

+0

@mootmoot:我已经有Python脚本你做什么所列: )。我想转移到SAM。 – RAbraham

回答

0

我想通了带警告

买者1:触发通知将显示在控制台S3,但不是在LAMBDA控制台。我使用boto3 s3和lambda客户端(我想要替换)的现有python部署脚本在两个控制台中显示通知。

买者2:用于监控,我看到我的拉姆达触发只有当我切换到看到拉姆达别名视图。但是我没有为我的lambda指定一个别名。所以我不知道为什么我没有看到它在非别名视图(只看到最新版本)

我不得不修改Events关键是这样的:

'Events': { 
    'RajivCopyEvent': { 
     'Type': 'S3', 
     'Properties': { 
     "Bucket": {"Ref": "SrcBucket"}, 
     "Events": "s3:ObjectCreated:*" 

     } 
    } 

}