2017-11-10 17 views
0

S3バケットを作成してすぐにラムダ通知イベントを割り当てようとしています。S3バケットラムダイベント:次の宛先設定を検証できません

ここに私が書いたノード・テスト・スクリプトです:

const aws = require('aws-sdk'); 
const uuidv4 = require('uuid/v4'); 

aws.config.update({ 
    accessKeyId: 'key', 
    secretAccessKey:'secret', 
    region: 'us-west-1' 
}); 

const s3 = new aws.S3(); 

const params = { 
    Bucket: `bucket-${uuidv4()}`, 
    ACL: "private", 
    CreateBucketConfiguration: { 
    LocationConstraint: 'us-west-1' 
    } 
}; 

s3.createBucket(params, function (err, data) { 
    if (err) { 
    throw err; 
    } else { 
    const bucketUrl = data.Location; 

    const bucketNameRegex = /bucket-[a-z0-9\-]+/; 
    const bucketName = bucketNameRegex.exec(bucketUrl)[0]; 

    const params = { 
     Bucket: bucketName, 
     NotificationConfiguration: { 
     LambdaFunctionConfigurations: [ 
      { 
      Id: `lambda-upload-notification-${bucketName}`, 
      LambdaFunctionArn: 'arn:aws:lambda:us-west-1:xxxxxxxxxx:function:respondS3Upload', 
      Events: ['s3:ObjectCreated:CompleteMultipartUpload'] 
      }, 
     ] 
     } 
    }; 

    // Throws "Unable to validate the following destination configurations" until an event is manually added and deleted from the bucket in the AWS UI Console 
    s3.putBucketNotificationConfiguration(params, function(err, data) { 
     if (err) { 
     console.error(err); 
     console.error(this.httpResponse.body.toString()); 
     } else { 
     console.log(data); 
     } 
    }); 
    } 
}); 

を作成は正常に動作しますが、aws-sdkからs3.putBucketNotificationConfigurationを呼び出すと、スロー:

{ InvalidArgument: Unable to validate the following destination configurations 
    at Request.extractError ([...]/node_modules/aws-sdk/lib/services/s3.js:577:35) 
    at Request.callListeners ([...]/node_modules/aws-sdk/lib/sequential_executor.js:105:20) 
    at Request.emit ([...]/node_modules/aws-sdk/lib/sequential_executor.js:77:10) 
    at Request.emit ([...]/node_modules/aws-sdk/lib/request.js:683:14) 
    at Request.transition ([...]/node_modules/aws-sdk/lib/request.js:22:10) 
    at AcceptorStateMachine.runTo ([...]/node_modules/aws-sdk/lib/state_machine.js:14:12) 
    at [...]/node_modules/aws-sdk/lib/state_machine.js:26:10 
    at Request.<anonymous> ([...]/node_modules/aws-sdk/lib/request.js:38:9) 
    at Request.<anonymous> ([...]/node_modules/aws-sdk/lib/request.js:685:12) 
    at Request.callListeners ([...]/node_modules/aws-sdk/lib/sequential_executor.js:115:18) 
    message: 'Unable to validate the following destination configurations', 
    code: 'InvalidArgument', 
    region: null, 
    time: 2017-11-10T02:55:43.004Z, 
    requestId: '9E1CB35811ED5828', 
    extendedRequestId: 'tWcmPfrAu3As74M/0sJL5uv+pLmaD4oBJXwjzlcoOBsTBh99iRAtzAloSY/LzinSQYmj46cwyfQ=', 
    cfId: undefined, 
    statusCode: 400, 
    retryable: false, 
    retryDelay: 4.3270874729153475 } 

<?xml version="1.0" encoding="UTF-8"?> 
<Error> 
    <Code>InvalidArgument</Code> 
    <Message>Unable to validate the following destination configurations</Message> 
    <ArgumentName1>arn:aws:lambda:us-west-1:xxxxxxxxxx:function:respondS3Upload, null</ArgumentName1> 
    <ArgumentValue1>Not authorized to invoke function [arn:aws:lambda:us-west-1:xxxxxxxxxx:function:respondS3Upload]</ArgumentValue1> 
    <RequestId>9E1CB35811ED5828</RequestId> 
    <HostId>tWcmPfrAu3As74M/0sJL5uv+pLmaD4oBJXwjzlcoOBsTBh99iRAtzAloSY/LzinSQYmj46cwyfQ=</HostId> 
</Error> 

私はに割り当てられた役割とそれを実行しましたが、ラムダは、私が思うものは、それが必要とするすべてのポリシーです。私は何かが欠けているかもしれない。私はこのスクリプトを実行するために私のルートアクセスキーを使用しています。

Role

私はそれはS3がイベントを追加する前にバケットを作成するための時間を必要とするタイミングエラーかもしれないと思ってきましたが、私はしばらく待ってました、バケット名をハードコーディングし、再び私のスクリプトを実行します。同じエラーがスローされます。

S3 UIでイベントフックを作成してすぐに削除すると、そのバケット名をハードコードするとスクリプトが機能してしまうのです。 UIでイベントを作成すると必要な権限が追加されるようですが、SDKやコンソールのUIに何が含まれているのか分かりません。しようとする

S3 Event Config

任意の考えや物事?あなたの助けをありがとう

答えて

2

s3バケットにラムダ機能を呼び出す権限がないため、このメッセージが表示されています。

AWS documentationに従って!必要な権限の2種類があります。

  1. 権限Amazon S3のは、あなたのラムダ関数を呼び出すようにするために、あなたのラムダ関数はサービス
  2. 権限を呼び出すようにするためにあなたがAWS」タイプのオブジェクトを作成する必要があります

: :Lambda :: Permission 'これは次のようになります。

{ 
    "Version": "2012-10-17", 
    "Id": "default", 
    "Statement": [ 
    { 
     "Sid": "<optional>", 
     "Effect": "Allow", 
     "Principal": { 
     "Service": "s3.amazonaws.com" 
     }, 
     "Action": "lambda:InvokeFunction", 
     "Resource": "<ArnToYourFunction>", 
     "Condition": { 
     "StringEquals": { 
      "AWS:SourceAccount": "<YourAccountId>" 
     }, 
     "ArnLike": { 
      "AWS:SourceArn": "arn:aws:s3:::<YourBucketName>" 
     } 
     } 
    } 
    ] 
} 
関連する問題