Skip to content

Commit b50dfd3

Browse files
author
Serkan ÖZAL
authored
implement empty methods in LambdaContext (#66)
1 parent 9d22eb7 commit b50dfd3

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-39
lines changed

src/main/java/cloud/localstack/LambdaContext.java

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,116 @@
55
import com.amazonaws.services.lambda.runtime.Context;
66
import com.amazonaws.services.lambda.runtime.LambdaLogger;
77

8+
import java.text.SimpleDateFormat;
9+
import java.util.Date;
10+
import java.util.UUID;
811
import java.util.logging.Level;
912
import java.util.logging.Logger;
1013

1114
public class LambdaContext implements Context {
1215

13-
private final Logger LOG = Logger.getLogger(LambdaContext.class.getName());
16+
private static final int DEFAULT_MEMORY_SIZE_IN_MB = 256;
17+
private static final String DEFAULT_ACCOUNT_ID = "123456789012";
18+
private static final String DEFAULT_REGION = "us-east-1";
19+
private static final String DEFAULT_FUNCTION_NAME = "localstack";
20+
private static final String DEFAULT_FUNCTION_VERSION = "$LATEST";
1421

15-
public LambdaLogger getLogger() {
16-
return new LambdaLogger() {
22+
private static final String TODAY = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
23+
private static final String CONTAINER_ID = UUID.randomUUID().toString();
1724

18-
@Override
19-
public void log(String msg) {
20-
LOG.log(Level.INFO, msg);
21-
}
25+
private final Logger LOG = Logger.getLogger(LambdaContext.class.getName());
2226

23-
@Override
24-
public void log(byte[] msg) {
25-
log(new String(msg));
26-
}
27-
};
28-
}
27+
private final String requestId;
2928

30-
public String getAwsRequestId() {
31-
// TODO Auto-generated method stub
32-
return null;
29+
public LambdaContext(String requestId) {
30+
this.requestId = requestId;
3331
}
3432

35-
public ClientContext getClientContext() {
36-
// TODO Auto-generated method stub
37-
return null;
33+
@Override
34+
public String getAwsRequestId() {
35+
return requestId;
3836
}
3937

38+
@Override
4039
public String getFunctionName() {
41-
// TODO Auto-generated method stub
42-
return null;
40+
String functionName = System.getenv("AWS_LAMBDA_FUNCTION_NAME");
41+
if (functionName == null) {
42+
functionName = DEFAULT_FUNCTION_NAME;
43+
}
44+
return functionName;
4345
}
4446

47+
@Override
4548
public String getFunctionVersion() {
46-
// TODO Auto-generated method stub
47-
return null;
48-
}
49-
50-
public CognitoIdentity getIdentity() {
51-
// TODO Auto-generated method stub
52-
return null;
49+
String functionVersion = System.getenv("AWS_LAMBDA_FUNCTION_VERSION");
50+
if (functionVersion == null) {
51+
functionVersion = DEFAULT_FUNCTION_VERSION;
52+
}
53+
return functionVersion;
5354
}
5455

56+
@Override
5557
public String getInvokedFunctionArn() {
56-
// TODO Auto-generated method stub
57-
return null;
58+
String region = System.getenv("AWS_REGION");
59+
if (region == null) {
60+
region = DEFAULT_REGION;
61+
}
62+
return String.format("arn:aws:%s:%s:function:%s:%s",
63+
region, DEFAULT_ACCOUNT_ID, getFunctionName(), getFunctionVersion());
5864
}
5965

66+
@Override
6067
public String getLogGroupName() {
61-
// TODO Auto-generated method stub
62-
return null;
68+
return String.format("/aws/lambda/%s", getFunctionName());
6369
}
6470

71+
@Override
6572
public String getLogStreamName() {
66-
// TODO Auto-generated method stub
67-
return null;
73+
return String.format("%s[%s]%s", TODAY, getFunctionVersion(), CONTAINER_ID);
6874
}
6975

76+
@Override
7077
public int getMemoryLimitInMB() {
71-
// TODO Auto-generated method stub
72-
return 0;
78+
String memorySize = System.getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE");
79+
if (memorySize == null) {
80+
return DEFAULT_MEMORY_SIZE_IN_MB;
81+
} else {
82+
try {
83+
return Integer.parseInt(memorySize);
84+
} catch (NumberFormatException e) {
85+
return DEFAULT_MEMORY_SIZE_IN_MB;
86+
}
87+
}
7388
}
7489

90+
@Override
7591
public int getRemainingTimeInMillis() {
76-
// TODO Auto-generated method stub
77-
return 0;
92+
return Integer.MAX_VALUE;
93+
}
94+
95+
@Override
96+
public LambdaLogger getLogger() {
97+
return new LambdaLogger() {
98+
@Override
99+
public void log(String msg) {
100+
LOG.log(Level.INFO, msg);
101+
}
102+
103+
@Override
104+
public void log(byte[] msg) {
105+
log(new String(msg));
106+
}
107+
};
108+
}
109+
110+
@Override
111+
public ClientContext getClientContext() {
112+
return null;
113+
}
114+
115+
@Override
116+
public CognitoIdentity getIdentity() {
117+
return null;
78118
}
79119

80120
}

src/main/java/cloud/localstack/LambdaExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Optional;
32+
import java.util.UUID;
3233
import java.util.stream.Collectors;
3334

3435
/**
@@ -89,7 +90,7 @@ public static void main(String[] args) throws Exception {
8990
}
9091
}
9192

92-
Context ctx = new LambdaContext();
93+
Context ctx = new LambdaContext(UUID.randomUUID().toString());
9394
if (handler instanceof RequestHandler) {
9495
Object result = ((RequestHandler<Object, ?>) handler).handleRequest(inputObject, ctx);
9596
// try turning the output into json

0 commit comments

Comments
 (0)