Skip to content

Instantly share code, notes, and snippets.

@helton
Last active June 27, 2021 20:56
Show Gist options
  • Save helton/893c504e399b429a4734f7c1e3a6e713 to your computer and use it in GitHub Desktop.
Save helton/893c504e399b429a4734f7c1e3a6e713 to your computer and use it in GitHub Desktop.
AWS API Gateway - Examples

API Gateway - Examples

curl "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/dev/api/just-passthrough" -H "Authorization: Basic <base64encodeduserpassword>"
  • Using passthrough (manipulating response payload):
curl "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/dev/api/passthrough-extract-json" -H "Authorization: Basic <base64encodeduserpassword>"
  • Using lambda function to process request:
curl "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/dev/api/using-lambda" -H "Authorization: Basic <base64encodeduserpassword>"
  • Using mocked response (no backend):
curl "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/dev/api/using-mock" -H "Authorization: Basic <base64encodeduserpassword>"

References

const USER = "<some-user-here>";
const PASSWORD = "<some-password-here>";
exports.handler = function (event, context, callback) {
var authorizationHeader = event.headers.authorization
if (!authorizationHeader) return callback('Unauthorized')
var encodedCreds = authorizationHeader.split(' ')[1]
var plainCreds = (new Buffer(encodedCreds, 'base64')).toString().split(':')
var username = plainCreds[0]
var password = plainCreds[1]
console.log(`username: ${username}`);
console.log(`password: ${password}`);
if (!(username === USER && password === PASSWORD)) return callback('Unauthorized')
var authResponse = buildAllowAllPolicy(event, username)
callback(null, authResponse)
}
function buildAllowAllPolicy (event, principalId) {
var apiOptions = {}
var tmp = event.methodArn.split(':')
var apiGatewayArnTmp = tmp[5].split('/')
var awsAccountId = tmp[4]
var awsRegion = tmp[3]
var restApiId = apiGatewayArnTmp[0]
var stage = apiGatewayArnTmp[1]
var apiArn = 'arn:aws:execute-api:' + awsRegion + ':' + awsAccountId + ':' +
restApiId + '/' + stage + '/*/*'
const policy = {
principalId: principalId,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'execute-api:Invoke',
Effect: 'Allow',
Resource: [apiArn]
}
]
}
}
return policy
}
const https = require("https")
function fetchJson(url) {
return new Promise((resolve, reject) => {
https.get(url, res => {
let data = "";
res.on("data", d => {
data += d
});
res.on("end", () => {
const json = JSON.parse(data).entries[0].json
resolve(json);
});
res.on("error", err => {
reject(err);
})
});
});
}
exports.handler = async (event) => {
const url = "https://run.mocky.io/v3/a89f3fb6-a34c-4ca1-9854-bbf97a108d45";
const response = await fetchJson(url);
return response;
};
{
"openapi": "3.0.1",
"info": {
"title": "my-api",
"version": "2021-06-27T19:56:31Z"
},
"servers": [
{
"url": "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/{basePath}",
"variables": {
"basePath": {
"default": "/dev"
}
}
}
],
"paths": {
"/api/using-mock": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "mock",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\n \"some_key\": \"some value from mock\"\n}"
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
},
"/api/using-lambda": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "aws",
"httpMethod": "POST",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:706689456704:function:extract-json/invocations",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match",
"contentHandling": "CONVERT_TO_TEXT"
}
}
},
"/api/just-passthrough": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "https://run.mocky.io/v3/a89f3fb6-a34c-4ca1-9854-bbf97a108d45",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match"
}
}
},
"/api/passthrough-extract-json": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "https://run.mocky.io/v3/a89f3fb6-a34c-4ca1-9854-bbf97a108d45",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.entries[0].json')"
}
}
},
"passthroughBehavior": "when_no_match"
}
}
}
},
"components": {
"schemas": {
"Empty": {
"title": "Empty Schema",
"type": "object"
}
}
}
}
{
"openapi": "3.0.1",
"info": {
"title": "my-api",
"version": "2021-06-27T19:56:31Z"
},
"servers": [
{
"url": "https://ocywyw3jq5.execute-api.us-east-1.amazonaws.com/{basePath}",
"variables": {
"basePath": {
"default": "/dev"
}
}
}
],
"paths": {
"/api/using-mock": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
}
}
},
"/api/using-lambda": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
}
}
},
"/api/just-passthrough": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
}
}
},
"/api/passthrough-extract-json": {
"get": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Empty": {
"title": "Empty Schema",
"type": "object"
}
}
}
}
{
"swagger": "2.0",
"info": {
"version": "2021-06-27T19:56:31Z",
"title": "my-api"
},
"host": "ocywyw3jq5.execute-api.us-east-1.amazonaws.com",
"basePath": "/dev",
"schemes": [
"https"
],
"paths": {
"/api/just-passthrough": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-integration": {
"httpMethod": "GET",
"uri": "https://run.mocky.io/v3/a89f3fb6-a34c-4ca1-9854-bbf97a108d45",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match",
"type": "http"
}
}
},
"/api/passthrough-extract-json": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-integration": {
"httpMethod": "GET",
"uri": "https://run.mocky.io/v3/a89f3fb6-a34c-4ca1-9854-bbf97a108d45",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.entries[0].json')"
}
}
},
"passthroughBehavior": "when_no_match",
"type": "http"
}
}
},
"/api/using-lambda": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:706689456704:function:extract-json/invocations",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match",
"contentHandling": "CONVERT_TO_TEXT",
"type": "aws"
}
}
},
"/api/using-mock": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\n \"some_key\": \"some value from mock\"\n}"
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
}
}
}
},
"definitions": {
"Empty": {
"type": "object",
"title": "Empty Schema"
}
}
}
{
"swagger": "2.0",
"info": {
"version": "2021-06-27T19:56:31Z",
"title": "my-api"
},
"host": "ocywyw3jq5.execute-api.us-east-1.amazonaws.com",
"basePath": "/dev",
"schemes": [
"https"
],
"paths": {
"/api/just-passthrough": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
}
}
},
"/api/passthrough-extract-json": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
}
}
},
"/api/using-lambda": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
}
}
},
"/api/using-mock": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
}
}
}
},
"definitions": {
"Empty": {
"type": "object",
"title": "Empty Schema"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment