Skip to content

Commit

Permalink
Fix default function name
Browse files Browse the repository at this point in the history
As noted in aws#46, the function name is derived from
the URL used to invoke it. The emulator uses `function`
in the API endpoint, but the `AWS_LAMBDA_FUNCTION_NAME`
environment variable is set to `test_function`. This
is an inconsistency between the emulator and the AWS
environment.
  • Loading branch information
skwashd committed Jul 9, 2024
1 parent 394ab66 commit 42ccdb2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ The rest of these Environment Variables can be set to match AWS Lambda's environ
* `AWS_LAMBDA_FUNCTION_NAME`
* `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`
By default `aws-lambda-rie` sets the value of the `AWS_LAMBDA_FUNCTION_NAME` environment variable to `test_function`, while the
function name in the endpoint URL is `function`. If you want the RIE to behave like AWS Lambda, where the function name in the
endpoint matches the value of the environment variable, set the value of the `AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR`
environment variable to `"FALSE"`.
## Level of support
You can use the emulator to test if your function code is compatible with the Lambda environment, executes successfully
Expand Down
14 changes: 11 additions & 3 deletions cmd/aws-lambda-rie/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func GetenvWithDefault(key string, defaultValue string) string {
return envValue
}

func GetFunctionName() string {
defaultValue := "function"
if GetenvWithDefault("AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR", "TRUE") == "TRUE" {
defaultValue = "test_function"
}
return GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", defaultValue)
}

func printEndReports(invokeId string, initDuration string, memorySize string, invokeStart time.Time, timeoutDuration time.Duration) {
// Calcuation invoke duration
invokeDuration := math.Min(float64(time.Now().Sub(invokeStart).Nanoseconds()),
Expand Down Expand Up @@ -118,7 +126,7 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs i
invokeStart := time.Now()
invokePayload := &interop.Invoke{
ID: uuid.New().String(),
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function")),
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetFunctionName()),
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
Payload: bytes.NewReader(bodyBytes),
Expand Down Expand Up @@ -198,7 +206,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs inte
additionalFunctionEnvironmentVariables["AWS_LAMBDA_LOG_STREAM_NAME"] = "$LATEST"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_VERSION"] = "$LATEST"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = "test_function"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = GetFunctionName()

// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
// not be viewable when the function runs.
Expand All @@ -216,7 +224,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs inte
AwsSecret: os.Getenv("AWS_SECRET_ACCESS_KEY"),
AwsSession: os.Getenv("AWS_SESSION_TOKEN"),
XRayDaemonAddress: "0.0.0.0:0", // TODO
FunctionName: GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function"),
FunctionName: GetFunctionName(),
FunctionVersion: functionVersion,
RuntimeInfo: interop.RuntimeInfo{
ImageJSON: "{}",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/testdata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def check_env_var_handler(event, context):

def assert_env_var_is_overwritten(event, context):
print(os.environ.get("AWS_LAMBDA_FUNCTION_NAME"))
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME") == "test_function":
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME") == "function":
raise("Function name was not overwritten")
else:
return "My lambda ran succesfully"

def assert_lambda_arn_in_context(event, context):
if context.invoked_function_arn == f"arn:aws:lambda:us-east-1:012345678912:function:{os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test_function')}":
if context.invoked_function_arn == f"arn:aws:lambda:us-east-1:012345678912:function:{os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'function')}":
return "My lambda ran succesfully"
else:
raise("Function Arn was not there")
Expand Down

0 comments on commit 42ccdb2

Please sign in to comment.