Skip to content

Instantly share code, notes, and snippets.

@tpaschalis
Created November 5, 2020 22:02
Show Gist options
  • Save tpaschalis/475db49d5034dff1ec3432936dbc40b4 to your computer and use it in GitHub Desktop.
Save tpaschalis/475db49d5034dff1ec3432936dbc40b4 to your computer and use it in GitHub Desktop.
Triggering an AWS Lambda through a POST request
$ aws apigateway create-rest-api --name lambda-trigger-api
HEADER 2020-11-05T20:24:08+02:00 False <api-id> lambda-trigger-api
TYPES EDGE
$ aws apigateway get-rest-api --rest-api-id <api-id>
HEADER 2020-11-05T20:24:08+02:00 False <api-id> lambda-trigger-api
TYPES EDGE
$ aws apigateway get-resources --rest-api-id <api-id>
ITEMS <api-root-id> /
$ aws apigateway create-resource --rest-api-id <api-id> --path-part somepath --parent-id <api-root-id>
<resource_id> <api-root-id> /somepath somepath
$ aws apigateway put-method --rest-api-id <api-id> --resource-id <resource_id> --http-method POST --authorization-type NONE
False NONE POST
$ aws apigateway get-method --rest-api-id <api-id> --resource-id <resource_id> --http-method POST
$ aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource_id> \
--http-method POST --type AWS --integration-http-method POST \
--uri arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:123456789012:function:sample-event-handle/invocations
<resource_id> POST WHEN_NO_MATCH 29000 AWS arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-central-1:123456789012:function:sample-event-handle/invocations
$ aws apigateway put-method-response --rest-api-id <api-id> \
--resource-id <resource_id> --http-method POST \
--status-code 200 --response-models application/json=Empty
200
RESPONSEMODELS Empty
$ aws apigateway create-model --rest-api-id <api-id> \
--name 'lambdaResponse' \
--description 'lambdaResponseModel' \
--content-type 'application/json' \
--schema '{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "lambdaResponse", "type": "string" }'
application/json lambdaResponseModel p5712b lambdaResponse { "$schema": "http://json-schema.org/draft-04/schema#", "title": "lambdaResponse", "type": "string" }
$ aws apigateway put-method-response --rest-api-id <api-id> \
--resource-id <resource_id> --http-method POST \
--status-code 200 --response-models '{"application/json":"lambdaResponse"}'
200
RESPONSEMODELS lambdaResponse
$ aws apigateway put-integration-response --rest-api-id <api-id> \
--resource-id <resource_id> --http-method POST \
--status-code 200 --response-templates application/json=""
200
RESPONSETEMPLATES None
$ aws apigateway create-deployment --rest-api-id <api-id> --stage-name my-stage
2020-11-05T20:40:00+02:00 qf2slj
$ aws lambda add-permission --function-name sample-event-handle \
--statement-id apigateway-test-2 --action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:eu-central-1:123456789012:<api-id>/*/POST/somepath"
{"Sid":"apigateway-test-2","Effect":"Allow","Principal":{"Service":"apigateway.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:eu-central-1:123456789012:function:sample-event-handle","Condition":{"ArnLike":{"AWS:SourceArn":"arn:aws:execute-api:eu-central-1:123456789012:<api-id>/*/POST/somepath"}}}
$ aws lambda add-permission --function-name sample-event-handle \
--statement-id apigateway-prod-2 --action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:eu-central-1:123456789012:<api-id>/my-stage/POST/somepath"{"Sid":"apigateway-prod-2","Effect":"Allow","Principal":{"Service":"apigateway.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:eu-central-1:123456789012:function:sample-event-handle","Condition":{"ArnLike":{"AWS:SourceArn":"arn:aws:execute-api:eu-central-1:123456789012:<api-id>/my-stage/POST/somepath"}}}
$ aws apigateway test-invoke-method --rest-api-id <api-id> \
--resource-id <resource_id> --http-method POST --path-with-query-string "" \
--body file://test-payload.json
Thu Nov 05 19:46:58 UTC 2020 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:123456789012:function:sample-event-handle/invocations
Thu Nov 05 19:46:58 UTC 2020 : Received response. Status: 200, Integration latency: 35 ms
Thu Nov 05 19:46:58 UTC 2020 : Endpoint response headers: {Date=Thu, 05 Nov 2020 19:46:58 GMT, Content-Type=application/json, Content-Length=35, Connection=keep-alive, x-amzn-RequestId=396af6e5-fe9b-42c7-87c4-9db090402c02, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5fa456b2-beda73b8340f62daf4d397fe;sampled=0}
Thu Nov 05 19:46:58 UTC 2020 : Endpoint response body before transformations: "{ID:tpaschalis Val:100 Flag:true}"
Thu Nov 05 19:46:58 UTC 2020 : Method response body after transformations: "{ID:tpaschalis Val:100 Flag:true}"
Thu Nov 05 19:46:58 UTC 2020 : Method response headers: {X-Amzn-Trace-Id=Root=1-5fa456b2-beda73b8340f62daf4d397fe;Sampled=0, Content-Type=application/json}
Thu Nov 05 19:46:58 UTC 2020 : Successfully completed execution
Thu Nov 05 19:46:58 UTC 2020 : Method completed with status: 200
200
HEADERS application/json Root=1-5fa456b2-beda73b8340f62daf4d397fe;Sampled=0
CONTENT-TYPE application/json
X-AMZN-TRACE-ID Root=1-5fa456b2-beda73b8340f62daf4d397fe;Sampled=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment