Skip to content

Instantly share code, notes, and snippets.

@gambtho
Created January 29, 2018 17:31
Show Gist options
  • Save gambtho/e94abe69ff38c0c473ab97ff49487945 to your computer and use it in GitHub Desktop.
Save gambtho/e94abe69ff38c0c473ab97ff49487945 to your computer and use it in GitHub Desktop.
lambda gateway fun
.DEFAULT_GOAL := help
name="something here"
region=us-east-1
profile="something here"
path="something here"
help:
cat ./Makefile
build:
GOOS=linux go build -o main main.go
package:
zip deployment.zip main
test:
go test
update-function:
aws lambda update-function-code \
--region ${region} --function-name ${name} \
--zip-file fileb://./deployment.zip \
--profile ${profile}
get-url:
$(eval api-id := $(shell make get-api-id | tail -n 1))
echo "https://${api-id}.execute-api.${region}.amazonaws.com/prod/${path}"
curl-url:
$(eval url := $(shell make get-url | tail -n 1))
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2" ${url}
deploy:
make build && make package && make update-function
## Below is just for manual setup -- terraform or cloudformation is preferred
setup:
make create-function && \
make create-gateway && \
make create-api-resource && \
make create-api-method && \
make create-api-integration && \
make create-method-response && \
make create-integration-response && \
make create-api-permissions && \
make get-url
get-role:
aws iam get-role --role-name "${name}-role" --query Role.Arn --output text --profile ${profile}
create-function:
aws lambda create-function \
--region "${region}" --function-name "${name}" \
--zip-file fileb://./deployment.zip \
--role "${role}" \
--environment Variables={API_KEY="${API_KEY}"} \
--handler main --runtime go1.x --profile "${profile}"
create-gateway:
aws apigateway create-rest-api --name "api-${name}" --description "api for ${name}" --region ${region} --profile "${profile}"
create-role:
aws iam create-role --role-name "${name}-role" --assume-role-policy-document file://role.json --profile ${profile}
create-policy:
aws iam put-role-policy --role-name "${name}-role" --policy-name "${name}-policy" --policy-document file://basiciamrole.json --profile ${profile}
get-lambda-arn:
aws lambda list-functions --region "${region}" --profile "${profile}" | jq -cr '[.Functions[] | select( .FunctionName==("${name}")) ] | .[0].FunctionArn '
get-api-id:
aws apigateway get-rest-apis --region ${region} --profile "${profile}" | jq -cr '[ .items[] | select( .name | contains("golambda")) ] | .[0].id '
get-parent-id:
$(eval api-id := $(shell make get-api-id | tail -n 1)) echo "${api-id}" && \
aws apigateway get-resources --rest-api-id "${api-id}" --region "${region}" --profile "${profile}" | jq -cr '[.items[] | select( .path==("/")) ] | .[0].id '
get-resource-id:
$(eval api-id := $(shell make get-api-id | tail -n 1)) echo "${api-id}" && \
aws apigateway get-resources --rest-api-id "${api-id}" --region "${region}" --profile "${profile}" | jq -cr '[.items[] | select (.pathPart==("imdb")) ] | .[0].id'
create-api-resource:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval parent-id := $(shell make get-parent-id | tail -n 1))
aws apigateway create-resource --rest-api-id "${api-id}" --parent-id "${parent-id}" --path-part "${path}" --region "${region}" --profile "${profile}"
create-api-method:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval resource-id := $(shell make get-resource-id | tail -n 1))
aws apigateway put-method --rest-api-id "${api-id}" --resource-id "${resource-id}" --http-method POST --authorization-type NONE --region "${region}" --profile "${profile}"
create-api-integration:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval resource-id := $(shell make get-resource-id | tail -n 1))
$(eval lambda-arn := $(shell make get-lambda-arn | tail -n 1))
aws apigateway put-integration --rest-api-id "${api-id}" \
--resource-id "${resource-id}" \
--http-method POST \
--type AWS \
--profile "${profile}" \
--region "${region}" --integration-http-method POST --uri 'arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${lambda-arn}/invocations'
create-method-response:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval resource-id := $(shell make get-resource-id | tail -n 1))
$(eval lambda-arn := $(shell make get-lambda-arn | tail -n 1))
aws apigateway put-method-response \
--rest-api-id ${api-id} \
--resource-id ${resource-id} \
--http-method POST \
--status-code 200 \
--response-models "{}" \
--profile ${profile} \
--region ${region}
create-integration-response:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval resource-id := $(shell make get-resource-id | tail -n 1))
$(eval lambda-arn := $(shell make get-lambda-arn | tail -n 1))
aws apigateway put-integration-response \
--rest-api-id ${api-id} \
--resource-id ${resource-id} \
--http-method POST \
--status-code 200 \
--selection-pattern ".*" \
--profile ${profile} \
--region ${region}
get-api-arn:
$(eval api-id := $(shell make get-api-id | tail -n 1))
$(eval lambda-arn := $(shell make get-lambda-arn | tail -n 1))
echo ${lambda-arn} | sed -e 's/lambda/execute-api/' -e "s/function:${name}/${api-id}/"
deploy-gateway:
$(eval api-id := $(shell make get-api-id | tail -n 1))
aws apigateway create-deployment --rest-api-id ${api-id} --stage-name prod --region ${region} --profile ${profile}
create-api-permissions:
$(eval api-arn := $(shell make get-api-arn | tail -n 1))
aws lambda add-permission --function-name ${name} --statement-id apigateway-$name \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "${api-arn}/*/POST/$path" \
--region ${region} \
--profile ${profile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment