aws-sam-cli
aws-sam-cli copied to clipboard
Support for context.function_name?
Description: I want a lambda to call itself. I am using an expression inside a python programme FunctionName=context.function_name
yet, it returns ResourceNotFoundException error as there is no such a function, actually somehow (why?) named test.. with the arn (as example) arn:aws:lambda::eu-west-1:104260451092:function:test.
If I set an enviroment variable: AWS_LAMBDA_FUNCTION_NAME inside template, it will work "almost" ok, but it will call aws account lambda, not the local one which I test.
So how to reference (the function name) of the locally invoked lambda function, so it will call itself ?
I thought it will be a name such as a copy of a main handler file - in my case name (name.py)
but no, as again - An error occurred (ResourceNotFoundException) when calling the Invoke operation: Function not found: arn:aws:lambda:eu-west-1:104260451092:function:main
Output of sam --version
:
SAM CLI, version 0.6.0
Hope there is still a way to pass a function name to the local lambda, not to call the aws one, but to call itself.
@aniecki You can call Lambda's locally through the sam local start-lambda
command. You can find some details on how to configure boto3 (other SDK's follow similar patterns) here.
If you are using Windows or Mac, you will need to configure the endpoint to be host.docker.internal:3001
. This will allows docker to talk to the localhost endpoint on your machine. Docker for Linux does not yet support this. If you are on that system, you can either attach the container to the host
network, which will allow you to directly use localhost:3001
(or the port sam local start-lambda
is configured to).
sam local start-lambda
only supports the FunctionName
being the LogicalId of the function in the template.
Also it is not clear to me (can't remember) the reason why we don't add the FunctionName to the env var. This could be due to we don't support or understand the Name
Property of the function. But this should't stop what you are trying to do.
@jfuss Any change you can expand on this ?
I'm running aws-sam-cli in debug-api mode, and trying to make one lambda call another lambda.
Here's my Template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
derp-serverless
SAM Template for derp-serverless
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 5
Resources:
UserFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: bin/
Handler: getUser
Runtime: go1.x
Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
Events:
CatchAll:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /user/{userId+}
Method: GET
cors: true
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
PARAM1: VALUE
DerpRepoFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: bin/
Handler: derpRepo
Runtime: go1.x
Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
And running in a Lamda:
sessionNew, err := session.NewSession()
if err != nil {
fmt.Print(err)
}
svc := lambdaSDK.New(sessionNew)
input := &lambdaSDK.InvokeInput{
FunctionName: aws.String("DerpRepoFunction"),
}
result, err := svc.Invoke(input)
if err != nil {
...
But I keep having the same ResourceNotFoundException
error. Can you point me in the right direction ?
I have the same use case as @ aniecki: I want to call a lambda function from itself, so I need a way to find out the name of the function currently executing. I want this to work both with sam local start-lambda
and in production. The context.function_name
value is not correct in local invocation. I also tried to create an environment variable manually in template.yml
:
Environment:
Variables:
JMY_LAMBDA_FUNCTION_NAME:
!GetAtt MyLambdaFunction.Arn
However, this doesn't work due to a circular dependency of the function to itself.
@jfuss: What is the recommended way to do this? Any plans to add the function name to the environment? For me, providing the correct value in context.function_name
would also work fine.
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
I am still interested in a solution.
same here
This is related to that right? https://github.com/aws/aws-sam-cli/issues/1177
So I was looking into this more. With recent changes (say last year or two), context.function_name
should work those this will default to the LogicalId of the function. If we changed https://github.com/aws/aws-sam-cli/blob/develop/samcli/commands/local/lib/local_lambda.py#L288 to function.functionname
it should handle adding the FunctionName if provided in the template else default to the logicalId (like we do today).
It's a pretty small change and just needs a couple tests with it. If someone is willing to contribute, we would be happy to review and get this patched.