Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cyxou/e237f8eafae225e9188263d003405d91 to your computer and use it in GitHub Desktop.
Save cyxou/e237f8eafae225e9188263d003405d91 to your computer and use it in GitHub Desktop.
package.json
{
"name": "my-lambda-function",
"config": {
"funcName": "bot-gatecrasher-gen_person_process_instruction",
"zip": "../../../build/dist/gen_person_process_instruction.zip",
"restapiid": "your-rest-api-id"
},
"scripts": {
"//1": "---------------------- UTILITIES -----------------------------------------",
"dumpconf": "env | grep npm_ | sort | uniq",
"confirm": "printf '\\033[0;31mAre you sure you wanna do this (y/n)? \\033[0m'; read REPLY; if [ \"$REPLY\" != \"y\" ]; then exit 1; fi;",
"//2": "---------------------- CREATE FUNCTION -----------------------------------",
"precreate": "npm run build -s",
"create": "aws lambda create-function --function-name $npm_package_config_funcName --runtime nodejs4.3 --role arn:aws:iam::XXXXXXXXXXXX:role/lambda_basic_execution --handler index.handler --description \"$npm_package_description\" --timeout 10 --memory-size 128 --publish --zip-file fileb://$npm_package_config_zip",
"postcreate": "aws lambda create-alias --function-name $npm_package_config_funcName --name dev --function-version '$LATEST' && aws lambda create-alias --function-name $npm_package_config_funcName --name prod --function-version '$LATEST'",
"//3": "---------------------- BUILD FUNCTION ------------------------------------",
"clear": "rm -f $npm_package_config_zip",
"prebuild": "npm run clear -s",
"build": "zip -r $npm_package_config_zip node_modules lib index.js -x '*.un~' '*.swp'",
"//4": "---------------------- DEPLOY TO *DEV* ENVIRONMENT -----------------------",
"deploy:dev": "npm run build -s && aws lambda update-function-code --function-name $npm_package_config_funcName --zip-file fileb://$npm_package_config_zip",
"//5": "---------------------- DEPLOY TO *PROD* ENVIRONMENT ----------------------",
"publish": "npm run build -s && aws lambda update-function-code --function-name $npm_package_config_funcName --zip-file fileb://$npm_package_config_zip --publish",
"update:alias:prod": "aws lambda update-alias --function-name $npm_package_config_funcName --name prod --function-version $(aws lambda list-versions-by-function --function-name $npm_package_config_funcName | node -pe \"var o = JSON.parse(require('fs').readFileSync('/dev/stdin').toString());o.Versions[o.Versions.length-1].Version\")",
"deploy:prod": "npm run confirm -s && npm run publish -s && npm run update:alias:prod",
"//6": "---------------------- CREATE API GATEWAY RESOURCE -----------------------",
"delete:apig:resource": "if [ -n \"$npm_config_resourceId\" ]; then aws apigateway delete-resource --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId; else echo 'ResourceId is not set in .npmrc'; fi",
"precreate:apig:resource": "npm run delete:apig:resource -s",
"create:apig:resource": "aws apigateway create-resource --rest-api-id $npm_package_config_restapiid --parent-id wrjy8z --path-part $npm_package_name | node -pe \"var o = JSON.parse(require('fs').readFileSync('/dev/stdin').toString());o.id\" | xargs -0 printf 'resourceId = %s' > .npmrc",
"//7": "---------------------- SETUP API GATEWAY METHOD --------------------------",
"create:apig:method": "aws apigateway put-method --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --authorization-type none",
"create:apig:methodResponses": "aws apigateway put-method-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 200 --response-models '{\"application/json\":\"Empty\"}' && aws apigateway put-method-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 400 && aws apigateway put-method-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 403 && aws apigateway put-method-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 500",
"create:apig:integration": "aws apigateway put-integration --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --type AWS --integration-http-method POST --uri \"arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:function:$npm_package_config_funcName:\\${stageVariables.ENV}/invocations\"",
"create:apig:integrationResponses": "aws apigateway put-integration-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 200 --selection-pattern '' && aws apigateway put-integration-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 400 --selection-pattern 'Bad request.*' && aws apigateway put-integration-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 403 --selection-pattern 'Forbidden.*' && aws apigateway put-integration-response --rest-api-id $npm_package_config_restapiid --resource-id $npm_config_resourceId --http-method POST --status-code 500 --selection-pattern 'Error.*'",
"add:permission:dev": "aws lambda add-permission --function-name arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:function:$npm_package_config_funcName:dev --source-arn arn:aws:execute-api:eu-west-1:XXXXXXXXXXXX:$npm_package_config_restapiid/*/POST/api/gatecrasher/$npm_package_name --principal apigateway.amazonaws.com --statement-id $(cat /proc/sys/kernel/random/uuid) --action lambda:InvokeFunction",
"add:permission:prod": "aws lambda add-permission --function-name arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:function:$npm_package_config_funcName:prod --source-arn arn:aws:execute-api:eu-west-1:XXXXXXXXXXXX:$npm_package_config_restapiid/*/POST/api/gatecrasher/$npm_package_name --principal apigateway.amazonaws.com --statement-id $(cat /proc/sys/kernel/random/uuid) --action lambda:InvokeFunction",
"add:permissions": "npm run add:permission:dev -s && npm run add:permission:prod -s",
"create:apigateway": "npm run create:apig:resource -s && npm run create:apig:method -s && npm run create:apig:methodResponses -s && npm run create:apig:integration -s && npm run create:apig:integrationResponses -s && npm run add:permissions -s"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment