Skip to content

Commit

Permalink
allow "invoked function arn" to be specified from env vars (lambci#98)
Browse files Browse the repository at this point in the history
This is to support the use case of invoking lambda using a specific
alias. In this case, the arn is expected to contain the alias, while
the function version should still point to the actual version.

Currently the invoked arn is always derived from the function name +
version, so it's not possible to support the above use case.

Contributed by @quantcast
  • Loading branch information
wsee authored and mhart committed Jun 30, 2018
1 parent 5ddbd13 commit 6c21390
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Docker tags (follow the Lambda runtime names):
Env vars:
- `AWS_LAMBDA_FUNCTION_NAME`
- `AWS_LAMBDA_FUNCTION_VERSION`
- `AWS_LAMBDA_FUNCTION_INVOKED_ARN`
- `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`
- `AWS_LAMBDA_FUNCTION_TIMEOUT`
- `AWS_LAMBDA_FUNCTION_HANDLER`
Expand Down
2 changes: 1 addition & 1 deletion dotnetcore2.0/run/MockBootstraps/MockLambdaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public string OutputText

public string AccountId => EnvHelper.GetOrDefault("AWS_ACCOUNT_ID", "000000000000");

public string Arn => $"arn:aws:lambda:{Region}:{AccountId}:function:{FunctionName}";
public string Arn => EnvHelper.GetOrDefault("AWS_LAMBDA_FUNCTION_INVOKED_ARN", $"arn:aws:lambda:{Region}:{AccountId}:function:{FunctionName}");

string RandomLogStreamName => $"{DateTime.Now.ToString("yyyy/MM/dd")}/[{FunctionVersion}]{random.Next().ToString("x") + random.Next().ToString("x")}";
}
Expand Down
2 changes: 1 addition & 1 deletion go1.x/run/aws-lambda-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (mc *MockLambdaContext) Request() *messages.InvokeRequest {
Payload: []byte(mc.EventBody),
RequestId: mc.RequestId,
XAmznTraceId: getEnv("_X_AMZN_TRACE_ID", ""),
InvokedFunctionArn: arn(mc.Region, mc.AccountId, mc.FnName),
InvokedFunctionArn: getEnv("AWS_LAMBDA_FUNCTION_INVOKED_ARN", arn(mc.Region, mc.AccountId, mc.FnName)),
Deadline: messages.InvokeRequest_Timestamp{
Seconds: mc.Deadline().Unix(),
Nanos: int64(mc.Deadline().Nanosecond()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public class LambdaRuntime {
AWS_SESSION_TOKEN = getEnv("AWS_SESSION_TOKEN");
AWS_REGION = getEnvOrDefault("AWS_REGION", getEnvOrDefault("AWS_DEFAULT_REGION", "us-east-1"));
ACCOUNT_ID = getEnvOrDefault("AWS_ACCOUNT_ID", "000000000000");
FUNCTION_ARN = "arn:aws:lambda:" + AWS_REGION + ":" + ACCOUNT_ID + ":function:" + FUNCTION_NAME;
FUNCTION_ARN = getEnvOrDefault("AWS_LAMBDA_FUNCTION_INVOKED_ARN,
"arn:aws:lambda:" + AWS_REGION + ":" + ACCOUNT_ID + ":function:" + FUNCTION_NAME);
X_AMZN_TRACE_ID = getEnvOrDefault("_X_AMZN_TRACE_ID", "");

String[] args = getCmdLineArgs();
Expand Down
3 changes: 2 additions & 1 deletion nodejs/run/awslambda-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var ACCOUNT_ID = process.env.AWS_ACCOUNT_ID || randomAccountId()
var ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'SOME_ACCESS_KEY_ID'
var SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'SOME_SECRET_ACCESS_KEY'
var SESSION_TOKEN = process.env.AWS_SESSION_TOKEN
var INVOKED_ARN = process.env.AWS_LAMBDA_FUNCTION_INVOKED_ARN || arn(REGION, ACCOUNT_ID, FN_NAME)

function consoleLog(str) {
process.stderr.write(formatConsole(str))
Expand Down Expand Up @@ -65,7 +66,7 @@ var OPTIONS = {
cognitoidentityid: undefined,
cognitopoolid: undefined,
},
invokedfunctionarn: arn(REGION, ACCOUNT_ID, FN_NAME),
invokedfunctionarn: INVOKED_ARN,
}

var invoked = false
Expand Down
3 changes: 2 additions & 1 deletion nodejs4.3/run/awslambda-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var ACCOUNT_ID = process.env.AWS_ACCOUNT_ID || randomAccountId()
var ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'SOME_ACCESS_KEY_ID'
var SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'SOME_SECRET_ACCESS_KEY'
var SESSION_TOKEN = process.env.AWS_SESSION_TOKEN
var INVOKED_ARN = process.env.AWS_LAMBDA_FUNCTION_INVOKED_ARN || arn(REGION, ACCOUNT_ID, FN_NAME)

function consoleLog(str) {
process.stderr.write(formatConsole(str))
Expand Down Expand Up @@ -64,7 +65,7 @@ var OPTIONS = {
// cognitoIdentityId: undefined,
// cognitoPoolId: undefined,
},
invokedFunctionArn: arn(REGION, ACCOUNT_ID, FN_NAME),
invokedFunctionArn: INVOKED_ARN,
}

// Some weird spelling error in the source?
Expand Down
3 changes: 2 additions & 1 deletion nodejs6.10/run/awslambda-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var ACCOUNT_ID = process.env.AWS_ACCOUNT_ID || randomAccountId()
var ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'SOME_ACCESS_KEY_ID'
var SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'SOME_SECRET_ACCESS_KEY'
var SESSION_TOKEN = process.env.AWS_SESSION_TOKEN
var INVOKED_ARN = process.env.AWS_LAMBDA_FUNCTION_INVOKED_ARN || arn(REGION, ACCOUNT_ID, FN_NAME)

function consoleLog(str) {
process.stderr.write(formatConsole(str))
Expand Down Expand Up @@ -64,7 +65,7 @@ var OPTIONS = {
// cognitoIdentityId: undefined,
// cognitoPoolId: undefined,
},
invokedFunctionArn: arn(REGION, ACCOUNT_ID, FN_NAME),
invokedFunctionArn: INVOKED_ARN,
}

// Some weird spelling error in the source?
Expand Down
3 changes: 2 additions & 1 deletion nodejs8.10/run/awslambda-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var ACCOUNT_ID = process.env.AWS_ACCOUNT_ID || randomAccountId()
var ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID || 'SOME_ACCESS_KEY_ID'
var SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY || 'SOME_SECRET_ACCESS_KEY'
var SESSION_TOKEN = process.env.AWS_SESSION_TOKEN
var INVOKED_ARN = process.env.AWS_LAMBDA_FUNCTION_INVOKED_ARN || arn(REGION, ACCOUNT_ID, FN_NAME)

function consoleLog(str) {
process.stderr.write(formatConsole(str))
Expand Down Expand Up @@ -64,7 +65,7 @@ var OPTIONS = {
// cognitoIdentityId: undefined,
// cognitoPoolId: undefined,
},
invokedFunctionArn: arn(REGION, ACCOUNT_ID, FN_NAME),
invokedFunctionArn: INVOKED_ARN,
}

// Some weird spelling error in the source?
Expand Down
2 changes: 1 addition & 1 deletion python2.7/run/runtime-mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _arn(region, account_id, fct_name):
'secret': _GLOBAL_SECRET_ACCESS_KEY,
'session': _GLOBAL_SESSION_TOKEN
}
_GLOBAL_INVOKED_FUNCTION_ARN = _arn(_GLOBAL_REGION, _GLOBAL_ACCOUNT_ID, _GLOBAL_FCT_NAME)
_GLOBAL_INVOKED_FUNCTION_ARN = os.environ.get('AWS_LAMBDA_FUNCTION_INVOKED_ARN', _arn(_GLOBAL_REGION, _GLOBAL_ACCOUNT_ID, _GLOBAL_FCT_NAME))
_GLOBAL_XRAY_TRACE_ID = os.environ.get('_X_AMZN_TRACE_ID', None)
_GLOBAL_XRAY_PARENT_ID = None
_GLOBAL_XRAY_SAMPLED = None
Expand Down
2 changes: 1 addition & 1 deletion python3.6/run/runtime-mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _arn(region, account_id, fct_name):
'secret': _GLOBAL_SECRET_ACCESS_KEY,
'session': _GLOBAL_SESSION_TOKEN
}
_GLOBAL_INVOKED_FUNCTION_ARN = _arn(_GLOBAL_REGION, _GLOBAL_ACCOUNT_ID, _GLOBAL_FCT_NAME)
_GLOBAL_INVOKED_FUNCTION_ARN = os.environ.get('AWS_LAMBDA_FUNCTION_INVOKED_ARN', _arn(_GLOBAL_REGION, _GLOBAL_ACCOUNT_ID, _GLOBAL_FCT_NAME))
_GLOBAL_XRAY_TRACE_ID = os.environ.get('_X_AMZN_TRACE_ID', None)
_GLOBAL_XRAY_PARENT_ID = None
_GLOBAL_XRAY_SAMPLED = None
Expand Down

0 comments on commit 6c21390

Please sign in to comment.