// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceCheckStateRequest.java
// @Data
// public class InstanceCheckStateRequest {
// 
// 	private String queueName;
// 
// 	private String sqsEndpoint;
// 
// 	private int numberOfMessages = 10;
// 
// }
// 
// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceRequest.java
// @Data
// public class InstanceRequest {
// 
// 	private String instanceId;
// 
// 	private String queueName;
// 
// 	private String sqsEndpoint;
// 
// 	private int instanceStateCheckTimeoutSec = 300;
// 
// 	private long sendMessageTimeMillis;
// 
// }
// 
// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceRequests.java
// @Data
// public class InstanceRequests {
// 
// 	private List<InstanceRequest> instanceRequests;
// 
// }


import java.util.Arrays;
import java.util.concurrent.Future;
import com.amazonaws.services.ec2.AmazonEC2Async;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesResult;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceCheckStateRequest;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceRequest;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceRequests;

					+ "CheckInstanceStateRequest[" + checkInstanceStateRequest + "]");
		}

		// Only the specified number, reliably acquired
		int numberOfMessages = checkInstanceStateRequest.getNumberOfMessages();
		for (int i = 0; i < numberOfMessages; i++) {
			AmazonSQSAsync client = createSQSClient();
			client.setEndpoint(sqsEndpoint);
			try {
				String queueUrl = client.createQueue(queueName).getQueueUrl();

				ReceiveMessageRequest req = new ReceiveMessageRequest(queueUrl).withVisibilityTimeout(5)
						.withMaxNumberOfMessages(checkInstanceStateRequest.getNumberOfMessages());
				Future<ReceiveMessageResult> result = client.receiveMessageAsync(req);
				while (!result.isDone()) {
					Thread.sleep(100);
				}
				result.get().getMessages().stream()
						.forEach(s -> checkInstanceState(s, "running", checkInstanceStateRequest, context));

			} catch (Exception e) {
				logger.log("[ERROR][checkInstanceStatus][running]message[" + e.getMessage() + "] stackTrace["
						+ getStackTrace(e) + "] CheckInstanceStateRequest[" + checkInstanceStateRequest + "]");

			} finally {
				client.shutdown();
			}
		}
	}


// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceCheckStateRequest.java
// @Data
// public class InstanceCheckStateRequest {
// 
// 	private String queueName;
// 
// 	private String sqsEndpoint;
// 
// 	private int numberOfMessages = 10;
// 
// }
// 
// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceRequest.java
// @Data
// public class InstanceRequest {
// 
// 	private String instanceId;
// 
// 	private String queueName;
// 
// 	private String sqsEndpoint;
// 
// 	private int instanceStateCheckTimeoutSec = 300;
// 
// 	private long sendMessageTimeMillis;
// 
// }
// 
// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/model/InstanceRequests.java
// @Data
// public class InstanceRequests {
// 
// 	private List<InstanceRequest> instanceRequests;
// 
// }


// Path: instance/src/main/java/jp/gr/java_conf/uzresk/aws/ope/instance/InstanceStartFunction.java
import java.util.Arrays;
import java.util.concurrent.Future;
import com.amazonaws.services.ec2.AmazonEC2Async;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesResult;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceCheckStateRequest;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceRequest;
import jp.gr.java_conf.uzresk.aws.ope.instance.model.InstanceRequests;

					+ "CheckInstanceStateRequest[" + checkInstanceStateRequest + "]");
		}

		// Only the specified number, reliably acquired
		int numberOfMessages = checkInstanceStateRequest.getNumberOfMessages();
		for (int i = 0; i < numberOfMessages; i++) {
			AmazonSQSAsync client = createSQSClient();
			client.setEndpoint(sqsEndpoint);
			try {
				String queueUrl = client.createQueue(queueName).getQueueUrl();

				ReceiveMessageRequest req = new ReceiveMessageRequest(queueUrl).withVisibilityTimeout(5)
						.withMaxNumberOfMessages(checkInstanceStateRequest.getNumberOfMessages());
				Future<ReceiveMessageResult> result = client.receiveMessageAsync(req);
				while (!result.isDone()) {
					Thread.sleep(100);
				}
				result.get().getMessages().stream()
						.forEach(s -> checkInstanceState(s, "running", checkInstanceStateRequest, context));

			} catch (Exception e) {
				logger.log("[ERROR][checkInstanceStatus][running]message[" + e.getMessage() + "] stackTrace["
						+ getStackTrace(e) + "] CheckInstanceStateRequest[" + checkInstanceStateRequest + "]");

			} finally {
				client.shutdown();
			}
		}
	}


	StartInstancesResult startInstance(InstanceRequest instanceRequest, Context context) {
