Created
September 15, 2017 22:09
-
-
Save zakhark/0aad795ccc35a23ebc64cc6a6855bba0 to your computer and use it in GitHub Desktop.
ec2spot.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awserr" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/ec2" | |
) | |
func main() { | |
fmt.Println("starting SpawnHost function") | |
SpawnHost() | |
fmt.Println("finished SpawnHost function") | |
} | |
// SpawnHost tests spawning a host. | |
func SpawnHost() { | |
fmt.Println("requesting a spot instance") | |
svc := ec2.New(session.New(), &aws.Config{ | |
Region: aws.String("us-east-1"), | |
Credentials: credentials.NewStaticCredentials("key", "secret", ""), | |
}) | |
input := &ec2.RequestSpotInstancesInput{ | |
InstanceCount: aws.Int64(1), | |
LaunchSpecification: &ec2.RequestSpotLaunchSpecification{ | |
// IamInstanceProfile: &ec2.IamInstanceProfileSpecification{ | |
// Arn: aws.String("arn:aws:iam::123456789012:instance-profile/my-iam-role"), | |
// }, | |
ImageId: aws.String("ami-12345678"), | |
InstanceType: aws.String("c3.xlarge"), | |
SecurityGroupIds: []*string{ | |
aws.String("sg-12345678"), | |
}, | |
SubnetId: aws.String("subnet-12345678"), | |
}, | |
SpotPrice: aws.String("0.050"), | |
Type: aws.String("one-time"), | |
} | |
result, err := svc.RequestSpotInstances(input) | |
if err != nil { | |
if aerr, ok := err.(awserr.Error); ok { | |
switch aerr.Code() { | |
default: | |
fmt.Println(aerr.Error()) | |
} | |
} else { | |
// Print the error, cast err to awserr.Error to get the Code and | |
// Message from an error. | |
fmt.Println(err.Error()) | |
} | |
return | |
} | |
fmt.Println(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment