Skip to content

Commit

Permalink
Check if instance type is available in AZ
Browse files Browse the repository at this point in the history
  • Loading branch information
jawadqur committed Jun 13, 2024
1 parent 5e2fce4 commit 7b2bb14
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions hatchery/nextflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ func createBatchComputeEnvironment(nextflowGlobalConfig NextflowGlobalConfig, ne
return "", err
}

subnets := []*string{}
for _, subnet := range subnetids {
s := subnet
subnets = append(subnets, &s)
}

// update any settings that may have changed in the config
// TODO also make sure it is pointing at the correct subnets - if the VPC is deleted,
// we should recreate the compute environment as well because it will be pointing at
Expand All @@ -678,6 +684,7 @@ func createBatchComputeEnvironment(nextflowGlobalConfig NextflowGlobalConfig, ne
MinvCpus: aws.Int64(int64(nextflowConfig.InstanceMinVCpus)),
MaxvCpus: aws.Int64(int64(nextflowConfig.InstanceMaxVCpus)),
InstanceTypes: []*string{aws.String(nextflowConfig.InstanceType)},
Subnets: subnets,
Type: aws.String(nextflowConfig.ComputeEnvironmentType),
Tags: tagsMap,
},
Expand Down Expand Up @@ -1075,11 +1082,52 @@ func setupSubnet(subnetName string, cidr string, vpcid string, ec2Svc *ec2.EC2)
return exsubnet.Subnets[0].SubnetId, nil
}

// Fetch all availability zones
// this is being limited to a region by the ec2svc that gets passed in.
describeZonesOutput, err := ec2Svc.DescribeAvailabilityZones(&ec2.DescribeAvailabilityZonesInput{})
if err != nil {
return nil, fmt.Errorf("failed to describe availability zones: %v", err)
}

var selectedZone string
for _, zone := range describeZonesOutput.AvailabilityZones {
if *zone.State == "available" {
input := &ec2.DescribeInstanceTypeOfferingsInput{
LocationType: aws.String("availability-zone"),
Filters: []*ec2.Filter{
{
Name: aws.String("location"),
Values: []*string{aws.String(*zone.ZoneName)},
},
{
Name: aws.String("instance-type"),
// TODO: Should this be configurable?
Values: []*string{aws.String("g4dn.xlarge")},
},
},
}
result, err := ec2Svc.DescribeInstanceTypeOfferings(input)
if err != nil {
return nil, fmt.Errorf("Error describing instance type offerings: %v", err)
}
if len(result.InstanceTypeOfferings) > 0 {
Config.Logger.Printf("Debug: Zone: %v has instance type g4dn.xlarge available. Using that for subnet", *zone.ZoneName)
selectedZone = *zone.ZoneName
break // Exit the loop if we found a suitable zone
}
}
}

if selectedZone == "" {
return nil, fmt.Errorf("no suitable availability zone found")
}

// create subnet
Config.Logger.Printf("Debug: Creating subnet '%v' with name '%s'", cidr, subnetName)
sn, err := ec2Svc.CreateSubnet(&ec2.CreateSubnetInput{
CidrBlock: aws.String(cidr),
VpcId: aws.String(vpcid),
CidrBlock: aws.String(cidr),
VpcId: aws.String(vpcid),
AvailabilityZone: aws.String(selectedZone),
TagSpecifications: []*ec2.TagSpecification{
{
// Name
Expand Down

0 comments on commit 7b2bb14

Please sign in to comment.