2017-10-07 15 views
0

J'essaie d'appeler une fonction Lambda AWS de manière RxJS:Comment appeler AWS Lambda avec Observable.bindNodeCallback (lambda.invoke)

invokeLambda(): Observable<string> { 
    const lambda = new AWS.Lambda({region: environment.region, apiVersion: "2015-03-31"}); 
    const invoke$ = (functionName, payload, invocationType = "RequestResponse") => { 
    return Observable.bindNodeCallback(lambda.invoke)(); 
}; 

return invoke$(environment.functionName, {}).map((result: InvocationResponse) => JSON.parse(<string>result.Payload)); 

}

mais lorsque je tente:

this.myService.invokeLambda().subscribe(() => { dosomething(); } 

Je reçois l'erreur:

page.html:5 ERROR TypeError: this.makeRequest is not a function 
at svc.(anonymous function) 

Qu'est-ce que je fais mal?

+2

semble contexte n'est pas lié, avez-vous essayé 'bindNodeCallback (lambda.invoke.bind (lambda)) ...'? –

+0

a travaillé comme un charme. Merci –

Répondre

0

Je recommande de créer une façade pour envoyer des demandes http à Amazon API Gateway qui est un proxy à AWS Lambda. Vous pouvez encapsuler le gestionnaire d'événements AWS SDK dans un observable. Dans l'exemple suivant, j'utilise rxjs pour rechercher un nom de pool d'identités et déterminer s'il existe dans une liste de pools d'identités Amazon Cognito Federated Identities dans mon compte AWS. J'ai intégré la fonction Lambda avec AWS Step Functions pour améliorer les capacités de canal "next" de rxjs. Rxjs, AWS Lambda et AWS Step Functions réessayent les définitions d'état de tâche, ce qui m'a permis de "continuer" la recherche d'existence du nom de pool d'identités Amazon Cognito Federated Identities via la pagination des noms cognitoidentity.listIdentityPools. Step Functions permet l'exécution récursive des fonctions lambda lors de l'utilisation de rxjs. En retournant le jeton de pagination renvoyé par la première exécution de la fonction lambda en tant que paramètre à l'exécution récursive suivante de la fonction lambda via les états de la fonction pas à pas. C'est à dire.

exports.handler = (event, context, callback) => { 

    AWS.config.region = "..."; 

    AWS.config.apiVersions = { 
    cognitoidentity: '2014-06-30' 
    }; 

    var cognitoidentity = new AWS.CognitoIdentity(); 

    var params = { 
    MaxResults: 1, 
    get NextToken() { 
     if (event.NextToken == null || undefined) { 
     return null; 
     } else { 
     return event.NextToken; 
     } 
    } 
    }; 

    var eventResult = { 
    identityPoolName: '', 
    identityPoolId: '', 
    NextToken: '', 
    NextState: '', 
    error: '', 
    errorReason: '' 
    }; 

function listIdentityPoolsObservable(params) { 
    return Rx.Observable.create(observer => { 

    cognitoidentity.listIdentityPools(params) 
     .on('success', function(response) { 
     observer.next(response.data); 
     }) 
     .on('error', function(error, response) { 
     observer.error(error); 
     }) 
     .on('complete', function(response) { 
     if (response.error) { 
      observer.error(response.error) 
     } else { 
      observer.complete(response); 
     } 
     }).send(); 
    }); 
    }; 

    const source$ = listIdentityPoolsObservable(params) 
    .share() 
    .observeOn(Rx.Scheduler.asap); 

    const identityPoolsSource$ = source$.map(x => { 
    return x.IdentityPools; 
    }) 
    .flatMap(x => { 
    return x; 
    }) 
    .filter(x => x.IdentityPoolName === event.identityPoolName) 
    .map(x => { 
    if (x.IdentityPoolName === event.identityPoolName) { 
     var dataArr = [x.IdentityPoolName, x.IdentityPoolId]; 
     return dataArr; 
    } 
    }) 
    .defaultIfEmpty(false); 

    const nextTokenSource$ = source$ 
    .filter(x => x.NextToken != null || undefined) 
    .map(x => { 
     if (x.NextToken != null || undefined) { 
     return x.NextToken; 
     } 
    }) 
    .defaultIfEmpty(false); 

    var identityAndToken = Rx.Observable 
    .forkJoin(identityPoolsSource$, nextTokenSource$) 
    .subscribe(x => { 
     //conditional statements... 
     callback(null, eventResult); 
    }); 

    function ExceptionExistence(eventResult) { 
    this.name = eventResult.errorName; 
    this.errorReason = eventResult.errorReason; 
    }; 
    ExceptionExistence.prototype = new Error(); 

};