Skip to content

Instantly share code, notes, and snippets.

@artburkart
Last active August 30, 2018 02:36
Show Gist options
  • Save artburkart/508424db27b886a14fea577b13a73ed8 to your computer and use it in GitHub Desktop.
Save artburkart/508424db27b886a14fea577b13a73ed8 to your computer and use it in GitHub Desktop.
testing_localstack_apigateway_lambda

Usage

cd localstack
SERVICES=lambda,apigateway docker-compose up  # in localstack repo folder
cd -
git clone https://gist.github.com/artburkart/508424db27b886a14fea577b13a73ed8 test-apigateway
cd test-apigateway
bash test.sh
#!/bin/bash
# Cleans lambda functions
LAMBDA='AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar aws --endpoint-url=http://localhost:4574 lambda'
eval "$LAMBDA list-functions" | jq '.Functions[0].FunctionName' | xargs -I{} bash -c "eval $LAMBDA delete-function --function-name {}"
# Cleans api gateway rest apis
GATEWAY='AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar aws --endpoint-url=http://localhost:4567 apigateway'
eval "$GATEWAY get-rest-apis" | jq '.items[].id' | xargs -I{} bash -c "$GATEWAY delete-rest-api --rest-api-id {}"
{
"operation": "echo",
"payload": {
"somekey1": "somevalue1",
"somekey2": "somevalue2"
}
}
console.log('Loading function');
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient();
/**
* Provide an event that contains the following keys:
*
* - operation: one of the operations in the switch statement below
* - tableName: required for operations that interact with DynamoDB
* - payload: a parameter to pass to the operation being performed
*/
exports.handler = function(event, context, callback) {
//console.log('Received event:', JSON.stringify(event, null, 2));
var operation = event.operation;
if (event.tableName) {
event.payload.TableName = event.tableName;
}
switch (operation) {
case 'create':
dynamo.put(event.payload, callback);
break;
case 'read':
dynamo.get(event.payload, callback);
break;
case 'update':
dynamo.update(event.payload, callback);
break;
case 'delete':
dynamo.delete(event.payload, callback);
break;
case 'list':
dynamo.scan(event.payload, callback);
break;
case 'echo':
callback(null, "Success");
break;
case 'ping':
callback(null, "pong");
break;
default:
callback('Unknown operation: ${operation}');
}
};
#!/bin/bash
LAMBDA='AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar aws --endpoint-url=http://localhost:4574 lambda'
GATEWAY='AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar aws --endpoint-url=http://localhost:4567 apigateway'
# Zip lambda function
if [ ! -f ./LambdaFunctionOverHttps.zip ]; then
zip LambdaFunctionOverHttps.zip LambdaFunctionOverHttps.js
fi
# Create function
eval $LAMBDA create-function --region us-east-1 --function-name LambdaFunctionOverHttps --zip-file 'fileb://LambdaFunctionOverHttps.zip' --handler LambdaFunctionOverHttps.handler --runtime nodejs6.10 --role foobar
# Get Lambda ARN
LAMBDA_ARN=`eval "$LAMBDA get-function --function-name LambdaFunctionOverHttps" | jq '.Configuration.FunctionArn' -r`
# Create gateway
API_ID=`eval "$GATEWAY create-rest-api --name DynamoDBOperations --region us-east-1" | jq '.id' -r`
# Get resource id
PARENT_ID=`eval "$GATEWAY get-resources --rest-api-id $API_ID" | jq '.items[0].id' -r`
# Create resource?
RESOURCE_ID=`eval "$GATEWAY create-resource --rest-api-id $API_ID --parent-id $PARENT_ID --path-part DynamoDBManager" | jq '.id'`
# Add a method to the new resource?
eval $GATEWAY put-method --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST --authorization-type NONE
# ???
eval $GATEWAY put-integration --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST --type AWS --integration-http-method POST --uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/$LAMBDA_ARN/invocations
# ???
eval "$GATEWAY put-method-response --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST --status-code 200 --response-models '{\"application/json\": \"Empty\"}'"
# ???
eval "$GATEWAY put-integration-response --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST --status-code 200 --response-templates '{\"application/json\": \"\"}'"
# Create deployment of API
eval $GATEWAY create-deployment --rest-api-id $API_ID --stage-name prod
# Attempt to invoke lambda through gateway
eval "$GATEWAY test-invoke-method --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST --path-with-query-string '' --body '{\"operation\": \"echo\", \"payload\": {\"somekey1\": \"somevalue1\", \"somekey2\": \"somevalue2\"}}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment