Remove aws-sdk dependency from AWS Lambda node

This commit is contained in:
Matheus Cansian
2019-10-05 17:17:23 -03:00
parent 822e12ff35
commit 39c0b08d9d
3 changed files with 78 additions and 16 deletions

View File

@@ -6,7 +6,7 @@ import {
import { config } from 'aws-sdk';
import { OptionsWithUri } from 'request';
import { sign } from 'aws4';
export async function awsConfigCredentials(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions): Promise<void> {
const credentials = this.getCredentials('aws');
@@ -21,3 +21,48 @@ export async function awsConfigCredentials(this: IHookFunctions | IExecuteFuncti
secretAccessKey: `${credentials.secretAccessKey}`,
});
}
export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = `${service}.${credentials.region}.amazonaws.com`
// Sign AWS API request with the user credentials
const signOpts = {headers: headers || {}, host: endpoint, method: method, path: path, body: body}
sign(signOpts, {accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`})
const options: OptionsWithUri = {
headers: signOpts.headers,
method: method,
uri: `https://${endpoint}${signOpts.path}`,
body: signOpts.body,
};
let response: string
try {
response = await this.helpers.request!(options);
} catch (error) {
console.error(error);
const errorMessage = error.response.body.message || error.response.body.Message;
if (error.statusCode === 403) {
if (errorMessage == 'The security token included in the request is invalid.') {
throw new Error('The AWS credentials are not valid!');
} else if (errorMessage.startsWith('The request signature we calculated does not match the signature you provided')) {
throw new Error('The AWS credentials are not valid!');
}
}
throw errorMessage;
}
try {
return JSON.parse(response);
} catch (e) {
return response
}
}