Scripting permissions to execute lambda from aws api gateway - aws-lambda

In the aws UI, I get this message when I want to add a lambda function to aws api gateway integration request. How can I script this using boto3?
Been struggling with doing it using lambda: add_permission but missing something still.

This is possible using lambda add_permission API or CLI.
The policy should be structured like this:
...
"AWS:SourceArn":"arn:aws:execute-api:[REGION]:[ACCOUNT_ID]:[API_ID]/*/[HTTP_VERB]/[RESOURCE_PATH]"}},
"Action":"lambda:InvokeFunction",
"Resource":"arn:aws:lambda:[REGION]:[ACCOUNT_ID]:function:[FUNCTION_NAME]",
"Effect":"Allow",
"Principal":{"Service":"apigateway.amazonaws.com"}
...

Related

webhook to AWS Lambda function

I am creating the AWS Lambda Function, Where the values will be sent in the post method on the Webhook Url from the chat bot. Can anyone suggest me to create the webhook from WP site to the AWS Lambda so that i can able to make it in working condition.
You can invoke the AWS lambda on triggers like SQS queue trigger, s3 trigger, etc. So You can do one thing once your post method calls upload a JSON file to S3 and apply trigger that whenever any file uploaded to your S3 bucket trigger the AWS lambda in this way it will work. You can even define some URL with Route 53 and trigger lambda when that URL hits.

AWS Lambda forbidden error via api gateway get request

I am currently working on aws and have an apigateway with one get request. I then have a lambda which i call via the api gateway.
So when i now call the gateway passing the accesstoken as generated from iam when logging in it throws an error
{
"message": "Unauthorized"
}
I thought it might have been the gateway but it actually hits the lambda as i did console log in there. So not sure how else i can check what it can be.
I am using Lambda proxy integration.
Any tips?
Cheers

How should I diagnose an HTTP request 500 that appears to be related to Authorizers in AWS API Gateway?

I am brand new to AWS API Gateway/AWS Lambda/AWS Amplify. I have a React Native application that I am trying to use AWS Amplify to make an API call and issue a PUT request, which would then cause the API Gateway to invoke my AWS Lambda function. When I create the AWS Amplify API endpoint, I don't see a command line option to define a PUT method. The default is "Any", which works, but I would like to specify a PUT method specifically. When I add in a PUT method manually on the API Gateway website and then call it from my React Native front end, I get...
Error: Request failed with status code 500
Looking at the API Gateway responses, this is due to either an "Authorizer Configuration Error" or an "Authorizer Failure", so I am assuming the problem is not with my front end code but with the configuration of "authorizers" on the API Gateway. What are authorizers? How do they relate to making an API call? And what steps can I take to troubleshoot what the problem might be?
You can use a lambda, a Cognito User Pool or an IaM role as an Authorizer. The short version is that your API endpoints can either be open and public or have an Authorizer, if they have an authorizer then they have to be setup correctly. It is set through the API Gateway config for an endpoint in the Method Request section.
AWS Lambda authorizer info:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
AWS Cognito Info:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html

how does aws lambda invoke work

I am fairly new to lambda and trying to wrap my head around it. I created a basic hello world function and invoked it through
aws lambda invoke
My question is
Dont I have to create an API gateway and expose the lambda function through the API gateway for it work.
How does aws lambda invoke if I have not created a gateway and exposed the function?
You do not have to necessarily create API gateway for invoking lambda function. Every lambda function is already available to be accessed via Amazon's Web Service using API:
POST /2015-03-31/functions/FunctionName/invocations?Qualifier=Qualifier HTTP/1.1
See http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html for more details.
However, the above API expects that the request payload is signed using aws signature version 4 . The CLI call aws lambda invoke automatically takes care of that piece once you have configured valid access and secret keys.
The API Gateway in front of lambda allows you to add:
Custom resource names
Custom authentication schemes (even no authentication if desired)
Custom way of sending payload
and more...
In summary, API Gateway gives you more control over the API resource and can even abstract the user from internals of AWS API.

AWS API Gateway Custom Authorizer not running

I have created a Lambda function which I've configured as the 'custom auth' on the method request of one of my API endpoints. When I use the 'test' function of the AWS API Gateway I don't see any output from my Lambda function in the log output.
I have 'deployed' the API.
However something is happening because when I hit the api endpoint using the configured custom domain name I get
{"message":"Unauthorized"}
However if I remove the 'custom auth' from this endpoint and hit the same endpoint, it works !
I've enabled the cloudwatch logging and this seems to show that the lambda function is not invoked but there's nothing under the apiGateway log group either, but, something must be happening, I just can't see it.
Can anybody point me in the direction of how I debug this ?
The API Gateway Test Invoke functionality will NOT invoke any authorizers, and will directly invoke your integration.
You can test the lambda authorizer independently by using the Authorizer Test Invoke available in the "Authorizers" tab on the API Gateway Console.
Your lambda function must return a response that includes the principal identifier (principalId) and a policy document containing a list of policy statements.
A more detailed documentation on this can be found here.
Ritisha.
You can add permissions via aws cli to make you authorizer call the lambda, i did this and works perfectly!.
aws --profile profile lambda add-permission \
--statement-id uuid \
--action lambda:InvokeFunction \
--function-name "arn:aws:lambda:$region:$accountId:function:functionName" \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:$region:$accountId:$apigateway_id/authorizers/$authorizerId"

Resources