|
28 | 28 | } |
29 | 29 | ) |
30 | 30 |
|
31 | | -//ResourceMachineCreate creates AWS instance |
| 31 | +// ResourceMachineCreate creates AWS instance |
32 | 32 | func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) error { |
33 | 33 | userData := d.Get("startup_script").(string) |
34 | 34 | pairName := d.Id() |
@@ -216,20 +216,45 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf |
216 | 216 | }, |
217 | 217 | }, |
218 | 218 | } |
219 | | - // use availability zone from user |
220 | | - if availabilityZone != "" && subnetId == "" { |
| 219 | + |
| 220 | + if subnetId == "" { |
| 221 | + if availabilityZone == "" { |
| 222 | + offeringsInput := &ec2.DescribeInstanceTypeOfferingsInput{ |
| 223 | + LocationType: types.LocationTypeAvailabilityZone, |
| 224 | + Filters: []types.Filter{ |
| 225 | + { |
| 226 | + Name: aws.String("instance-type"), |
| 227 | + Values: []string{instanceType}, |
| 228 | + }, |
| 229 | + }, |
| 230 | + } |
| 231 | + |
| 232 | + for offeringsPaginator := ec2.NewDescribeInstanceTypeOfferingsPaginator(svc, offeringsInput); offeringsPaginator.HasMorePages(); { |
| 233 | + page, err := offeringsPaginator.NextPage(ctx) |
| 234 | + if err != nil { |
| 235 | + return err |
| 236 | + } |
| 237 | + |
| 238 | + if offerings := page.InstanceTypeOfferings; len(offerings) > 0 { |
| 239 | + availabilityZone = aws.ToString(offerings[0].Location) |
| 240 | + break |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + // use availability zone |
221 | 246 | subnetOptions.Filters = append(subnetOptions.Filters, types.Filter{ |
222 | 247 | Name: aws.String("availability-zone"), |
223 | 248 | Values: []string{availabilityZone}, |
224 | 249 | }) |
225 | | - } |
226 | | - // use exact subnet-id from user |
227 | | - if subnetId != "" { |
| 250 | + } else { |
| 251 | + // use exact subnet-id from user |
228 | 252 | subnetOptions.Filters = append(subnetOptions.Filters, types.Filter{ |
229 | 253 | Name: aws.String("subnet-id"), |
230 | 254 | Values: []string{subnetId}, |
231 | 255 | }) |
232 | 256 | } |
| 257 | + |
233 | 258 | subDesc, err := svc.DescribeSubnets(ctx, subnetOptions) |
234 | 259 | if err != nil { |
235 | 260 | return decodeAWSError(region, err) |
@@ -385,7 +410,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf |
385 | 410 | return nil |
386 | 411 | } |
387 | 412 |
|
388 | | -//ResourceMachineDelete deletes AWS instance |
| 413 | +// ResourceMachineDelete deletes AWS instance |
389 | 414 | func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interface{}) error { |
390 | 415 | id := aws.String(d.Id()) |
391 | 416 | region := GetRegion(d.Get("region").(string)) |
|
0 commit comments