46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
awsserversdk "gitea.s1f.ren/shiran/aws-server-sdk"
|
|
)
|
|
|
|
func main() {
|
|
client, err := awsserversdk.NewClient(os.Getenv("AWS_SERVER_BASE_URL"), os.Getenv("AWS_SERVER_APP_TOKEN"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
region := os.Getenv("AWS_SERVER_REGION")
|
|
options, err := client.Catalog.GetCreateOptions(context.Background(), region)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if len(options.Images) == 0 || len(options.InstanceTypes) == 0 {
|
|
log.Fatal("Region has no published image or instance type")
|
|
}
|
|
request := awsserversdk.CreateInstanceRequest{ImageID: options.Images[0].ImageID, InstanceType: options.InstanceTypes[0].InstanceType, Name: "sdk-example", AssociateEIP: true, RootVolume: &awsserversdk.VolumeSpec{SizeGiB: 30, Type: "gp3"}}
|
|
if len(options.Placements) > 0 {
|
|
request.PlacementID = options.Placements[0].PlacementID
|
|
}
|
|
estimate, err := client.Pricing.EstimateInstance(context.Background(), region, request)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("estimated hourly %.6f %s", estimate.HourlyPrice, estimate.Currency)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
|
|
defer cancel()
|
|
operation, err := client.Instances.Create(ctx, region, request)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
task, err := operation.Wait(ctx)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("instance %s created by task %s", operation.ID, task.ID)
|
|
}
|