question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add an API endpoint for a Lambda function?

See original GitHub issue

I’m trying to automate bringing up an API gateway, uploading a lambda function, and tying the two together.

I can successfully create a new REST API, add a Resource, and add a method to that Resource. I can successfully upload my lambda function.

However, I can’t figure out how to integrate it so that my API triggers a lambda function because I don’t know how to add an API endpoint to my lambda function using boto3.

Trying to call this function, not sure how to get a uri for my newly created lambda function -

response = apiClient.put_integration(
                restApiId=restApiId,
                resourceId=helloWorldId,
                httpMethod="POST",
                integrationHttpMethod="POST",
                type="AWS",
                uri=??????????????????,
  )

In short, I need help figuring out how to programmatically fill this portion of my lambda function using boto3 (and then retrieving that URI): screen shot 2016-03-29 at 2 21 11 pm

Any help would be appreciated. Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
AJRenoldcommented, Apr 13, 2016

I ran into this issue implementing something similar today. Here’s a copy paste of code that should get your Lambda hooked up with an API Gateway endpoint. Way too many steps if you ask me!

lambda_func_name = 'callmemaybe'
AWS_LAMBDA_API_ID = 'abcd1234'
AWS_REGION = 'us-east-1'

api_client = boto3.client('apigateway', region_name=AWS_REGION)
aws_lambda = boto3.client('lambda', region_name=AWS_REGION)

## create resource
resource_resp = api_client.create_resource(
    restApiId=AWS_LAMBDA_API_ID,
    parentId='foo', # resource id for the Base API path
    pathPart=lambda_func_name
)

## create POST method
put_method_resp = api_client.put_method(
    restApiId=AWS_LAMBDA_API_ID,
    resourceId=resource_resp['id'],
    httpMethod="POST",
    authorizationType="NONE",
    apiKeyRequired=True,

)

lambda_version = aws_lambda.meta.service_model.api_version

uri_data = {
    "aws-region": AWS_REGION,
    "api-version": lambda_version,
    "aws-acct-id": "xyzABC",
    "lambda-function-name": lambda_func_name,
}

uri = "arn:aws:apigateway:{aws-region}:lambda:path/{api-version}/functions/arn:aws:lambda:{aws-region}:{aws-acct-id}:function:{lambda-function-name}/invocations".format(**uri_data)

## create integration
integration_resp = api_client.put_integration(
    restApiId=AWS_LAMBDA_API_ID,
    resourceId=resource_resp['id'],
    httpMethod="POST",
    type="AWS",
    integrationHttpMethod="POST",
    uri=uri,
)

api_client.put_integration_response(
    restApiId=AWS_LAMBDA_API_ID,
    resourceId=resource_resp['id'],
    httpMethod="POST",
    statusCode="200",
    selectionPattern=".*"
)

## create POST method response
api_client.put_method_response(
    restApiId=AWS_LAMBDA_API_ID,
    resourceId=resource_resp['id'],
    httpMethod="POST",
    statusCode="200",
)

uri_data['aws-api-id'] = AWS_LAMBDA_API_ID
source_arn = "arn:aws:execute-api:{aws-region}:{aws-acct-id}:{aws-api-id}/*/POST/{lambda-function-name}".format(**uri_data)

aws_lambda.add_permission(
    FunctionName=lambda_func_name,
    StatementId=uuid.uuid4().hex,
    Action="lambda:InvokeFunction",
    Principal="apigateway.amazonaws.com",
    SourceArn=source_arn
)

# state 'your stage name' was already created via API Gateway GUI
api_client.create_deployment(
    restApiId=AWS_LAMBDA_API_ID,
    stageName="your stage name",
)
8reactions
ajcritescommented, Aug 11, 2016

@ndcampbell the issue for me was just that lambda can only be invoked with POST so if you are creating an integration request to lambda it must be POST no matter what the method request is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a simple microservice using Lambda and API Gateway
Sign in to the AWS Management Console and open the AWS Lambda console. · Choose Create Lambda function. · Choose Use a blueprint....
Read more >
How To Create an Endpoint for an AWS Lambda Function ...
Now, let's see it in action. Open your favorite API tester, such as Postman. Create a new request with any HTTP method and...
Read more >
How to Create Lambda function URLs - DEV Community ‍ ‍
How to Create Lambda function URLs · First of all, go through Architecture Diagram. · In AWS management console search and go inside...
Read more >
Creating an AWS Lambda Function and API Endpoint | Slack
Go to your Lambda function · Click the Triggers tab · Click the method POST · Use the left tree to navigate to...
Read more >
Make REST APIs Using AWS Lambda and API Gateway
Step 2: Create Amazon API Gateway · Now this lambda function is a downstream system of your API. All the request landing on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found