0

私はLambda to Firebaseメッセージを使用します。私は参照してください​​。しかし、ラムダ機能はまだGoogleサーバーに接続できないためタイムアウトしています。AWS Lambdaをfirebase-admin initializeAppタイムアウトを使用して

Handler.js

/ [START imports] 
const firebase = require('firebase-admin'); 
const serviceAccount = require("../serviceAccount.json"); 

module.exports.message = (event, context, callback) => { 
    context.callbackWaitsForEmptyEventLoop = false; 
    const registrationToken = "xxxxxxx"; 

    const payload = { 
    data: { 
     score: "850", 
     time: "2:45" 
    } 
    }; 

    // [START initialize] 
    if(firebase.apps.length == 0) { // <---Important!!! In lambda, it will cause double initialization. 
    firebase.initializeApp({ 
     credential: firebase.credential.cert(serviceAccount), 
     databaseURL: 'https://messaging-xxxxx.firebaseio.com' 
    }); 
    } 

    // Send a message to the device corresponding to the provided 
    // registration token. 
    firebase.messaging().sendToDevice(registrationToken, payload) 
    .then(function(response) { 
     // See the MessagingDevicesResponse reference documentation for 
     // the contents of response. 
     console.log("Successfully sent message:", response); 
     callback(null, { 
     statusCode: 200, 
     body: JSON.stringify("Successful!"), 
     }); 
    }) 
    .catch(function(error) { 
     console.log("Error sending message:", error); 
     callback(null, { 
     statusCode: 500, 
     body: JSON.stringify({ 
      "status": "error", 
      "message": error 
     }) 
     }) 
    }); 
}; 

CloudWatchの

[Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "connect ETIMEDOUT 172.217.26.45:443".]

しかし、私は私のEC2と仕事検索で実行するために同じserviceAccount.jsonを使用しています。 誰かがこれに遭遇しますか?

+0

'serviceAccount.json'ファイルはどのように追加しましたか?私はあなたがLambdaにzipをアップロードしたと仮定し、それは単なるインラインコードではない? – Deif

+1

このスレッドは役に立ちましたか? http://stackoverflow.com/questions/36508974/python-request-in-aws-lambda-timing-out – jwngr

+0

@Deif serverlessを使用してserviceAccount.jsonファイルをアップロードします。 – Jim

答えて

2

数時間の苦労の末、私はついにその理由を見つけました。 私のラムダは、VPCを使ってRDSとVPCのネットワークインターフェイスを接続するためにプライベートIPしか持っていないためです。

AWS document

When you add VPC configuration to a Lambda function, it can only access resources in that VPC. If a Lambda function needs to access both VPC resources and the public Internet, the VPC needs to have a Network Address Translation (NAT) instance inside the VPC.

だから私はVPC内でNATを作成する必要があります。 私はこれに続き、Blogと問題を解決しました。

関連する問題