use of com.amazonaws.services.ec2.model.Vpc in project aws-cf-templates by widdix.
the class TestVPCSshBastion method test.
@Test
public void test() {
final String vpcStackName = "vpc-2azs-" + this.random8String();
final String bastionStackName = "vpc-ssh-bastion-" + this.random8String();
final String classB = "10";
final String keyName = "key-" + this.random8String();
try {
final KeyPair key = this.createKey(keyName);
try {
this.createStack(vpcStackName, "vpc/vpc-2azs.yaml", new Parameter().withParameterKey("ClassB").withParameterValue(classB));
try {
this.createStack(bastionStackName, "vpc/vpc-ssh-bastion.yaml", new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName), new Parameter().withParameterKey("KeyName").withParameterValue(keyName));
final String host = this.getStackOutputValue(bastionStackName, "IPAddress");
this.probeSSH(host, key);
} finally {
this.deleteStack(bastionStackName);
}
} finally {
this.deleteStack(vpcStackName);
}
} finally {
this.deleteKey(keyName);
}
}
use of com.amazonaws.services.ec2.model.Vpc in project aws-cf-templates by widdix.
the class TestEC2AutoRecovery method test.
@Test
public void test() {
final String vpcStackName = "vpc-2azs-" + this.random8String();
final String stackName = "ec2-auto-recovery-" + this.random8String();
final String classB = "10";
final String keyName = "key-" + this.random8String();
try {
final KeyPair key = this.createKey(keyName);
try {
this.createStack(vpcStackName, "vpc/vpc-2azs.yaml", new Parameter().withParameterKey("ClassB").withParameterValue(classB));
try {
this.createStack(stackName, "ec2/ec2-auto-recovery.yaml", new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName), new Parameter().withParameterKey("KeyName").withParameterValue(keyName));
final String host = this.getStackOutputValue(stackName, "IPAddress");
this.probeSSH(host, key);
} finally {
this.deleteStack(stackName);
}
} finally {
this.deleteStack(vpcStackName);
}
} finally {
this.deleteKey(keyName);
}
}
use of com.amazonaws.services.ec2.model.Vpc in project herd by FINRAOS.
the class EmrPricingHelper method updateEmrClusterDefinitionWithBestPrice.
/**
* Finds the best price for each master and core instances based on the subnets and master and core instance search parameters given in the definition.
* <p/>
* The results of the findings are used to update the given definition.
* <p/>
* If the instance's instanceSpotPrice is set, the instance definition will keep that value. If the instance's instanceMaxSearchPrice is set, the best price
* will be found. If the found price is spot, the instanceSpotPrice will be set to the value of instanceMaxSearchPrice. If the found price is on-demand, the
* instanceSpotPrice will be removed. The definition's subnetId will be set to the particular subnet which the best price is found. The value will always be
* replaced by a single subnet ID.
* <p/>
* The definition's instanceMaxSearchPrice and instanceOnDemandThreshold will be removed by this operation.
*
* @param emrClusterAlternateKeyDto EMR cluster alternate key
* @param emrClusterDefinition The EMR cluster definition with search criteria, and the definition that will be updated
* @param awsParamsDto the AWS related parameters for access/secret keys and proxy details
*/
public void updateEmrClusterDefinitionWithBestPrice(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterDefinition emrClusterDefinition, AwsParamsDto awsParamsDto) {
EmrVpcPricingState emrVpcPricingState = new EmrVpcPricingState();
// Get total count of instances this definition will attempt to create
int totalInstanceCount = getTotalInstanceCount(emrClusterDefinition);
// Get the subnet information
List<Subnet> subnets = getSubnets(emrClusterDefinition, awsParamsDto);
for (Subnet subnet : subnets) {
emrVpcPricingState.getSubnetAvailableIpAddressCounts().put(subnet.getSubnetId(), subnet.getAvailableIpAddressCount());
}
// Filter out subnets with not enough available IPs
removeSubnetsWithAvailableIpsLessThan(subnets, totalInstanceCount);
if (subnets.isEmpty()) {
LOGGER.info(String.format("Insufficient IP availability. namespace=\"%s\" emrClusterDefinitionName=\"%s\" emrClusterName=\"%s\" " + "totalRequestedInstanceCount=%s emrVpcPricingState=%s", emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName(), emrClusterAlternateKeyDto.getEmrClusterName(), totalInstanceCount, jsonHelper.objectToJson(emrVpcPricingState)));
throw new ObjectNotFoundException(String.format("There are no subnets in the current VPC which have sufficient IP addresses available to run your " + "clusters. Try expanding the list of subnets or try again later. requestedInstanceCount=%s%n%s", totalInstanceCount, emrVpcPricingStateFormatter.format(emrVpcPricingState)));
}
// Best prices are accumulated in this list
List<EmrClusterPriceDto> emrClusterPrices = new ArrayList<>();
InstanceDefinition masterInstanceDefinition = getMasterInstanceDefinition(emrClusterDefinition);
InstanceDefinition coreInstanceDefinition = getCoreInstanceDefinition(emrClusterDefinition);
InstanceDefinition taskInstanceDefinition = getTaskInstanceDefinition(emrClusterDefinition);
Set<String> requestedInstanceTypes = new HashSet<>();
String masterInstanceType = masterInstanceDefinition.getInstanceType();
requestedInstanceTypes.add(masterInstanceType);
if (coreInstanceDefinition != null) {
String coreInstanceType = coreInstanceDefinition.getInstanceType();
requestedInstanceTypes.add(coreInstanceType);
}
if (taskInstanceDefinition != null) {
String taskInstanceType = taskInstanceDefinition.getInstanceType();
requestedInstanceTypes.add(taskInstanceType);
}
// Get AZs for the subnets
for (AvailabilityZone availabilityZone : getAvailabilityZones(subnets, awsParamsDto)) {
// Create a mapping of instance types to prices for more efficient, in-memory lookup
// This method also validates that the given instance types are real instance types supported by AWS.
Map<String, BigDecimal> instanceTypeOnDemandPrices = getInstanceTypeOnDemandPrices(availabilityZone, requestedInstanceTypes);
// Create a mapping of instance types to prices for more efficient, in-memory lookup
// When AWS does not return any spot price history for an instance type in an availability zone, the algorithm will not use that availability zone
// when selecting the lowest price.
Map<String, BigDecimal> instanceTypeSpotPrices = getInstanceTypeSpotPrices(availabilityZone, requestedInstanceTypes, awsParamsDto);
emrVpcPricingState.getSpotPricesPerAvailabilityZone().put(availabilityZone.getZoneName(), instanceTypeSpotPrices);
emrVpcPricingState.getOnDemandPricesPerAvailabilityZone().put(availabilityZone.getZoneName(), instanceTypeOnDemandPrices);
// Get and compare master price
BigDecimal masterSpotPrice = instanceTypeSpotPrices.get(masterInstanceType);
BigDecimal masterOnDemandPrice = instanceTypeOnDemandPrices.get(masterInstanceType);
Ec2PriceDto masterPrice = getBestInstancePrice(masterSpotPrice, masterOnDemandPrice, masterInstanceDefinition);
// Get and compare core price
Ec2PriceDto corePrice = null;
if (coreInstanceDefinition != null) {
String coreInstanceType = coreInstanceDefinition.getInstanceType();
BigDecimal coreSpotPrice = instanceTypeSpotPrices.get(coreInstanceType);
BigDecimal coreOnDemandPrice = instanceTypeOnDemandPrices.get(coreInstanceType);
corePrice = getBestInstancePrice(coreSpotPrice, coreOnDemandPrice, coreInstanceDefinition);
}
// Get and compare task price
Ec2PriceDto taskPrice = null;
if (taskInstanceDefinition != null) {
String taskInstanceType = taskInstanceDefinition.getInstanceType();
BigDecimal taskSpotPrice = instanceTypeSpotPrices.get(taskInstanceType);
BigDecimal taskOnDemandPrice = instanceTypeOnDemandPrices.get(taskInstanceType);
taskPrice = getBestInstancePrice(taskSpotPrice, taskOnDemandPrice, taskInstanceDefinition);
}
// If prices were found
if (masterPrice != null && (coreInstanceDefinition == null || corePrice != null) && (taskInstanceDefinition == null || taskPrice != null)) {
// Add the pricing result to the result list
emrClusterPrices.add(createEmrClusterPrice(availabilityZone, masterPrice, corePrice, taskPrice));
}
// If prices were not found for either master or core, this AZ cannot satisfy the search criteria. Ignore this AZ.
}
if (emrClusterPrices.isEmpty()) {
LOGGER.info(String.format("No subnets which satisfied the best price search criteria. namespace=\"%s\" emrClusterDefinitionName=\"%s\" " + "emrClusterName=\"%s\" emrVpcPricingState=%s", emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName(), emrClusterAlternateKeyDto.getEmrClusterName(), jsonHelper.objectToJson(emrVpcPricingState)));
throw new ObjectNotFoundException(String.format("There were no subnets which satisfied your best price search criteria. If you explicitly opted to use spot EC2 instances, please confirm " + "that your instance types support spot pricing. Otherwise, try setting the max price or the on-demand threshold to a higher value.%n%s", emrVpcPricingStateFormatter.format(emrVpcPricingState)));
}
// Find the best prices from the result list
EmrClusterPriceDto bestEmrClusterPrice = getEmrClusterPriceWithLowestCoreInstancePrice(emrClusterPrices);
// Find the best subnet among the best AZ's
Subnet bestEmrClusterSubnet = getBestSubnetForAvailabilityZone(bestEmrClusterPrice.getAvailabilityZone(), subnets);
// Update the definition with the new calculated values
updateInstanceDefinitionsWithBestPrice(emrClusterDefinition, bestEmrClusterSubnet, bestEmrClusterPrice);
}
use of com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSRemoteCleanup method cleanUpVpc.
/**
* Cleaning all VPC's that are not tagged with a name: enumtest-vpc or a default VPC in US_EAST_1 region
* Deleting a VPC would require its dependencies to be deleted in the following order:
* 1) EC2 Instances
* 2) NAT Gateway
* 3) Internet Gateway
* 4) VPN Gateway
* 5) Network ACL's
* 6) Security Group ( not deleting default SG)
* 7) Subnets
* NOTE: Not deleting RouteTables currently
*/
@Test
public void cleanUpVpc() {
if (this.isMock) {
return;
}
AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
DescribeVpcsResult vpcsResult = usEastEc2Client.describeVpcs();
List<Vpc> vpcs = vpcsResult.getVpcs();
List<String> vpcIdsToBeDeleted = new ArrayList<>();
List<String> enumTestVpcIds = new ArrayList<>();
try {
vpcs.stream().forEach(vpc -> {
vpc.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(NAME_TAG_KEY) && this.vpcTagsNotToBeDeleted.contains(tag.getValue().toLowerCase())).forEach(tag -> enumTestVpcIds.add(vpc.getVpcId()));
if (!vpc.getIsDefault()) {
vpcIdsToBeDeleted.add(vpc.getVpcId());
}
});
vpcIdsToBeDeleted.removeAll(enumTestVpcIds);
vpcIdsToBeDeleted.stream().forEach(vpcId -> {
DescribeInstancesRequest instancesRequest = new DescribeInstancesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeInstancesResult instancesResult = usEastEc2Client.describeInstances(instancesRequest);
deleteAwsEc2instances(vpcIdsToBeDeleted, instancesResult, usEastEc2Client);
deleteNATGateway(vpcId, usEastEc2Client);
deleteNetworkInterfaces(vpcId, usEastEc2Client);
deleteInternetGateways(vpcId, usEastEc2Client);
deleteVirtualPrivateGateways(vpcId, usEastEc2Client);
disassociateAndDeleteNetworkACLs(vpcId, usEastEc2Client);
deleteSecurityGroups(vpcId, usEastEc2Client);
deleteSubnets(vpcId, usEastEc2Client);
DeleteVpcRequest deleteVpcRequest = new DeleteVpcRequest().withVpcId(vpcId);
this.host.log("Terminating stale vpc: %s", vpcId);
usEastEc2Client.deleteVpc(deleteVpcRequest);
});
} catch (Exception e) {
this.host.log(Level.INFO, e.getMessage());
}
}
use of com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkService method handleStages.
private void handleStages(AWSNetworkContext context) {
try {
switch(context.stage) {
case NETWORK_TASK_STATE:
getNetworkTaskState(context, AWSNetworkStage.NETWORK_STATE);
break;
case NETWORK_STATE:
getNetworkState(context, AWSNetworkStage.CREDENTIALS);
break;
case CREDENTIALS:
getCredentials(context, AWSNetworkStage.AWS_CLIENT);
break;
case AWS_CLIENT:
this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.network.regionId, this).whenComplete((ec2Client, t) -> {
if (t != null) {
context.stage = AWSNetworkStage.FAILED;
context.error = t;
handleStages(context);
return;
}
context.client = new AWSNetworkClient(ec2Client);
if (context.networkRequest.requestType == NetworkInstanceRequest.InstanceRequestType.CREATE) {
context.stage = AWSNetworkStage.PROVISION_VPC;
} else {
context.stage = AWSNetworkStage.REMOVE_GATEWAY;
}
handleStages(context);
});
break;
case PROVISION_VPC:
String vpcID = context.client.createVPC(context.network.subnetCIDR);
updateNetworkProperties(AWS_VPC_ID, vpcID, context, AWSNetworkStage.PROVISION_SUBNET);
break;
case PROVISION_SUBNET:
Subnet subnet = context.client.createSubnet(context.network.subnetCIDR, getCustomProperty(context, AWS_VPC_ID));
createSubnetState(subnet, context, AWSNetworkStage.PROVISION_GATEWAY);
break;
case PROVISION_GATEWAY:
String gatewayID = context.client.createInternetGateway();
context.client.attachInternetGateway(getCustomProperty(context, AWS_VPC_ID), gatewayID);
updateNetworkProperties(AWS_GATEWAY_ID, gatewayID, context, AWSNetworkStage.PROVISION_ROUTE);
break;
case PROVISION_ROUTE:
RouteTable routeTable = context.client.getMainRouteTable(context.network.customProperties.get(AWS_VPC_ID));
context.client.createInternetRoute(getCustomProperty(context, AWS_GATEWAY_ID), routeTable.getRouteTableId(), ROUTE_DEST_ALL);
updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, routeTable.getRouteTableId(), context, AWSNetworkStage.FINISHED);
break;
case REMOVE_GATEWAY:
context.client.detachInternetGateway(getCustomProperty(context, AWS_VPC_ID), getCustomProperty(context, AWS_GATEWAY_ID));
context.client.deleteInternetGateway(getCustomProperty(context, AWS_GATEWAY_ID));
updateNetworkProperties(AWS_GATEWAY_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_SUBNET);
break;
case REMOVE_SUBNET:
// Iterate SubnetStates (page-by-page) and delete AWS Subnet and SubnetState
deleteSubnetStates(context, AWSNetworkStage.REMOVE_ROUTE);
break;
case REMOVE_ROUTE:
// only need to update the document, the AWS artifact will be
// removed on VPC removal
updateNetworkProperties(AWS_VPC_ROUTE_TABLE_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.REMOVE_VPC);
break;
case REMOVE_VPC:
context.client.deleteVPC(getCustomProperty(context, AWS_VPC_ID));
updateNetworkProperties(AWS_VPC_ID, AWSUtils.NO_VALUE, context, AWSNetworkStage.FINISHED);
break;
case FAILED:
context.taskManager.patchTaskToFailure(context.error);
break;
case FINISHED:
context.taskManager.finishTask();
break;
default:
break;
}
} catch (Throwable error) {
// Same as FAILED stage
context.taskManager.patchTaskToFailure(error);
}
}
Aggregations