From 34ed7358c5df0c2242525d9c7568063315d50b01 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sun, 20 Sep 2020 23:33:55 -0400 Subject: [PATCH] Port tooling and increase durability --- Tooling/EndpointSetup.sh | 63 ++++++++++++++------- Tooling/aws_method_request_passthrough.json | 7 --- Tooling/body_and_auth_mapping.json | 3 + 3 files changed, 47 insertions(+), 26 deletions(-) delete mode 100644 Tooling/aws_method_request_passthrough.json create mode 100644 Tooling/body_and_auth_mapping.json diff --git a/Tooling/EndpointSetup.sh b/Tooling/EndpointSetup.sh index 4588a3f..81c2d08 100644 --- a/Tooling/EndpointSetup.sh +++ b/Tooling/EndpointSetup.sh @@ -1,7 +1,8 @@ +#!/bin/bash #Base script from: https://github.com/NMerz/DoctorsNote/blob/master/AWS%20Setup/Lambda-GatewayInitialization.sh #NOTE: This has been tested and works; however, the apigateway does not properly show as a trigger in AWS's web UI -#NOTE2: This assumes that the root Gateway and Lambda role have been set up previously (one-time setup) and their values are store in the constants below +#NOTE2: This assumes that the root Gateway and Lambda role have been set up previously (one-time setup) and their values are stored in the constants below #constants APIID=datoh7woc9 #rest-api-id is tied to the apigateway while resource-id seems tied to the specific url extension @@ -12,31 +13,55 @@ DEPLOYSTAGE=Development DEBUGFILE=/dev/null -echo -n "Please enter function name: " +echo -n "Please enter function/endpoint name: " read functionName -echo -n "Please enter path to zip of function code: " -read functionPath -echo -n "Please enter url extension: " -read partName +echo -n "Please enter method(GET, POST, etc.): " +read method -LAMBDAARN=$(aws lambda create-function --function-name ${functionName} --zip-file fileb://${functionPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}.lambda_handler | head -n 3 | tail -n 1 | cut -d \" -f 4) +jarPath=$(find .. -name "${functionName}.jar") +if [[ "$jarPath" == "" ]]; then + echo "Unable to find file ${functionName}.jar" >&2 + exit 1 +fi +functionPath=${jarPath%/${functionName}.jar} +zipPath=${functionPath}.zip + +zip ${zipPath} ${jarPath} + +RAWLAMBDA=$(aws lambda create-function --function-name ${functionName}${method} --zip-file fileb://${zipPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}.lambda_handler) + +if [[ $? -ne 0 ]]; then + echo "Unable to create Lamba" >&2 + exit 1 +fi + +LAMBDAARN=$(echo $RAWLAMBDA | head -n 3 | tail -n 1 | cut -d \" -f 8) echo ${LAMBDAARN} > ${DEBUGFILE} -RESOURCEID=$(aws apigateway create-resource --rest-api-id ${APIID} --parent-id ${ROOTRESOURCEID} --path-part ${partName} | head -n 2 | tail -n 1 | cut -d \" -f 4) +RAWRESOURCEID=$(aws apigateway create-resource --rest-api-id ${APIID} --parent-id ${ROOTRESOURCEID} --path-part ${functionName}) + +if [[ $? -ne 0 ]]; then + echo "Unable to create Resource. This needs to be handled at some future point" >&2 + exit 1 +fi + +RESOURCEID=$(echo ${RAWRESOURCEID} | head -n 2 | tail -n 1 | cut -d \" -f 4) echo ${RESOURCEID} > ${DEBUGFILE} -aws apigateway put-method --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method POST --authorization-type COGNITO_USER_POOLS --authorizer-id awt4cs --api-key-required > ${DEBUGFILE} - -aws apigateway put-integration --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method POST --type AWS --integration-http-method POST --uri arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/${LAMBDAARN}/invocations > ${DEBUGFILE} - -aws lambda add-permission --function-name ${functionName} --statement-id ${functionName}API --action lambda:InvokeFunction --principal apigateway.amazonaws.com > ${DEBUGFILE} - -aws apigateway put-method-response --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method POST --status-code 200 > ${DEBUGFILE} - -aws apigateway put-integration-response --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method POST --status-code 200 --selection-pattern "" > ${DEBUGFILE} - -aws apigateway create-deployment --rest-api-id ${APIID} --stage-name ${DEPLOYSTAGE} --description "Deployment by creation script for function ${functionName}" > ${DEBUGFILE} +aws apigateway put-method --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method ${method} --authorization-type COGNITO_USER_POOLS --authorizer-id awt4cs --api-key-required > ${DEBUGFILE} + + + +aws apigateway put-integration --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method ${method} --type AWS --integration-http-method POST --uri arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/${LAMBDAARN}/invocations --request-templates 'file://body_and_auth_mapping.json' > ${DEBUGFILE} + +aws lambda add-permission --function-name ${functionName}${method} --statement-id ${functionName}API --action lambda:InvokeFunction --principal apigateway.amazonaws.com > ${DEBUGFILE} + +aws apigateway put-method-response --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method ${method} --status-code 200 > ${DEBUGFILE} + +aws apigateway put-integration-response --rest-api-id ${APIID} --resource-id ${RESOURCEID} --http-method ${method} --status-code 200 --selection-pattern "" > ${DEBUGFILE} + +aws apigateway create-deployment --rest-api-id ${APIID} --stage-name ${DEPLOYSTAGE} --description "Deployment by creation script for function ${functionName}${method}" > ${DEBUGFILE} diff --git a/Tooling/aws_method_request_passthrough.json b/Tooling/aws_method_request_passthrough.json deleted file mode 100644 index efea7ce..0000000 --- a/Tooling/aws_method_request_passthrough.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "body": $input.json('$'), - "context" : { - "sub" : "$context.authorizer.claims.sub", - "email" : "$context.authorizer.claims.email" - } -} \ No newline at end of file diff --git a/Tooling/body_and_auth_mapping.json b/Tooling/body_and_auth_mapping.json new file mode 100644 index 0000000..600bc4b --- /dev/null +++ b/Tooling/body_and_auth_mapping.json @@ -0,0 +1,3 @@ +{ + "application/json": "{\"body\": \"$input.json('$')\",\"context\" : {\"sub\" : \"$context.authorizer.claims.sub\",\"email\" : \"$context.authorizer.claims.email\"}}" +} \ No newline at end of file