0

I have the below batch script for creating my Lambda function via the AWS CLI:

rem -----------------------------------------
rem create or update the lambda function
aws lambda create-function ^
    --function-name %LAMBDA_FUNCTION_NAME% ^
    --runtime python3.9 ^
    --role %LAMBDA_ROLE_ARN% ^
    --handler %LAMBDA_HANDLER% ^
    --zip-file fileb://%LAMBDA_ZIP_FILE% ^
    --profile %AWS_PROFILE% ^
    --region %REGION% ^
    --timeout 180 ^
    --memory-size 1024 ^
    --layers %LAMBDA_ARN_LAYER% ^
    --environment Variables={PYTHONPATH=python/lib}

@echo on
@echo Deployed the AWS Lambda function %LAMBDA_FUNCTION_NAME% in region %REGION%
@echo off

rem -----------------------------------------
rem add S3 trigger
aws lambda create-event-source-mapping ^
    --function-name %LAMBDA_FUNCTION_NAME% ^
    --event-source-arn arn:aws:s3:::%S3_BUCKET_NAME% ^
    --batch-size 1 ^
    --starting-position "LATEST" ^
    --profile %AWS_PROFILE% ^
    --region %REGION% ^
    --event-source-request-parameters Events=s3:ObjectCreated:* Filter='{"Key": {"Suffix": [".MF4",".MFC",".MFE",".MFM"]}}'

However, I get an error for the last part of the create-function:

Unknown options: --event-source-request-parameters, Filter='{Key:, {Suffix:, [.MF4,.MFC,.MFE,.MFM]}}', Events=s3:ObjectCreated:*

In what way is my syntax wrong? I want to use my S3 bucket as the trigger whenever a file with one of the file extensions listed is uploaded.

mfcss
  • 1,039
  • 1
  • 9
  • 25
  • --event-source-request-parameters is not mention in the [documentat](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/create-event-source-mapping.html). Likely you want --filter-critera – Anon Coward Jan 19 '23 at 19:52

1 Answers1

1

S3 event notification doesn't need an event source mapping. You should use the following CLI command instead to use your S3 bucket as the trigger:

aws s3api put-bucket-notification-configuration --bucket S3_BUCKET_NAME ...
jellycsc
  • 10,904
  • 2
  • 15
  • 32
  • I used the above with the following one line code, but I get an error because the filter cannot have more than 1 suffix? "{\"LambdaFunctionConfigurations\":[{\"LambdaFunctionArn\":\"arn:aws:lambda:eu-central-1:319723967016:function:canedge-influxdb-writer\",\"Events\":[\"s3:ObjectCreated:*\"],\"Filter\":{\"Key\":{\"FilterRules\":[{\"Name\":\"suffix\",\"Value\":\".MF4\"},{\"Name\":\"suffix\",\"Value\":\".MFC\"}]}}}]}" – mfcss Jan 19 '23 at 20:45
  • I also tried with just 1 suffix as follows: "{\"LambdaFunctionConfigurations\":[{\"LambdaFunctionArn\":\"arn:aws:lambda:eu-central-1:319723967016:function:canedge-influxdb-writer\",\"Events\":[\"s3:ObjectCreated:*\"],\"Filter\":{\"Key\":{\"FilterRules\":[{\"Name\":\"suffix\",\"Value\":\".MF4\"}]}}}]}" However in this case I get an error An error occurred (InvalidArgument) when calling the PutBucketNotificationConfiguration operation: Unable to validate the following destination configurations – mfcss Jan 19 '23 at 20:50
  • @mfcss https://stackoverflow.com/a/47674337/10692493 – jellycsc Jan 19 '23 at 21:05
  • Thanks, I think it got solved by adding this: aws lambda add-permission --function-name %LAMBDA_FUNCTION_NAME% --principal s3.amazonaws.com --statement-id %STATEMENT_ID% --action lambda:InvokeFunction --source-arn arn:aws:s3:::%S3_BUCKET% --source-account %AWS_ACCOUNT_ID% – mfcss Jan 19 '23 at 21:18
  • @mfcss No problem. I'm glad you find it helpful. – jellycsc Jan 19 '23 at 21:22