Skip to content

Commit 46983db

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 2ca3e4a commit 46983db

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
@@ -145,6 +145,11 @@ The rest of these Environment Variables can be set to match AWS Lambda's environ
145145
* `AWS_LAMBDA_FUNCTION_NAME`
146146
* `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`
147147
148+
By default `aws-lambda-rie` sets the value of the `AWS_LAMBDA_FUNCTION_NAME` environment variable to `test_function`, while the
149+
function name in the endpoint URL is `function`. If you want the RIE to behave like AWS Lambda, where the function name in the
150+
endpoint matches the value of the environment variable, set the value of the `AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR`
151+
environment variable to `"FALSE"`.
152+
148153
## Level of support
149154
150155
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
@@ -39,6 +39,14 @@ func GetenvWithDefault(key string, defaultValue string) string {
3939
return envValue
4040
}
4141

42+
func GetFunctionName() string {
43+
defaultValue := "function"
44+
if GetenvWithDefault("AWS_LAMBDA_RIE_INCONSISTENT_BEHAVIOUR", "TRUE") == "TRUE" {
45+
defaultValue = "test_function"
46+
}
47+
return GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", defaultValue)
48+
}
49+
4250
func printEndReports(invokeId string, initDuration string, memorySize string, invokeStart time.Time, timeoutDuration time.Duration) {
4351
// Calcuation invoke duration
4452
invokeDuration := math.Min(float64(time.Now().Sub(invokeStart).Nanoseconds()),
@@ -95,7 +103,7 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox) {
95103
invokeStart := time.Now()
96104
invokePayload := &interop.Invoke{
97105
ID: uuid.New().String(),
98-
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function")),
106+
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetFunctionName()),
99107
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
100108
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
101109
Payload: bytes.NewReader(bodyBytes),
@@ -175,7 +183,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
175183
additionalFunctionEnvironmentVariables["AWS_LAMBDA_LOG_STREAM_NAME"] = "$LATEST"
176184
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_VERSION"] = "$LATEST"
177185
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
178-
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = "test_function"
186+
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = GetFunctionName()
179187

180188
// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
181189
// not be viewable when the function runs.
@@ -194,7 +202,7 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
194202
AwsSecret: os.Getenv("AWS_SECRET_ACCESS_KEY"),
195203
AwsSession: os.Getenv("AWS_SESSION_TOKEN"),
196204
XRayDaemonAddress: "0.0.0.0:0", // TODO
197-
FunctionName: GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function"),
205+
FunctionName: GetFunctionName(),
198206
FunctionVersion: functionVersion,
199207

200208
CustomerEnvironmentVariables: additionalFunctionEnvironmentVariables,

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)