Created
June 19, 2021 12:51
-
-
Save rgpower/56dc4c2c48d1a62ad6cd563aaebbfb4b to your computer and use it in GitHub Desktop.
AWS-SDK for js exposed as a REST service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Requires Lambda Proxy Integration | |
| * | |
| * Example usage | |
| * | |
| * curl -s https://SomeGatewayID.execute-api.us-east-1.amazonaws.com/test/aws/DynamoDB.DocumentClient/default/query -X PUT -H 'X-api-key:TheKey' -d '{"TableName":"SomeTable","KeyConditionExpression":"id = :id","ExpressionAttributeValues":{":id":"SomeID"}}' -H 'Content-Type: application/json' | jq . | |
| * curl -s https://SomeGatewayID.execute-api.us-east-1.amazonaws.com/test/aws/SimpleDB/2009-04-15/listDomains -X PUT -H 'X-api-key:TheKey' | jq . | |
| */ | |
| const API_KEY = '' + process.env['API_KEY'] | |
| const AWS = require('aws-sdk') | |
| const MESSAGES = { | |
| ENV_API_KEY_MISSING: 'Internal Error: env API_KEY is missing', | |
| CLI_API_KEY_MISSING: 'Bad Request: X-Api-Key header is MANDATORY', | |
| CLI_API_KEY_INVALID: 'Please UPDATE your client X-Api-Key', | |
| SRV_INVALID_SERVICE_METHOD: 'Invalid AWS service method', | |
| SRV_INVALID_SERVICE: 'Invalid AWS service', | |
| SRV_INSTANTIATION_ERROR: 'Error creating instance', | |
| SRV_UNEXPECTED_ERROR: 'Unexpected Error' | |
| } | |
| function _r (statusCode, headers, body) { | |
| return { | |
| statusCode: '' + statusCode, | |
| headers: headers, | |
| body: JSON.stringify(body) | |
| } | |
| } | |
| exports.handler = (event, context, callback) => { | |
| if (!API_KEY) { | |
| callback(null, _r(500, null, MESSAGES.ENV_API_KEY_MISSING)) | |
| } | |
| let headers = /** @type {Object.<string,?>} */ event.headers | |
| let params = /** @type {Object.<string,?>} */ event.pathParameters | |
| let body = /** @type {Object.<string,?>} */ event.body | |
| /* Note that X-api-key etc will get automatically | |
| lowercased by execution environment | |
| */ | |
| let clientApiKey = headers['x-api-key'] | |
| if (!clientApiKey) { | |
| callback(null, _r(400, null, MESSAGES.CLI_API_KEY_MISSING)) | |
| } else { | |
| if (API_KEY === clientApiKey) { | |
| let version = params['version'] | |
| let service = params['service'] | |
| let method = params['method'] | |
| let svcParts = service.split('.') | |
| try { | |
| let NS = AWS | |
| while (svcParts.length) { | |
| let part = svcParts.shift() | |
| NS = NS[part] | |
| if (!NS) { | |
| let errMsg = { message: MESSAGES.SRV_INVALID_SERVICE + `: [${part}]` } | |
| callback(null, _r(400, null, errMsg)) | |
| return | |
| } | |
| } | |
| let options = version === 'default' ? undefined : { apiVersion: version } | |
| let svc | |
| svc = new NS(options) | |
| if (typeof svc[method] !== 'function') { | |
| throw new Error(`method ${method} is undefined on ${service}`) | |
| } | |
| svc[method](JSON.parse(body)).promise() | |
| .then(function (response) { | |
| let httpResp = response.$response.httpResponse | |
| callback(null, _r(httpResp.statusCode, null, { | |
| statusCode: httpResp.statusCode, | |
| body: response, | |
| headers: httpResp.headers | |
| })) | |
| }) | |
| .catch(function (awsErr) { | |
| let errMsg = { message: awsErr.message, code: awsErr.code, errors: awsErr.errors } | |
| callback(null, _r(awsErr.statusCode || 500, null, errMsg)) | |
| }) | |
| } catch (err) { | |
| let errMsg = {message: MESSAGES.SRV_UNEXPECTED_ERROR + ': ' + err.message} | |
| callback(null, _r(500, null, errMsg)) | |
| } | |
| } else { | |
| let errMsg = {message: MESSAGES.CLI_API_KEY_INVALID} | |
| callback(null, _r(401, null, errMsg)) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment