AWS APIGateway and Lambda — a GET Mehod CLI Demo

Image for post
Image for post

What is AWS Lambda

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

What is AWS APIGateway

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

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

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"
}
}
}
}
Image for post
Image for post

Step 3: Get the Endpoint

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

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

Step 5: Cleaning Up

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

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store