I'm trying to save an item in DynamoDB and send an OTP from the same function. I'm able to save the data but not able to receive the OTP SMS. I guess my code is exiting the function before the SMS is sent.
I'm able to save and send SMS in two separate lambda functions but unable to merge both the logic in one. I don't know what I'm doing wrong.
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
DynamoDB dynamoDb = new DynamoDB(client);
try {
JSONObject event = (JSONObject) parser.parse(reader);
if (event.get("body") != null) {
Random rnd = new Random();
String otp = String.format("%06d", rnd.nextInt(999999));
Order order = new Order((String) event.get("body"));
order.setOtp(otp);
String message = otp +" is your OTP for example.com";
AmazonSNSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)))
.build()
.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(order.getMobile()).withMessageAttributes(smsAttributes));
dynamoDb.getTable(DYNAMODB_TABLE_NAME).putItem(new PutItemSpec().withItem(
new Item()
.withString("orderid", order.getOrderid())
.withString("mobile", order.getMobile())
.withString("otp", order.getOtp())));
}
JSONObject responseBody = new JSONObject();
responseBody.put("message", "OTP Sent !!");
JSONObject headerJson = new JSONObject();
responseJson.put("statusCode", 200);
responseJson.put("headers", headerJson);
responseJson.put("body", responseBody.toString());
} catch (ParseException pex) {
responseJson.put("statusCode", 400);
responseJson.put("exception", pex);
}