Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 在CLI中的S3事件上调用AWS Lambda函数_Amazon Web Services_Amazon S3_Aws Lambda_Aws Cli - Fatal编程技术网

Amazon web services 在CLI中的S3事件上调用AWS Lambda函数

Amazon web services 在CLI中的S3事件上调用AWS Lambda函数,amazon-web-services,amazon-s3,aws-lambda,aws-cli,Amazon Web Services,Amazon S3,Aws Lambda,Aws Cli,我已经用CLI命令创建了Lambda函数 aws lambda create-function --function-name "functionName" --runtime "java8" --role "roleARN" --handler "com.company.package.S3FileCreator::createFile" --zip-file "fileb://./filePath.zip" 现在我想创建一个触发器函数,如果我在s3存储

我已经用CLI命令创建了Lambda函数

aws lambda create-function 
   --function-name "functionName" 
   --runtime "java8" 
   --role "roleARN" 
   --handler "com.company.package.S3FileCreator::createFile"
   --zip-file "fileb://./filePath.zip"
现在我想创建一个触发器函数,如果我在s3存储桶上放置任何对象,
我可以从Amazon控制台创建触发器,但是否可以从CLI创建触发器

我想您应该仔细阅读一下有关将Lambda与S3结合使用的内容

为了在文件上传到特定S3 bucket时触发lambda触发器,您需要在该S3 bucket上创建一个bucket通知。这个bucket通知应该在S3:ObjectCreated:Put上激活,因为您说过希望它在放置对象时激活

下面是使用CLI设置bucket通知的示例,下面是一些更接近您可能想要尝试的内容。仅供参考,我还没有测试过这个

your-bucket-notification-config.json

然后你会跑:

aws s3api put-bucket-notification-configuration --bucket your-bucket --notification-configuration file://your-bucket-notification-config.json

我想你应该仔细阅读一下关于将Lambda与S3结合使用的内容

为了在文件上传到特定S3 bucket时触发lambda触发器,您需要在该S3 bucket上创建一个bucket通知。这个bucket通知应该在S3:ObjectCreated:Put上激活,因为您说过希望它在放置对象时激活

下面是使用CLI设置bucket通知的示例,下面是一些更接近您可能想要尝试的内容。仅供参考,我还没有测试过这个

your-bucket-notification-config.json

然后你会跑:

aws s3api put-bucket-notification-configuration --bucket your-bucket --notification-configuration file://your-bucket-notification-config.json

我已通过以下步骤解决了问题:

在变量上分配函数名/s3桶名/rold ARN

FUNCTION_NAME=lambda-function-name
S3_BUCKE=s3_bucket_name
ROLE=AWS_role_ARN
创建Lambda函数并获取ARN

LAMBDA_ARN=$(aws lambda create-function --function-name "$FUNCTION_NAME" --runtime "java8"  --role "$ROLE" --handler "com.monsanto.aws.S3NotificationAgent::handleRequest"  --zip-file "fileb://./target/file_name.zip" | grep FunctionArn | cut -d'"' -f4)
获取当前AWS账号

AWS_ACCOUNT_NUMBER=$(aws sts get-caller-identity --output text --query 'Account') 
生成唯一语句id

STATEMENT_ID="$(date +%s%3N)-stm-name" 
授予S3 bucket调用lambda函数的权限

aws lambda add-permission --function-name "$FUNCTION_NAME"
--principal "s3.amazonaws.com" --statement-id "$STATEMENT_ID" --action "lambda:InvokeFunction" --source-arn "arn:aws:s3:::$S3_BUCKET"
--source-account "$AWS_ACCOUNT_NUMBER"
NOTIFICATION_CONFIGURATIONS='{"LambdaFunctionConfigurations":[{"Id":"'"$FUNCTION_NAME"'-event","LambdaFunctionArn":"'"$LAMBDA_ARN"'","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"suffix","Value":"log"},{"Name":"prefix","Value":"logtest"}]}}}]}'
aws s3api put-bucket-notification-configuration --bucket "$S3_BUCKET" 
--notification-configuration "$NOTIFICATION_CONFIGURATIONS"
为put bucket通知准备JSON参数(之间没有空格)

添加s3事件以调用Lambda函数

aws lambda add-permission --function-name "$FUNCTION_NAME"
--principal "s3.amazonaws.com" --statement-id "$STATEMENT_ID" --action "lambda:InvokeFunction" --source-arn "arn:aws:s3:::$S3_BUCKET"
--source-account "$AWS_ACCOUNT_NUMBER"
NOTIFICATION_CONFIGURATIONS='{"LambdaFunctionConfigurations":[{"Id":"'"$FUNCTION_NAME"'-event","LambdaFunctionArn":"'"$LAMBDA_ARN"'","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"suffix","Value":"log"},{"Name":"prefix","Value":"logtest"}]}}}]}'
aws s3api put-bucket-notification-configuration --bucket "$S3_BUCKET" 
--notification-configuration "$NOTIFICATION_CONFIGURATIONS"

我已通过以下步骤解决了问题:

在变量上分配函数名/s3桶名/rold ARN

FUNCTION_NAME=lambda-function-name
S3_BUCKE=s3_bucket_name
ROLE=AWS_role_ARN
创建Lambda函数并获取ARN

LAMBDA_ARN=$(aws lambda create-function --function-name "$FUNCTION_NAME" --runtime "java8"  --role "$ROLE" --handler "com.monsanto.aws.S3NotificationAgent::handleRequest"  --zip-file "fileb://./target/file_name.zip" | grep FunctionArn | cut -d'"' -f4)
获取当前AWS账号

AWS_ACCOUNT_NUMBER=$(aws sts get-caller-identity --output text --query 'Account') 
生成唯一语句id

STATEMENT_ID="$(date +%s%3N)-stm-name" 
授予S3 bucket调用lambda函数的权限

aws lambda add-permission --function-name "$FUNCTION_NAME"
--principal "s3.amazonaws.com" --statement-id "$STATEMENT_ID" --action "lambda:InvokeFunction" --source-arn "arn:aws:s3:::$S3_BUCKET"
--source-account "$AWS_ACCOUNT_NUMBER"
NOTIFICATION_CONFIGURATIONS='{"LambdaFunctionConfigurations":[{"Id":"'"$FUNCTION_NAME"'-event","LambdaFunctionArn":"'"$LAMBDA_ARN"'","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"suffix","Value":"log"},{"Name":"prefix","Value":"logtest"}]}}}]}'
aws s3api put-bucket-notification-configuration --bucket "$S3_BUCKET" 
--notification-configuration "$NOTIFICATION_CONFIGURATIONS"
为put bucket通知准备JSON参数(之间没有空格)

添加s3事件以调用Lambda函数

aws lambda add-permission --function-name "$FUNCTION_NAME"
--principal "s3.amazonaws.com" --statement-id "$STATEMENT_ID" --action "lambda:InvokeFunction" --source-arn "arn:aws:s3:::$S3_BUCKET"
--source-account "$AWS_ACCOUNT_NUMBER"
NOTIFICATION_CONFIGURATIONS='{"LambdaFunctionConfigurations":[{"Id":"'"$FUNCTION_NAME"'-event","LambdaFunctionArn":"'"$LAMBDA_ARN"'","Events":["s3:ObjectCreated:*"],"Filter":{"Key":{"FilterRules":[{"Name":"suffix","Value":"log"},{"Name":"prefix","Value":"logtest"}]}}}]}'
aws s3api put-bucket-notification-configuration --bucket "$S3_BUCKET" 
--notification-configuration "$NOTIFICATION_CONFIGURATIONS"