Skip to content

Instantly share code, notes, and snippets.

@abhigets
Last active September 11, 2019 14:44
Show Gist options
  • Save abhigets/4b05634bef3600c265646d2e10d235e7 to your computer and use it in GitHub Desktop.
Save abhigets/4b05634bef3600c265646d2e10d235e7 to your computer and use it in GitHub Desktop.
#!/bin/bash
#install jq as pre-requisite
#installation guide https://stedolan.github.io/jq/download/
#brew install jq
apk add jq
#get the policy from aws and store it in variable
QUERY='items[?name==`your api gateway name`]'
GATEWAY_ID=$(aws apigateway get-rest-apis --query ${QUERY} | jq -r '.[0].id')
OLD_POLICY=$(aws apigateway get-rest-api --rest-api-id ${GATEWAY_ID})
OLD_POLICY=$(jq -n "$OLD_POLICY" | jq '.policy')
echo $OLD_POLICY
echo "---------------"
#get the ip of my host
MY_IP=$(curl ipecho.net/plain;)
echo $MY_IP
echo "---------------"
#populate new policy by adding my ip
NEW_POLICY=$(echo $OLD_POLICY | sed 's;]}}}]};,\\\\\\"'$MY_IP'\\\\\\\"]}}}]};g')
echo $NEW_POLICY
echo "---------------"
POLICY_STRING="op=replace,path=/policy,value=${NEW_POLICY},from=string"
echo $POLICY_STRING
aws apigateway update-rest-api --rest-api-id nzpg22s1z0 --patch-operations ${POLICY_STRING}
#deploy api gateway to reflect the policy changes
aws apigateway create-deployment --rest-api-id nzpg22s1z0 --stage-name dev
#if updated and restore of different file then one can write the old policy to file and read it back in restore script
#echo $OLD_POLICY > "OLD_POLICY.json"
#run your test or task
#for me it was run integration test i.e
#npm run integ-test
#restoring the old poliocy
#if old policy was stored in file then retive it and delete it
#API_GATEWAY_OLD_POLICY=$(<$"OLD_POLICY.json")
#rm "OLD_POLICY.json"
POLICY_STRING="op=replace,path=/policy,value=${OLD_POLICY},from=string"
aws apigateway update-rest-api --rest-api-id nzpg22s1z0 --patch-operations ${POLICY_STRING}
#deploy api gateway to reflect the policy changes
aws apigateway create-deployment --rest-api-id nzpg22s1z0 --stage-name dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment