In this section, we show how to set up an API with the Lambda proxy integration using the AWS CLI. For detailed instructions for using the API Gateway console to configure a proxy resource with the Lambda proxy integration, see Tutorial: Create a REST API with a Lambda proxy integration.
As an example, we use the following sample Lambda function as the backend of the API:
export const handler = function(event, context, callback) { console.log('Received event:', JSON.stringify(event, null, 2)); var res ={ "statusCode": 200, "headers": { "Content-Type": "*/*" } }; var greeter = 'World'; if (event.greeter && event.greeter!=="") { greeter = event.greeter; } else if (event.body && event.body !== "") { var body = JSON.parse(event.body); if (body.greeter && body.greeter !== "") { greeter = body.greeter; } } else if (event.queryStringParameters && event.queryStringParameters.greeter && event.queryStringParameters.greeter !== "") { greeter = event.queryStringParameters.greeter; } else if (event.multiValueHeaders && event.multiValueHeaders.greeter && event.multiValueHeaders.greeter != "") { greeter = event.multiValueHeaders.greeter.join(" and "); } else if (event.headers && event.headers.greeter && event.headers.greeter != "") { greeter = event.headers.greeter; } res.body = "Hello, " + greeter + "!"; callback(null, res); };
Comparing this to the Lambda custom integration setup in Set up Lambda custom integrations in API Gateway, the input to this Lambda function can be expressed in the request parameters and body. You have more latitude to allow the client to pass the same input data. Here, the client can pass the greeter's name in as a query string parameter, a header, or a body property. The function can also support the Lambda custom integration. The API setup is simpler. You do not configure the method response or integration response at all.
To set up a Lambda proxy integration using the AWS CLI
-
Use the following create-rest-api command to create an API:
aws apigateway create-rest-api --name 'HelloWorld (AWS CLI)'
The output will look like the following:
{ "name": "HelloWorldProxy (AWS CLI)", "id": "te6si5ach7", "rootResourceId" : "krznpq9xpg", "createdDate": 1508461860 }
You use the API
id
(te6si5ach7
) and therootResourceId
(krznpq9xpg
) throughout this example. -
Use the following create-resource command to create an API Gateway Resource of
/greeting
:aws apigateway create-resource \ --rest-api-id te6si5ach7 \ --parent-id krznpq9xpg \ --path-part {proxy+}
The output will look like the following:
{ "path": "/{proxy+}", "pathPart": "{proxy+}", "id": "2jf6xt", "parentId": "krznpq9xpg" }
You use the
{proxy+}
resource'sid
value (2jf6xt
) to create a method on the/{proxy+}
resource in the next step. -
Use the following put-method to create an
ANY
method request ofANY /{proxy+}
:aws apigateway put-method --rest-api-id te6si5ach7 \ --resource-id 2jf6xt \ --http-method ANY \ --authorization-type "NONE"
The output will look like the following:
{ "apiKeyRequired": false, "httpMethod": "ANY", "authorizationType": "NONE" }
This API method allows the client to receive or send greetings from the Lambda function at the backend.
-
Use the following put-integration command to set up the integration of the
ANY /{proxy+}
method with a Lambda function, namedHelloWorld
. This function responds to the request with a message of"Hello, {name}!"
, if thegreeter
parameter is provided, or"Hello, World!"
, if the query string parameter is not set.aws apigateway put-integration \ --rest-api-id te6si5ach7 \ --resource-id 2jf6xt \ --http-method ANY \ --type AWS_PROXY \ --integration-http-method POST \ --uri arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:HelloWorld/invocations \ --credentials arn:aws:iam::123456789012:role/apigAwsProxyRole
Important
For Lambda integrations, you must use the HTTP method of
POST
for the integration request, according to the specification of the Lambda service action for function invocations. The IAM role ofapigAwsProxyRole
must have policies allowing theapigateway
service to invoke Lambda functions. For more information about IAM permissions, see API Gateway permissions model for invoking an API.The output will look like the following:
{ "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:1234567890:function:HelloWorld/invocations", "httpMethod": "POST", "cacheNamespace": "vvom7n", "credentials": "arn:aws:iam::1234567890:role/apigAwsProxyRole", "type": "AWS_PROXY" }
Instead of supplying an IAM role for
credentials
, you can use the add-permission command to add resource-based permissions. This is what the API Gateway console does. -
Use the following create-deployment command to deploy the API to a
test
stage:aws apigateway create-deployment \ --rest-api-id te6si5ach7 \ --stage-name test
-
Test the API using the following cURL commands in a terminal.
Calling the API with the query string parameter of
?greeter=jane
:curl -X GET 'https://te6si5ach7.execute-api.us-west-2.amazonaws.com/test/greeting?greeter=jane'
Calling the API with a header parameter of
greeter:jane
:curl -X GET https://te6si5ach7.execute-api.us-west-2.amazonaws.com/test/hi \ -H 'content-type: application/json' \ -H 'greeter: jane'
Calling the API with a body of
{"greeter":"jane"}
:curl -X POST https://te6si5ach7.execute-api.us-west-2.amazonaws.com/test/hi \ -H 'content-type: application/json' \ -d '{ "greeter": "jane" }'
In all the cases, the output is a 200 response with the following response body:
Hello, jane!