AWS APIGateway and Lambda — a GET Mehod CLI Demo

100% AWS CLI Based Execution

What is AWS Lambda

AWS Lambda is a function as a service! You need not manage the computing resources, load balancing etc. All you need to do is to create your function in any of the popular programming languages and upload it to AWS Lambda.

Lambda functions are triggered based on events that can be defined.

What is AWS APIGateway

Using AWS Gateway we can create public HTTP endpoints that can trigger internal AWS events. It can also receive and transform parameters.

In this demo we will create a Lambda function and expose it publicly using APIGateway. We will use HTTP GET method to accept input.

We will use the same Lambda function we created in this demo.

Step 1: Create a REST API Resource

Below are the steps to be done. First being the creation of a REST API.

aws apigateway create-rest-api --name 'SreeDemo1' --description 'My CLI Demo'
{
"id": "bjiowh3ozi",
"name": "SreeDemo1",
"description": "My CLI Demo",
"createdDate": 1542779236,
"apiKeySource": "HEADER",
"endpointConfiguration": {
"types": [
"EDGE"
]
}
}

Let us save the api id.

api_id=bjiowh3oziaws apigateway get-resources --rest-api-id $api_id
{
"items": [
{
"id": "qunfinifb8",
"path": "/"
}
]
}

We need the root resource id. Under one API we can create multiple resources as well.

root_id=qunfinifb8

Step 2: Prepare the GET Method

We will need to do a series of steps ready a GET method for this recourse.

aws apigateway put-method --rest-api-id $api_id --resource-id $root_id \
--http-method GET --authorization-type NONE \
--no-api-key-required \
--request-parameters "{ \"method.request.querystring.kg\": false }"

{
"httpMethod": "GET",
"authorizationType": "NONE",
"apiKeyRequired": false,
"requestParameters": {
"method.request.querystring.kg": false
}
}

Notice the request parameter kg in the above command. That is our query string param. There is not authorization for this GET method.

Next is the method response.

aws apigateway put-method-response --rest-api-id $api_id \
--resource-id $root_id --http-method GET \
--status-code 200 --response-models "{\"application/json\": \"Empty\"}"
{
"statusCode": "200",
"responseModels": {
"application/json": "Empty"
}
}

Now, connect to the Lambda function.

aws apigateway put-integration --rest-api-id $api_id --resource-id $root_id \
--type AWS \
--http-method GET \
--integration-http-method POST \
--uri "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:11111111:function:sree-py1/invocations" \
--request-templates "{ \"application/json\": \"{\\n\\\"Kg\\\": \\\"\$input.params('kg')\\\"\\n}\" }"
{
"type": "AWS",
"httpMethod": "POST",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:11111111:function:sree-py1/invocations",
"requestTemplates": {
"application/json": "{\n\"Kg\": \"$input.params('kg')\"\n}"
},
"passthroughBehavior": "WHEN_NO_MATCH",
"timeoutInMillis": 29000,
"cacheNamespace": "qunfinifb8",
"cacheKeyParameters": []
}

Notice the mapping template that will transform the query string parameter to JSON needed by our Lambda function.

Now add the integration response to receive the output from the Lambda function.

aws apigateway put-integration-response --rest-api-id $api_id \
--resource-id $root_id --http-method GET \
--status-code 200 \
--content-handling CONVERT_TO_TEXT

{
"statusCode": "200",
"contentHandling": "CONVERT_TO_TEXT"
}

Step 3: Add Permission

We need to inform our Lambda function to allow calls from our APIGateway GET method!

aws lambda add-permission --function-name sree-py1 \
--statement-id apigateway-test-2 --action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:us-west-2:11111111:$api_id/*/GET/"

{
"Statement": "{\"Sid\":\"apigateway-test-3\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws:lambda:us-west-2:11111111:function:sree-py1\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-west-2:11111111:bjiowh3ozi/*/GET/\"}}}"
}

Final get method will look like this.

aws apigateway get-method --rest-api-id $api_id --resource-id $root_id --http-method GET
{
"httpMethod": "GET",
"authorizationType": "NONE",
"apiKeyRequired": false,
"requestParameters": {
"method.request.querystring.kg": false
},
"methodResponses": {
"200": {
"statusCode": "200",
"responseModels": {
"application/json": "Empty"
}
}
},
"methodIntegration": {
"type": "AWS",
"httpMethod": "POST",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:11111111:function:sree-py1/invocations",
"requestTemplates": {
"application/json": "{\n\"Kg\": \"$input.params('kg')\"\n}"
},
"passthroughBehavior": "WHEN_NO_MATCH",
"timeoutInMillis": 29000,
"cacheNamespace": "qunfinifb8",
"cacheKeyParameters": [],
"integrationResponses": {
"200": {
"statusCode": "200",
"contentHandling": "CONVERT_TO_TEXT"
}
}
}
}

Step 3: Get the Endpoint

Let us create a deployment stage.

aws apigateway create-deployment --rest-api-id $api_id --stage-name stage1-prodaws apigateway get-stages --rest-api-id $api_id 
{
"item": [
{
"deploymentId": "dqdtli",
"stageName": "stage1-prod",
"cacheClusterEnabled": false,
"cacheClusterStatus": "NOT_AVAILABLE",
"methodSettings": {},
"createdDate": 1542780856,
"lastUpdatedDate": 1542780856
}
]
}

Step 4: End to End Testing

Let us use curl utility to pass the kg value as below.

curl https://bjiowh3ozi.execute-api.us-west-2.amazonaws.com/stage3-prod?kg=22"22 Kgs is 48.5 lbs\n"

Step 5: Cleaning Up

aws apigateway delete-rest-api --rest-api-id $api_id

Sreeprakash Neelakantan

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade