aws VS2017 serverless app syntax in serverless.template to add filter to s3 notification

时间:2018-06-04 16:57:26

标签: c# aws-lambda amazon-cloudformation

I have created a VS2017 C# app using the AWS Serverless Application template with the "Simple S3 Function" blueprint. The CloudFormation serverless.template file contains a spec for my handler function with an event spec to respond to "s3.ObjectCreated:*" events. I am trying to add a filter specification to that event spec to only respond to events with the "Source/" prefix. Here is my code:

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Transform" : "AWS::Serverless-2016-10-31",
  "Description" : "Template that creates a S3 bucket and a Lambda function that will be invoked when new objects are upload to the bucket.",
  "Parameters" : {
    "BucketName" : {
        "Type" : "String",
        "Description" : "Name of S3 bucket to be created. The Lambda function will be invoked when new objects are upload to the bucket. If left blank a name will be generated.",
        "MinLength" : "0"
    }
  },

  "Conditions" : {
    "BucketNameGenerated" : {"Fn::Equals" : [{"Ref" : "BucketName"}, ""]}
  },


  "Resources" : {

    "Bucket" : {
        "Type" : "AWS::S3::Bucket",
        "Properties" : {
            "BucketName" : { "Fn::If" : ["BucketNameGenerated", {"Ref" : "AWS::NoValue" }, { "Ref" : "BucketName" } ] }
        }
    },

    "S3Function" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "DCATInventory::DCATInventory.Function::FunctionHandler",
        "Runtime": "dotnetcore2.0",
        "CodeUri": "",
        "Description": "Default function",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaFullAccess", "AmazonRekognitionReadOnlyAccess" ],
        "Events": {
            "NewImagesBucket" : {
                "Type" : "S3",
                "Properties" : {
                    "Bucket" : { "Ref" : "Bucket" },
                    "Events" : [
                        "s3:ObjectCreated:*"
                    ],
                    "Filter" : {
                        "S3Key" : {
                            "Rules" : [{
                                "Name" : "prefix", 
                                "Value": "Source/"
                            }]
                        }
                    }
                }
            }
        }
      }
    }
  },
  "Outputs" : {
    "Bucket" : {
        "Value" : {"Ref":"Bucket"},
        "Description" : "Bucket that will invoke the lambda function when new objects are created."
    }
  }
}

This is the default code generated by the template with only the Filter spec added to the event properties. I am receiving an error stating "Rules key is invalid for this object" on line 48. I have read the documentation and googled this and this seems to be the correct syntax. Did I specify something wrong here? Thanks in advance.

1 个答案:

答案 0 :(得分:1)

事实证明,即使Visual Studio报告错误,上面显示的语法也是正确的。即使出现此错误,我也决定尝试将应用程序发布到AWS。我预计我会从CloudFront收到错误,但它已成功发布。 S3 Event确实发布并包含带有“Source /”前缀的过滤规则。