Bad handler when deploying Lambda function via CloudFormation ZipFile - amazon-web-services

I am deploying a Lambda function (Python 3.6) named lambda_handler using CloudFormation by specifying the code in-line as described here.
The Lambda deploys properly, however when testing the function I see a "Bad Handler" exception.
What is the correct way to refer to the handler when passing the Handler parameter to Lambda?

Use the handler index.lambda_handler. Since there is no deployment package name when submitting in-line code via ZipFile, the prefix index is used instead.

Related

API Gateway not passing event to lambda from console?

I'm trying to call a basic lambda from my API Gateway console. The lambda has an input taken in by the event called filterBy.
I created a query string called filterBy however, when I try to invoke the lambda I get the error:
{errorMessage: {'statusCode': 500, 'body': '{"msg": "KeyError(\'filterBy\')"}'}}
Presumably because the piece of code in my lambda
event['filterBy'] is not finding a filterBy in the event. What do I need to do so that I can get the filterBy in the event from the API Gateway console? I understand this is probably quite simple but I surprisingly cannot find anything about this so any help would be appreciated.
Based on the integration type, approach can be vary.
1. Lambda custom integrations:
Looks like you are trying to use Lambda custom integrations. If that is the case you have to add a mapping template as below.
(under the Integration Request -> Mapping Templates --> Add mapping template)
{
"filterBy": "$input.params('filterBy')"
}
Please refer this article or this video for more info.
2. Lambda proxy integrations:
If you are using Lambada proxy integration (either as REST API or as HTTP APIs), then instead of event['filterBy'], you have to access the queryStringParameters first and then access the relevant query param.
event['queryStringParameters']['filterBy']
And another thing is: once you modify something in API GW, please make sure to deploy and wait some time before test. :)

Using api call result as a parameter for CloudFormation resource

Is there anyway to make CloudFormation parameter dynamic? I know about the System Manager Parameter, but again I have to change its value manually. I want to use somehow the result of the API call or script(Bash, python) in my CloudFormation resources
for example, as part of the parameter, run a API call to get back some data (any data) and then use/reference the result into the resources, and all in one template.
You can use Cloudformation Custom resource to achieve similar effect, with some caveats.
As an example we can use AWS CDK, which provides a module to create custom resources, and even has a wrapper specifically designed to call AWS API and return the results: https://docs.aws.amazon.com/cdk/api/latest/docs/#aws-cdk_custom-resources.AwsSdkCall.html
Some things to remember:
Custom resource needs to return value in form {'PhysicalResourceId': ..., Data: {"MyAttribute": ...}} in order to support using !GetAtt MyResource.MyAttribute style of reference
Like any other CF resource, Custom resource is not triggered on every update, only if one of the parameters of the resource has changed. So if you supplied some parameter to your API call on stack creation, unless you change value, no update will happen and API call will not be triggered.

aws Lex UI does not show lambda function from other account

My colleague created a lex chatbot. I have been working on a lambda function that queries an external database they want to use in their bot. I created an intent to access the function and then exported the intent. I set up the AWS IAM service role for amazon lex and created resource permissions using this (https://docs.aws.amazon.com/lambda/latest/dg/services-lex.html). Though to make it work I did --action lambda:* Still, while the import completes without error, the lambda function does not seem to have been imported into the intent. When the intent is added to a bot, the lambda function is blank and the drop-down menu does not show my lambda function as an option. Is there a way to make the function accessible in my colleague's account in setting up the intent?
There should be an easier way to do this, but this is the workflow that at least works, even if it is not elegant:
create lambda function and export as zip (I use the python-lambda library)
other user creates an empty lambda function (same name and python version)
other user imports zip in labmda
Create an intent in Lex and export (do not put the lambda function in this)
other user imports the intent
other user modifies the fulfillment section of the intent to include the lambda function they just created.
completely kludgy but does work. Still looking for another way to make it work.

Error when calling LambdaInvoke in aws CDK stepfunction

I am trying to create a stepfunction via AWS CDK.
The stepfunctions should, among other things, call a lambda function.
I am using the lambdaInvoke task as described in: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_stepfunctions_tasks/LambdaInvoke.html
For the lambda_function parameter I am trying to use an existing Lambda by referencing its ARN using
https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_lambda/Function.html#aws_cdk.aws_lambda.Function.from_function_arn
(i tried using from_function_attributes as well)
My cdk code looks like this
lambda_name = sfn.Task(
self, "Lambda Name",
task=sfn_tasks.LambdaInvoke(self, "lambdaInvoke",lambda_function=lambda_.Function.from_function_arn(self,"import-lambda",function_arn="arn:aws:lambda:eu-west-1:accountnumber:function:LambdaName")),
result_path="$.guid"
)
I am trying to call an existing lambda. (the lambda itself is created and updated in a pipeline setup in the same CDK project, but in a different stack)
I also tried adding :%LATEST to LambdaName, and both give me the same error:
jsii.errors.JSIIError: props.task.bind is not a function
I made a support ticket for the issue, they ran into the same issue and referred me to here.
How do I invoke a lambda via cdk?
Update:
LambdaInvoke is not supposed to be imbedded in a task.
lambda_name = sfn_tasks.LambdaInvoke(
self, "lambdaInvoke",
lambda_function=lambda_.Function.from_function_arn(self,"import-lambda",function_arn="arn:aws:lambda:eu-west-1:accountnumber:function:LambdaName")),
result_path="$.guid"
)
Works

Amazon S3 object event creation

my AWS S3 bucket is associated with a Lambda function. Lambda is triggered by file insertion with certain file name. There is a XML file inside the same bucket from which lambda function reads the setting. The problem I am facing is that, when ever the settings file is not present/settings in the XML is wrong, the lambda launch will fail. But when the settings are made correct, lambda will get triggered for old files as well, where lambda got failed previously. I don't want trigger the Lambda again, once its failed for same file. Can some one direct me how to do that?
Take a look at the docs here:
http://aws.amazon.com/lambda/faqs/
Lambda will attempt to execute your event at least 3 times before it gives up.
If you can handle the errors in your function (related to reading the config file), so that the Lambda Function exists with out error, then Lambda will not (normally) run your function again.