Skip to content

Commit 42ccdb2

Browse files
committed
Fix default function name
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.
1 parent 394ab66 commit 42ccdb2

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ The rest of these Environment Variables can be set to match AWS Lambda's environ
172172
* `AWS_LAMBDA_FUNCTION_NAME`
173173
* `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`
174174
175+
By default `aws-lambda-rie` sets the value of the `AWS_LAMBDA_FUNCTION_NAME` environment variable to `test_function`, while the
176+
function name in the endpoint URL is `function`. If you want the RIE to behave like AWS Lambda, where the function name in the
177+
endpoint matches the value of the environment variable, set the value of the `AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR`
178+
environment variable to `"FALSE"`.
179+
175180
## Level of support
176181
177182
You can use the emulator to test if your function code is compatible with the Lambda environment, executes successfully

cmd/aws-lambda-rie/handlers.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func GetenvWithDefault(key string, defaultValue string) string {
5555
return envValue
5656
}
5757

58+
func GetFunctionName() string {
59+
defaultValue := "function"
60+
if GetenvWithDefault("AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR", "TRUE") == "TRUE" {
61+
defaultValue = "test_function"
62+
}
63+
return GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", defaultValue)
64+
}
65+
5866
func printEndReports(invokeId string, initDuration string, memorySize string, invokeStart time.Time, timeoutDuration time.Duration) {
5967
// Calcuation invoke duration
6068
invokeDuration := math.Min(float64(time.Now().Sub(invokeStart).Nanoseconds()),
@@ -118,7 +126,7 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs i
118126
invokeStart := time.Now()
119127
invokePayload := &interop.Invoke{
120128
ID: uuid.New().String(),
121-
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function")),
129+
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetFunctionName()),
122130
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
123131
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
124132
Payload: bytes.NewReader(bodyBytes),
@@ -198,7 +206,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs inte
198206
additionalFunctionEnvironmentVariables["AWS_LAMBDA_LOG_STREAM_NAME"] = "$LATEST"
199207
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_VERSION"] = "$LATEST"
200208
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
201-
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = "test_function"
209+
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = GetFunctionName()
202210

203211
// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
204212
// not be viewable when the function runs.
@@ -216,7 +224,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs inte
216224
AwsSecret: os.Getenv("AWS_SECRET_ACCESS_KEY"),
217225
AwsSession: os.Getenv("AWS_SESSION_TOKEN"),
218226
XRayDaemonAddress: "0.0.0.0:0", // TODO
219-
FunctionName: GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function"),
227+
FunctionName: GetFunctionName(),
220228
FunctionVersion: functionVersion,
221229
RuntimeInfo: interop.RuntimeInfo{
222230
ImageJSON: "{}",

test/integration/testdata/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def check_env_var_handler(event, context):
2525

2626
def assert_env_var_is_overwritten(event, context):
2727
print(os.environ.get("AWS_LAMBDA_FUNCTION_NAME"))
28-
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME") == "test_function":
28+
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME") == "function":
2929
raise("Function name was not overwritten")
3030
else:
3131
return "My lambda ran succesfully"
3232

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

0 commit comments

Comments
 (0)