aws credentials not found error - amazon-web-services

I have a jar uploaded to aws lambda but it keeps throwing below error:
{
"errorMessage": "java.lang.NullPointerException",
"errorType": "java.lang.NullPointerException",
"stackTrace": [
"com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:143)",
"com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:132)",
"com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:99)",
"com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:135)",
"com.amazonaws.http.AmazonHttpClient.getCredentialsFromContext(AmazonHttpClient.java:802)",
"com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:828)",
"com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:723)",
"com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:475)",
"com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:437)",
"com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:386)",
"com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2074)",
"com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2044)",
"com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1580)",
"com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:85)",
"com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:41)",
"com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:144)",
"augury.api.SaveAuguryApi.handleRequest(SaveAuguryApi.java:46)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:498)"
]
}
and stack trace:
java.lang.NullPointerException: java.lang.NullPointerException
java.lang.NullPointerException
at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:143)
at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:132)
at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:99)
at com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:135)
at com.amazonaws.http.AmazonHttpClient.getCredentialsFromContext(AmazonHttpClient.java:802)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:828)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:723)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:475)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:437)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:386)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2074)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2044)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1580)
at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:85)
at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:41)
at com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:144)
at augury.api.SaveAuguryApi.handleRequest(SaveAuguryApi.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
END RequestId: b2b9807e-6a09-11e6-9873-2588e6cfa497
REPORT RequestId: b2b9807e-6a09-11e6-9873-2588e6cfa497 Duration: 305.85 ms Billed Duration: 400 ms Memory Size: 512 MB Max Memory Used: 61 MB
And also my lambda java code:
package augury.api;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import augury.pojo.AuguryResponse;
import augury.pojo.AuguryResult;
public class SaveAuguryApi implements RequestHandler<AuguryResult, AuguryResponse> {
// Initialize the Log4j logger.
static final Logger log = Logger.getLogger(SaveAuguryApi.class);
static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
static String tableName = "tarot_history";
public AuguryResponse handleRequest(AuguryResult result, Context context) {
String userId = result.getUserId();
List<Integer> tarotIds = result.getTarotIds();
String createTime = result.getCreate_time();
if (log.isDebugEnabled()) {
log.debug("requestId = " + context.getAwsRequestId() + ", userId = " + userId + ", tarotIds = " + tarotIds
+ ", create_time = " + createTime);
}
if (StringUtils.isBlank(userId) || tarotIds == null || tarotIds.isEmpty() || StringUtils.isBlank(createTime)) {
return new AuguryResponse(400, "this request doesn't contain rightful parameters, please check log");
}
Table table = dynamoDB.getTable(tableName);
Item item = new Item();
item.withString("create_time", createTime);
item.withString("user_id", userId);
item.withList("tarot_ids", tarotIds);
item.withInt("id", 1);
table.putItem(item);
return new AuguryResponse(201, "tarot history created");
}
}
i tried but i still couldn't locate the problem. I'm new to aws lambda and I was trying to learn from the link

The example you are looking at assumes you have credential properties saved to a file, which isn't going to be the case in your Lambda environment. To use the IAM role assigned to the Lambda function change this:
static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
To this:
static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());

Related

Error in Cloud Scheduler Job Created From Java SDK

I have a custom training job that I run on a fixed schedule using Cloud Scheduler. When I create the job using either the Python client or gcp, the job runs fine. However, when I create the cloud scheduler job using the Java SDK, the job gets created but it fails. The SUMMARY of the error message I get in Cloud Logging is:
{"#type":"type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished", "jobName":"projects/{my_project_id}/locations/us-central1/jobs/java_job", "status":"INVALID_ARGUMENT", "targetType":"HTTP", "url":"https://us-central1-aiplatform.googleapis.com/v1/projects/{my_project_id}/locations/us-central1/customJobs"}
I looked at the jobs created in gcp, all fields for the three jobs (the one created using python client, the one created using java SDK and the one created directly in gcp) are the same. I cannot figure out why the job created using the Java SDK keeps failing.
Java SDK code:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.cloud.scheduler.v1.Job;
import com.google.cloud.scheduler.v1.LocationName;
import com.google.cloud.scheduler.v1.OAuthToken;
import com.google.protobuf.ByteString;
import com.google.cloud.scheduler.v1.CloudSchedulerClient;
import com.google.cloud.scheduler.v1.HttpMethod;
import com.google.cloud.scheduler.v1.HttpTarget;
public class Temp
{
static String projectId = "...";
static String location = "...";
static String serviceAccountEmail = "...-compute#developer.gserviceaccount.com";
static String outputUriPrefix = "gs://.../.../";
static String imageUri = String.format("%s-docker.pkg.dev/%s/.../...", location, projectId);
static String trainingJobName = "custom_training_job";
static String schedulerJobName = String.format("projects/%s/locations/%s/jobs/java_job", projectId, location);
static String scope = "https://www.googleapis.com/auth/cloud-platform";
static String httpTargetUri = String.format("https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/customJobs",
location, projectId, location);
static String machineType = "n1-standard-4";
static long replicaCount = 1;
static String getJobBody() throws JSONException {
JSONObject jobBody = new JSONObject();
jobBody.put("display_name", trainingJobName);
JSONObject base_output_directory = new JSONObject();
base_output_directory.put("output_uri_prefix", outputUriPrefix);
jobBody.put("base_output_directory", base_output_directory);
JSONObject jobSpec = new JSONObject();
JSONArray worker_pool_specs = new JSONArray();
JSONObject spec = new JSONObject();
spec.put("replica_count", replicaCount);
JSONObject machine_spec = new JSONObject();
machine_spec.put("machine_type", machineType);
spec.put("machine_spec", machine_spec);
JSONObject container_spec = new JSONObject();
container_spec.put( "image_uri", imageUri);
JSONArray args = new JSONArray();
args.put("--msg=hello!");
container_spec.put( "args", args);
spec.put("container_spec", container_spec);
worker_pool_specs.put(spec);
jobSpec.put("worker_pool_specs", worker_pool_specs);
jobBody.put("job_spec", jobSpec);
return jobBody.toString();
}
public static void main( String[] args ) throws IOException, JSONException
{
System.out.println(String.format("=======STARTING APPLICATION, version %s =======", "v5"));
CloudSchedulerClient client = CloudSchedulerClient.create();
String parent = LocationName.of(projectId, location).toString();
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-Agent", "Google-Cloud-Scheduler");
headers.put("Content-Type", "application/json; charset=utf-8");
OAuthToken token = OAuthToken.newBuilder()
.setServiceAccountEmail(serviceAccountEmail)
.setScope(scope)
.build();
HttpTarget httpTarget = HttpTarget.newBuilder()
.setUri(httpTargetUri)
.setHttpMethod(HttpMethod.POST)
.putAllHeaders(headers)
.setBody(ByteString.copyFromUtf8(getJobBody()))
.setOauthToken(token)
.build();
Job job = Job.newBuilder()
.setName(schedulerJobName)
.setDescription("test java job")
.setSchedule("* * * * *")
.setTimeZone("Africa/Abidjan")
.setHttpTarget(httpTarget)
.build();
client.createJob(parent, job);
client.close();
}
}
Python Client code:
from google.cloud import scheduler
import json
project_id = "..."
location = "..."
service_account_email = "...-compute#developer.gserviceaccount.com"
output_uri_prefix="gs://.../.../"
image_uri=f'{location}-docker.pkg.dev/{project_id}/.../...'
traning_job__name ="custom_training_job"
scheduler_job_name = f'projects/{project_id}/locations/{location}/jobs/python_job'
scope = "https://www.googleapis.com/auth/cloud-platform"
http_target_uri = f'https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/customJobs'
machine_type = "n1-standard-4"
replica_count = 1
job_spec = {
"display_name": traning_job__name,
"job_spec": {
"worker_pool_specs": [
{
"machine_spec": {
"machine_type": machine_type,
},
"replica_count": replica_count,
"container_spec": {
"image_uri": image_uri,
"args": [
"--msg=hello!"
]
}
}
],
"base_output_directory": {
"output_uri_prefix": output_uri_prefix
}
}
}
job = {
"name": scheduler_job_name,
"description": "Created from Python client",
"http_target": {
"uri": http_target_uri,
"http_method": "POST",
"headers": {
"User-Agent": "Google-Cloud-Scheduler",
"Content-Type": "application/json; charset=utf-8"
},
"body": json.dumps(job_spec).encode('utf-8'),
"oauth_token": {
"service_account_email": service_account_email,
"scope": scope
}
},
"schedule": "* * * * *",
"time_zone": "Africa/Abidjan"
}
client = scheduler.CloudSchedulerClient()
parent = f'projects/{project_id}/locations/{location}'
response = client.create_job(parent = parent, job = job)
EDIT
The problem was that in the getJobBody function, I was setting base_output_directory as a top level field, whereas it should be a nested field inside the job_spec. The problem is solved but is there a better way to do this? I know there is a CustomJobSpec class, but could not find a way to convert it into a Json style string.
As mentioned in the edit, the problem was that in the getJobBody function, the base_output_directory was being set as a top level field, whereas it should be a nested field inside the job_spec. So currently, as far as I know, the way to avoid this mistake is to set the jobBody carefully, I don't know of a way to do this in a more structured manner.

Amazon S3 files sharing

In my project there is a need for creating share link for external users without aws user from my researching found out a couple ways for doing so
Bucket policy based on tag
Lambda that creates sign url every time some user request the file
The question is what is the best practice for doing so
I need the download to be available until the user sharing the file stopes it
Thank guys for any answers
Using the AWS SDK, you can use Amazon S3 Pre-sign functionality. You can perform this task in any of the supported programming languages (Java, JS, Python, etc).
The following code shows how to sign an object via the Amazon S3 Java V2 API.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.time.Duration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.utils.IoUtils;
// snippet-end:[presigned.java2.getobjectpresigned.import]
/**
* To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class GetObjectPresignedUrl {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" GetObjectPresignedUrl <bucketName> <keyName> \n\n" +
"Where:\n" +
" bucketName - the Amazon S3 bucket name. \n\n"+
" keyName - a key name that represents a text file. \n\n";
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String keyName = args[1];
Region region = Region.US_WEST_2;
S3Presigner presigner = S3Presigner.builder()
.region(region)
.build();
getPresignedUrl(presigner, bucketName, keyName);
presigner.close();
}
// snippet-start:[presigned.java2.getobjectpresigned.main]
public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName ) {
try {
GetObjectRequest getObjectRequest =
GetObjectRequest.builder()
.bucket(bucketName)
.key(keyName)
.build();
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(getObjectRequest)
.build();
// Generate the presigned request
PresignedGetObjectRequest presignedGetObjectRequest =
presigner.presignGetObject(getObjectPresignRequest);
// Log the presigned URL
System.out.println("Presigned URL: " + presignedGetObjectRequest.url());
HttpURLConnection connection = (HttpURLConnection) presignedGetObjectRequest.url().openConnection();
presignedGetObjectRequest.httpRequest().headers().forEach((header, values) -> {
values.forEach(value -> {
connection.addRequestProperty(header, value);
});
});
// Send any request payload that the service needs (not needed when isBrowserExecutable is true)
if (presignedGetObjectRequest.signedPayload().isPresent()) {
connection.setDoOutput(true);
try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream();
OutputStream httpOutputStream = connection.getOutputStream()) {
IoUtils.copy(signedPayload, httpOutputStream);
}
}
// Download the result of executing the request
try (InputStream content = connection.getInputStream()) {
System.out.println("Service returned response: ");
IoUtils.copy(content, System.out);
}
} catch (S3Exception e) {
e.getStackTrace();
} catch (IOException e) {
e.getStackTrace();
}
// snippet-end:[presigned.java2.getobjectpresigned.main]
}
}

Get all the failed executions of a state machine in AWS Step function and execute dynamically. (In java)

I want to fetch all the failed executions and need to re-trigger them dynamically.
PS: In stepfunction definition I had proper retry mechanism, now I want to rerun the failed executions dynamically.
I need to implement it in java. Please help me with the approach.
Thanks in advance.
You can use the AWS Step Functions API to get a list of excutions:
https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/SfnClient.html#listExecutions-
Then you can get a list of ExecutionListItem by calling the executions() method that belongs to the ListExecutionsResponse object (returned by the listExecutions method)
https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html
Using this object - you can do two things:
1 - check status - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionStatus.html
2 - get state machine ARN value - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html#stateMachineArn--
Using the state machine ARN value, you can execute a state machine with the AWS Step Functions Java API V2:
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sfn.SfnClient;
import software.amazon.awssdk.services.sfn.model.*;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;
// snippet-end:[stepfunctions.java2.start_execute.import]
public class StartExecution {
public static void main(String[] args) {
final String USAGE = "\n" +
"Usage:\n" +
" StartExecution <stateMachineArn> <jsonFile>\n\n" +
"Where:\n" +
" stateMachineArn - the ARN of the state machine.\n\n" +
" jsonFile - A JSON file that contains the values to pass to the worflow.\n" ;
if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}
String stateMachineArn = args[0];
String jsonFile = args[1];
Region region = Region.US_EAST_1;
SfnClient sfnClient = SfnClient.builder()
.region(region)
.build();
String exeArn = startWorkflow(sfnClient,stateMachineArn, jsonFile);
System.out.println("The execution ARN is" +exeArn);
sfnClient.close();
}
// snippet-start:[stepfunctions.java2.start_execute.main]
public static String startWorkflow(SfnClient sfnClient, String stateMachineArn, String jsonFile) {
String json = getJSONString(jsonFile);
// Specify the name of the execution by using a GUID value.
UUID uuid = UUID.randomUUID();
String uuidValue = uuid.toString();
try {
StartExecutionRequest executionRequest = StartExecutionRequest.builder()
.input(json)
.stateMachineArn(stateMachineArn)
.name(uuidValue)
.build();
StartExecutionResponse response = sfnClient.startExecution(executionRequest);
return response.executionArn();
} catch (SfnException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
private static String getJSONString(String path) {
try {
JSONParser parser = new JSONParser();
JSONObject data = (JSONObject) parser.parse(new FileReader(path));//path to the JSON file.
String json = data.toJSONString();
return json;
} catch (IOException | org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
return "";
}
// snippet-end:[stepfunctions.java2.start_execute.main]
}

List resource record sets from hosted zone in route53 giving a very weird error

My purpose is to list all records available in all hosted zones. I created a listResourceRecordSetsRequest builder object and pass it through the response method to retrieve all the resource records but for some reason I get an error.
My code which is wrong and causing the error:
The line which starts with def response = .... is what's giving an error
ArrayList<ResourceRecordSet> getResourceRecords(HostedZone hostedZone){
def request = ListResourceRecordSetsRequest.builder().hostedZoneId(hostedZone.id()).maxItems("1000").build()
def response = route53Client.listResourceRecordSets(request as Consumer<ListResourceRecordSetsRequest.Builder>)
return response.resourceRecordSets()
}
Error:
groovy.lang.MissingMethodException: No signature of method: software.amazon.awssdk.services.route53.model.ListResourceRecordSetsRequest.accept() is applicable for argument types: (software.amazon.awssdk.services.route53.model.ListResourceRecordSetsRequest$BuilderImpl) values: [software.amazon.awssdk.services.route53.model.ListResourceRecordSetsRequest$BuilderImpl#56eafaa0]
I appreciate the help in advance!
What AWS SDK are you using - V2? I just tested this in AWS Java V2 API and it works fine.
Full Java V2 example.
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.*;
import java.util.List;
public class ListResourceRecordSets {
public static void main(String[] args) {
Region region = Region.AWS_GLOBAL;
Route53Client route53Client = Route53Client.builder()
.region(region)
.build();
listResourceRecord(route53Client);
route53Client.close();
}
public static void listResourceRecord(Route53Client route53Client) {
try {
ListResourceRecordSetsRequest request = ListResourceRecordSetsRequest.builder()
.hostedZoneId("XXXXX5442QZ69T8EEX5SZ")
.maxItems("12")
.build();
ListResourceRecordSetsResponse listResourceRecordSets = route53Client.listResourceRecordSets(request);
List<ResourceRecordSet> records = listResourceRecordSets.resourceRecordSets();
for (ResourceRecordSet record : records) {
System.out.println("The Record name is: " + record.name());
}
} catch (Route53Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}

Error: Could not find JobId Service: AmazonRekognition; Status Code: 400;

I am using this Java Lambda code provided by AWS to detect labels in a video:
https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/code_examples/java_examples/stored_video/java-lambda-handler-sns.java
//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import java.util.List;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.GetLabelDetectionRequest;
import com.amazonaws.services.rekognition.model.GetLabelDetectionResult;
import com.amazonaws.services.rekognition.model.LabelDetection;
import com.amazonaws.services.rekognition.model.LabelDetectionSortBy;
import com.amazonaws.services.rekognition.model.VideoMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JobCompletionHandler implements RequestHandler<SNSEvent, String> {
#Override
public String handleRequest(SNSEvent event, Context context) {
String message = event.getRecords().get(0).getSNS().getMessage();
LambdaLogger logger = context.getLogger();
// Parse SNS event for analysis results. Log results
try {
ObjectMapper operationResultMapper = new ObjectMapper();
JsonNode jsonResultTree = operationResultMapper.readTree(message);
logger.log("Rekognition Video Operation:=========================");
logger.log("Job id: " + jsonResultTree.get("JobId"));
logger.log("Status : " + jsonResultTree.get("Status"));
logger.log("Job tag : " + jsonResultTree.get("JobTag"));
logger.log("Operation : " + jsonResultTree.get("API"));
if (jsonResultTree.get("API").asText().equals("StartLabelDetection")) {
if (jsonResultTree.get("Status").asText().equals("SUCCEEDED")){
GetResultsLabels(jsonResultTree.get("JobId").asText(), context);
}
else{
String errorMessage = "Video analysis failed for job "
+ jsonResultTree.get("JobId")
+ "State " + jsonResultTree.get("Status");
throw new Exception(errorMessage);
}
} else
logger.log("Operation not StartLabelDetection");
} catch (Exception e) {
logger.log("Error: " + e.getMessage());
throw new RuntimeException (e);
}
return message;
}
void GetResultsLabels(String startJobId, Context context) throws Exception {
LambdaLogger logger = context.getLogger();
AmazonRekognition rek = AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
int maxResults = 1000;
String paginationToken = null;
GetLabelDetectionResult labelDetectionResult = null;
String labels = "";
Integer labelsCount = 0;
String label = "";
String currentLabel = "";
//Get label detection results and log them.
do {
GetLabelDetectionRequest labelDetectionRequest = new GetLabelDetectionRequest().withJobId(startJobId)
.withSortBy(LabelDetectionSortBy.NAME).withMaxResults(maxResults).withNextToken(paginationToken);
labelDetectionResult = rek.getLabelDetection(labelDetectionRequest);
paginationToken = labelDetectionResult.getNextToken();
VideoMetadata videoMetaData = labelDetectionResult.getVideoMetadata();
// Add labels to log
List<LabelDetection> detectedLabels = labelDetectionResult.getLabels();
for (LabelDetection detectedLabel : detectedLabels) {
label = detectedLabel.getLabel().getName();
if (label.equals(currentLabel)) {
continue;
}
labels = labels + label + " / ";
currentLabel = label;
labelsCount++;
}
} while (labelDetectionResult != null && labelDetectionResult.getNextToken() != null);
logger.log("Total number of labels : " + labelsCount);
logger.log("labels : " + labels);
}
}
When checking the results in cloudwatch I receive an error:

05:23:48
Rekognition Video Operation:=========================

05:23:48
Job id: "f647269c4f8bab504cfc9e50a2d89593e463c31755e3bdf41fac18b8be603d65"

05:23:48
Status : "SUCCEEDED"

05:23:48
Job tag : null

05:23:48
Operation : "StartLabelDetection"

05:23:49
*Error: Could not find JobId (Service: AmazonRekognition; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: c4d9bbe0-00f2-11e9-8aa2-21ecd18719a6)*

05:23:49
com.amazonaws.services.rekognition.model.ResourceNotFoundException: Could not find JobId (Service: AmazonRekognition; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: c4d9bbe0-00f2-11e9-8aa2-21ecd18719a6): java.lang.RuntimeException java.lang.RuntimeException: com.amazonaws.services.rekognition.model.ResourceNotFoundException: Could not find JobId (Service: AmazonRekognition; S

05:23:49
END RequestId: 4f531035-00f2-11e9-96d7-f55b78da9771

05:23:49
REPORT RequestId: 4f531035-00f2-11e9-96d7-f55b78da9771 Duration: 941.37 ms Billed Duration: 1000 ms Memory Size: 1024 MB Max Memory Used: 98 MB
I am using the root account and have set up AWS as described in the " Create the AWS Toolkit for Eclipse Lambda Project" Rekognition developer-guide.
Is the problem with the code or something else ?
After consulting with AWS Rekognition forum I was advised to make sure I am calling the fucntion from the same region as stipulated in the function :
AmazonRekognition rek =
AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();