MRFramework/src/main/java/com/mr/util/CreateInstances.java
/**
	 * Creates security group and instances in it.
	 * 
	 * @param numberOfInstances
	 * @throws InterruptedException
	 */
Creates security group and instances in it.


Creates security group and instances in it .
private static void createInstances(int numberOfInstances) throws InterruptedException {
		CreateSecurityGroupRequest secGrpReq = new CreateSecurityGroupRequest(securityGroupName, "A9 Security group");
		ec2.createSecurityGroup(secGrpReq);
		AuthorizeSecurityGroupIngressRequest r2 = new AuthorizeSecurityGroupIngressRequest();
		r2.setGroupName(securityGroupName);
		/************* the property of http *****************/
		IpPermission permission = new IpPermission();
		permission.setIpProtocol("tcp");
		permission.setFromPort(80);
		permission.setToPort(80);
		List<String> ipRanges = new ArrayList<String>();
		ipRanges.add("0.0.0.0/0");
		permission.setIpRanges(ipRanges);
		/************* the property of SSH **********************/
		IpPermission permission1 = new IpPermission();
		permission1.setIpProtocol("tcp");
		permission1.setFromPort(22);
		permission1.setToPort(22);
		List<String> ipRanges1 = new ArrayList<String>();
		ipRanges1.add("0.0.0.0/0");
		permission1.setIpRanges(ipRanges1);
		/************* the property of https **********************/
		IpPermission permission2 = new IpPermission();
		permission2.setIpProtocol("tcp");
		permission2.setFromPort(443);
		permission2.setToPort(443);
		List<String> ipRanges2 = new ArrayList<String>();
		ipRanges2.add("0.0.0.0/0");
		permission2.setIpRanges(ipRanges2);
		/************* the property of tcp **********************/
		IpPermission permission3 = new IpPermission();
		permission3.setIpProtocol("tcp");
		permission3.setFromPort(0);
		permission3.setToPort(65535);
		List<String> ipRanges3 = new ArrayList<String>();
		ipRanges3.add("0.0.0.0/0");
		permission3.setIpRanges(ipRanges3);
		/********************** add rules to the group *********************/
		List<IpPermission> permissions = new ArrayList<IpPermission>();
		permissions.add(permission);
		permissions.add(permission1);
		permissions.add(permission2);
		permissions.add(permission3);
		r2.setIpPermissions(permissions);
		ec2.authorizeSecurityGroupIngress(r2);
		List<String> groupName = new ArrayList<String>();
		groupName.add(securityGroupName);
		log.info("Number of intances = " + numberOfInstances);
		String amazonMachineImage = ami;
		int minInstanceCount = 1;
		int maxInstanceCount = numberOfInstances;
		RunInstancesRequest runInstancesRequest = new RunInstancesRequest(amazonMachineImage, minInstanceCount,
				maxInstanceCount);
		runInstancesRequest.setInstanceType(instanceType);
		runInstancesRequest.setKeyName(pemKeys);
		runInstancesRequest.setSecurityGroups(groupName);
		RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);
		/* To make sure instances are running */
		log.info("wait for instances to be in running state");
		List<Instance> resultInstance = runInstancesResult.getReservation().getInstances();
		for (Instance ins : resultInstance) {
			while (getInstanceStatus(ins.getInstanceId()) != 16) {
				Thread.sleep(5000);
			}
			activeInstances.add(ins.getInstanceId());
		}
		log.info("Instances created and running");
	}
private static void createInstances ( int numberOfInstances ) throws InterruptedException { CreateSecurityGroupRequest secGrpReq = new CreateSecurityGroupRequest ( securityGroupName , "A9 Security group" ) ; ec2 . createSecurityGroup ( secGrpReq ) ; AuthorizeSecurityGroupIngressRequest r2 = new AuthorizeSecurityGroupIngressRequest ( ) ; r2 . setGroupName ( securityGroupName ) ; /************* the property of http *****************/ IpPermission permission = new IpPermission ( ) ; permission . setIpProtocol ( "tcp" ) ; permission . setFromPort ( 80 ) ; permission . setToPort ( 80 ) ; List < String > ipRanges = new ArrayList < String > ( ) ; ipRanges . add ( "0.0.0.0/0" ) ; permission . setIpRanges ( ipRanges ) ; /************* the property of SSH **********************/ IpPermission permission1 = new IpPermission ( ) ; permission1 . setIpProtocol ( "tcp" ) ; permission1 . setFromPort ( 22 ) ; permission1 . setToPort ( 22 ) ; List < String > ipRanges1 = new ArrayList < String > ( ) ; ipRanges1 . add ( "0.0.0.0/0" ) ; permission1 . setIpRanges ( ipRanges1 ) ; /************* the property of https **********************/ IpPermission permission2 = new IpPermission ( ) ; permission2 . setIpProtocol ( "tcp" ) ; permission2 . setFromPort ( 443 ) ; permission2 . setToPort ( 443 ) ; List < String > ipRanges2 = new ArrayList < String > ( ) ; ipRanges2 . add ( "0.0.0.0/0" ) ; permission2 . setIpRanges ( ipRanges2 ) ; /************* the property of tcp **********************/ IpPermission permission3 = new IpPermission ( ) ; permission3 . setIpProtocol ( "tcp" ) ; permission3 . setFromPort ( 0 ) ; permission3 . setToPort ( 65535 ) ; List < String > ipRanges3 = new ArrayList < String > ( ) ; ipRanges3 . add ( "0.0.0.0/0" ) ; permission3 . setIpRanges ( ipRanges3 ) ; /********************** add rules to the group *********************/ List < IpPermission > permissions = new ArrayList < IpPermission > ( ) ; permissions . add ( permission ) ; permissions . add ( permission1 ) ; permissions . add ( permission2 ) ; permissions . add ( permission3 ) ; r2 . setIpPermissions ( permissions ) ; ec2 . authorizeSecurityGroupIngress ( r2 ) ; List < String > groupName = new ArrayList < String > ( ) ; groupName . add ( securityGroupName ) ; log . info ( "Number of intances = " + numberOfInstances ) ; String amazonMachineImage = ami ; int minInstanceCount = 1 ; int maxInstanceCount = numberOfInstances ; RunInstancesRequest runInstancesRequest = new RunInstancesRequest ( amazonMachineImage , minInstanceCount , maxInstanceCount ) ; runInstancesRequest . setInstanceType ( instanceType ) ; runInstancesRequest . setKeyName ( pemKeys ) ; runInstancesRequest . setSecurityGroups ( groupName ) ; RunInstancesResult runInstancesResult = ec2 . runInstances ( runInstancesRequest ) ; /* To make sure instances are running */ log . info ( "wait for instances to be in running state" ) ; List < Instance > resultInstance = runInstancesResult . getReservation ( ) . getInstances ( ) ; for ( Instance ins : resultInstance ) { while ( getInstanceStatus ( ins . getInstanceId ( ) ) != 16 ) { Thread . sleep ( 5000 ) ; } activeInstances . add ( ins . getInstanceId ( ) ) ; } log . info ( "Instances created and running" ) ; }
Creates security group and instances in it.
Creates security group and instances in it .