forked from markusklems/aws-lambda-java-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaApprovalFunctionHandler.java
More file actions
57 lines (48 loc) · 2.16 KB
/
Copy pathLambdaApprovalFunctionHandler.java
File metadata and controls
57 lines (48 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package lambda;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
/**
*
* @author markusklems
*
*/
public class LambdaApprovalFunctionHandler implements
RequestHandler<Object, Object> {
private static AmazonDynamoDBClient dynamoDB;
@Override
public Object handleRequest(Object input, Context context) {
context.getLogger().log("input: " + input);
if (input.toString().equals("{}") || input.toString().equals("")) {
context.getLogger().log("input is empty: abort");
return "{\"status\":\"error\",\"message\":\"input at lambda function is empty\"}";
}
dynamoDB = new AmazonDynamoDBClient().withRegion(Region
.getRegion(Regions.EU_WEST_1));
HashMap<String, String> mapInput = (HashMap<String, String>) input;
Map<String, AttributeValue> employeeKey = new HashMap<String, AttributeValue>();
String employeeId = mapInput.get("employee_id");
context.getLogger().log("employee_id: " + employeeId);
employeeKey.put("employee_id", new AttributeValue().withS(employeeId));
Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>();
attributeUpdates.put("approval", new AttributeValueUpdate()
.withValue(new AttributeValue().withS("approved")));
UpdateItemRequest updateItemRequest = new UpdateItemRequest()
.withKey(employeeKey).withAttributeUpdates(attributeUpdates)
.withTableName("lambda-reimbursment");
UpdateItemResult updateItemResult = dynamoDB
.updateItem(updateItemRequest);
context.getLogger().log("Result: " + updateItemResult);
return "{'status':'done'}";
}
}