Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Last active December 11, 2020 22:28
Show Gist options
  • Save patrickbrandt/0c8cfe8a93ee7bb38f11 to your computer and use it in GitHub Desktop.
Save patrickbrandt/0c8cfe8a93ee7bb38f11 to your computer and use it in GitHub Desktop.
Jenkins routines for AWS Lambda + API Gateway
# This is a "Managed Script" in Jenkins
COMMIT=`aws lambda get-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name $PUBLISH_FROM_ALIAS | grep "Description" | cut -d'"' -f4`
VERSION=`aws lambda publish-version --region $AWS_REGION --function-name $FUNCTION_NAME --description $COMMIT | grep "Version" | cut -d'"' -f4`
aws lambda update-alias --region $AWS_REGION --function-name $FUNCTION_NAME --function-version $VERSION --name $PUBLISH_TO_ALIAS --description $COMMIT
# This is a "Managed Script" in Jenkins
mkdir deploy
cp $WORKSPACE/$FUNCTION_ROOT/* deploy/
zip -rj deploy.zip deploy/*
aws lambda update-function-code --region $AWS_REGION --function-name $FUNCTION_NAME --zip-file fileb://deploy.zip --no-publish
aws lambda update-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name $FUNCTION_ALIAS --description $GIT_COMMIT
# This is a "Managed Script" in Jenkins
COMMIT=`aws lambda get-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name $PROMOTE_FROM_ALIAS | grep "Description" | cut -d'"' -f4`
VERSION=`aws lambda get-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name $PROMOTE_FROM_ALIAS | grep "FunctionVersion" | cut -d'"' -f4`
aws lambda update-alias --region $AWS_REGION --function-name $FUNCTION_NAME --function-version $VERSION --name $PROMOTE_TO_ALIAS --description $COMMIT
# exactly the same as 03_demo_QA_approve.sh, but with the following difference in env variables:
# PROMOTE_FROM_ALIAS=QA_Approved
# PROMOTE_TO_ALIAS=Staging
# exactly the same as 03_demo_QA_approve.sh, but with the following difference in env variables:
# PROMOTE_FROM_ALIAS=Staging
# PROMOTE_TO_ALIAS=Production
API_ID=$(aws apigateway get-rest-apis --region $AWS_REGION --query "items[?name=='$API_GATEWAY_NAME']" | grep id | cut -d'"' -f4)
# create stage variables
aws apigateway update-stage --region $AWS_REGION --rest-api-id $API_ID --stage-name dev --patch-operations op=replace,path=/variables/${LAMBDA_FUNCTION_NAME}_function_alias,value=$LAMBDA_FUNCTION_NAME:Dev
aws apigateway update-stage --region $AWS_REGION --rest-api-id $API_ID --stage-name test --patch-operations op=replace,path=/variables/${LAMBDA_FUNCTION_NAME}_function_alias,value=$LAMBDA_FUNCTION_NAME:Test
aws apigateway update-stage --region $AWS_REGION --rest-api-id $API_ID --stage-name stage --patch-operations op=replace,path=/variables/${LAMBDA_FUNCTION_NAME}_function_alias,value=$LAMBDA_FUNCTION_NAME:Staging
aws apigateway update-stage --region $AWS_REGION --rest-api-id $API_ID --stage-name prod --patch-operations op=replace,path=/variables/${LAMBDA_FUNCTION_NAME}_function_alias,value=$LAMBDA_FUNCTION_NAME:Production
# provide permissions for Api Gateway to invoke each function
aws lambda add-permission --region $AWS_REGION --function-name $LAMBDA_FUNCTION_NAME:Dev --principal apigateway.amazonaws.com --action lambda:InvokeFunction --statement-id `uuidgen`
aws lambda add-permission --region $AWS_REGION --function-name $LAMBDA_FUNCTION_NAME:Test --principal apigateway.amazonaws.com --action lambda:InvokeFunction --statement-id `uuidgen`
aws lambda add-permission --region $AWS_REGION --function-name $LAMBDA_FUNCTION_NAME:Staging --principal apigateway.amazonaws.com --action lambda:InvokeFunction --statement-id `uuidgen`
aws lambda add-permission --region $AWS_REGION --function-name $LAMBDA_FUNCTION_NAME:Production --principal apigateway.amazonaws.com --action lambda:InvokeFunction --statement-id `uuidgen`
aws apigateway create-rest-api --region $AWS_REGION --name $GATEWAY_NAME
API_ID=$(aws apigateway get-rest-apis --region $AWS_REGION --query "items[?name=='$GATEWAY_NAME']" | grep id | cut -d'"' -f4)
RESOURCE_ID=$(aws apigateway get-resources --region us-east-1 --rest-api-id $API_ID --query "items[?path=='/']" | grep id | cut -d'"' -f4)
aws apigateway put-method --region $AWS_REGION --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method OPTIONS --authorization-type NONE
aws apigateway put-integration --region $AWS_REGION --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method OPTIONS --type MOCK
aws apigateway create-deployment --region $AWS_REGION --rest-api-id $API_ID --stage-name 'dev'
aws apigateway create-deployment --region $AWS_REGION --rest-api-id $API_ID --stage-name 'test'
aws apigateway create-deployment --region $AWS_REGION --rest-api-id $API_ID --stage-name 'stage'
aws apigateway create-deployment --region $AWS_REGION --rest-api-id $API_ID --stage-name 'prod'
# NOTE: using code file as a work-around to an apparant error in the aws lambda creat-function cli
# TODO: test on RUNTIME to pick up the proper file
curl -O https://s3.amazonaws.com/default-lambda/default.py.zip
aws lambda create-function --region $AWS_REGION --function-name $FUNCTION_NAME --runtime $RUNTIME --role $ROLE --handler $HANDLER --timeout $TIMEOUT --memory-size $MEMORY_SIZE --zip-file fileb://default.py.zip --description "$DESCRIPTION"
aws lambda create-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name Dev --function-version '$LATEST'
aws lambda create-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name Test --function-version '$LATEST'
aws lambda create-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name QA_Approved --function-version '$LATEST'
aws lambda create-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name Staging --function-version '$LATEST'
aws lambda create-alias --region $AWS_REGION --function-name $FUNCTION_NAME --name Production --function-version '$LATEST'
@patrickbrandt
Copy link
Author

env variable examples for 01_demo_dev_deploy.sh:

FUNCTION_ROOT=lambda_functions/demo
AWS_REGION=us-east-1
FUNCTION_NAME=demo
FUNCTION_ALIAS=Dev

NOTE: this is stored as a managed script in Jenkins

@patrickbrandt
Copy link
Author

env variable examples for 02_demo_test_publish.sh:

AWS_REGION=us-east-1
FUNCTION_NAME=demo
PUBLISH_FROM_ALIAS=Dev
PUBLISH_TO_ALIAS=Test

@patrickbrandt
Copy link
Author

env variable examples for 03_demo_QA_approve.sh:

AWS_REGION=us-east-1
FUNCTION_NAME=demo
PROMOTE_FROM_ALIAS=Test
PROMOTE_TO_ALIAS=QA_Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment