AWS Lambda with API Gateway

25 May 2016

SC London
The place where passionate Software Craftspeople, Community, Experts & Thought Leaders come together
Book your ticket now!

In a previous post I showed you how to create and deploy an AWS Lambda. We will continue that work and look at updating just the code for that lambda. We will also add a REST endpoint to the AWS Lambda using AWS API Gateway.

So before you continue ... if you haven't already, please follow the instruction in the previous post to make sure you have a running AWS Lambda instance.

Step 1: Update your lambda

Paste the following in


### Create the lambda package
zip -j *.py


### Update the lambda code
aws lambda update-function-code \
  --function-name $function_name \
  --zip-file fileb://$package_file

or for Java


### Create the lambda package
mvn package


### Update the lambda code
aws lambda update-function-code \
   --function-name $function_name \
   --zip-file fileb://$package_file

Make the script executable chmod +x and update your lambda ./

Step 2: Pass something to your lambda

Now that we know how to update the lambda in the cloud, lets make a change so we can pass something as a parameter. Rather than saying "hello world!" we want it to say hello to whomever.

In Python:

def lambda_handler(event, context):
    return "Hello {}!".format(event['to'])

or like the following in Java:

package example;


public class Hello {
  public String lambdaHandler(Request request, Context context) {
    return "Hello " + request.getTo() + "!";

class Request {
  private String to;
  public void setTo(String to) { = to; }
  public String getTo() { return to; }

Step 3: Tell the lambda to say hello to whomever

aws lambda invoke --invocation-type RequestResponse --function-name helloworld --payload '{"to": "whomever"}' output.txt

You should see Hello whomever! in output text

Step 4: Lets add the rest API

Paste the following script into a file such as, change permissions for the file to execute, and execute the script. Take a deep breath ...

Note: this script expects the AWS_REGION and AWS_ACCOUNT_ID environment variables to be defined

set -e


echo "Creating a new API and capturing it's ID ..."
api_id=$(aws apigateway create-rest-api \
   --name HelloWorldAPI \
   --description "Hello World API" \
   --output text \
   --query 'id')
echo "> API ID is: $api_id"

echo "Storing the API ID on disk - we'll need it later ..."
echo $api_id > api_id.txt

echo "Geting the root resource id for the API ..."
root_id=$(aws apigateway get-resources \
   --rest-api-id "${api_id}" \
   --output text \
   --query 'items[?path==`'/'`].[id]')
echo root_id=$root_id

echo "Creating a resource for the /hello path"
resource_id=$(aws apigateway create-resource \
  --rest-api-id "${api_id}" \
  --parent-id "${root_id}" \
  --path-part hello | jq -r .id) 
echo "Resource id is $resource_id"

echo "Creating the GET method on the /hello resource"
aws apigateway put-method \
  --rest-api-id "${api_id}" \
  --resource-id "${resource_id}" \
  --http-method GET \
  --authorization-type NONE 

echo "Integrating the GET method to lambda. Note that the request tempalate uses API Gateway template language to pull in the query parameters as a JSON event for the lambda."
aws apigateway put-integration \
  --rest-api-id "${api_id}" \
  --resource-id "${resource_id}" \
  --http-method GET \
  --type AWS \
  --request-templates '{ "application/json": "{\n  #foreach($param in $input.params().querystring.keySet())\n    \"$param\": \"$util.escapeJavaScript($input.params().querystring.get($param))\" \n   #end\n  }" }' \
  --integration-http-method POST \
  --uri arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${account_id}:function:helloworld/invocations

echo "Creating a default response for the GET method"
aws apigateway put-method-response \
  --rest-api-id "${api_id}" \
  --resource-id "${resource_id}" \
  --http-method GET \
  --status-code 200 

echo "Creating a default response for the integration"
aws apigateway put-integration-response \
  --rest-api-id "${api_id}" \
  --resource-id "${resource_id}" \
  --http-method GET \
  --status-code 200 \
  --selection-pattern ".*"

echo "Adding permission for the API to call the lambda for test so we can use the console to make the api call"
aws lambda add-permission \
  --function-name helloworld \
  --statement-id apigateway-helloworld-get-test \
  --action lambda:InvokeFunction \
  --principal \
  --source-arn "arn:aws:execute-api:${region}:${account_id}:${api_id}/*/GET/hello"

echo "Adding permission for the API to call the lambda from any HTTP client"
aws lambda add-permission \
  --function-name helloworld \
  --statement-id apigateway-helloworld-get \
  --action lambda:InvokeFunction \
  --principal \
  --source-arn "arn:aws:execute-api:${region}:${account_id}:${api_id}/api/GET/hello"

echo "Creating a deployment"
aws apigateway create-deployment \
  --rest-api-id "${api_id}" \
  --stage-name api 

echo "All done! you can invoke the api on https://${api_id}.execute-api.${region}"

Step 5: Invoke the API

The last output of the scripts provides the URL that you can paste into the browser. You should see the response "Hello whomever!" once you hit enter in the brower.

Step 6: The Cleanup

You can use the script created in the previous post to delete the lambda. To delete the api: Paste the following script and execute as as per usual.

echo "Reading API id that I store in my create-api script"

echo "Removing the permissions from the lambda"
aws lambda remove-permission \
  --function-name helloworld \
  --statement-id apigateway-helloworld-get
aws lambda remove-permission \
  --function-name helloworld \
  --statement-id apigateway-helloworld-get-test

echo "Deleting the API"
aws apigateway delete-rest-api \
  --rest-api-id "${api_id}"

Step 7: Relax ... its over ;)

... for now!!!

Blogs Relacionados

Get content like this straight to your inbox!

Codurance Logo

Software es nuestra pasión.

Somos Software Craftspeople. Construimos software bien elaborado para nuestros clientes, ayudamos a los desarrolladores a mejorar en su oficio a través de la formación, la orientación y la tutoría y ayudamos a las empresas a mejorar en la distribución de software.

Últimos Blogs


3 Sutton Lane, planta 3
Londres, EC1M 5PU

Teléfono: +44 207 4902967

2 Mount Street
Manchester, M2 5WQ

Teléfono: +44 161 302 6795

Carrer de Pallars 99, planta 4, sala 41
Barcelona, 08018

Teléfono: +34 937 82 28 82

Correo electrónico: