Skip to content

Instantly share code, notes, and snippets.

@filewalkwithme
Created April 5, 2015 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filewalkwithme/a424b09147e5953f42f1 to your computer and use it in GitHub Desktop.
Save filewalkwithme/a424b09147e5953f42f1 to your computer and use it in GitHub Desktop.
Running Amazon EC2 instances with Golang
// from: http://maicon.io/golang-running-aws-ec2-instances.html
package main
import (
"fmt"
"os"
aws "github.com/goamz/goamz/aws"
ec2 "github.com/goamz/goamz/ec2"
)
func main() {
//Get environment variables
awsAccessKey := os.Getenv("MY_AWS_ACCESS_KEY")
awsSecretKey := os.Getenv("MY_AWS_SECRET_KEY")
if awsAccessKey == "" || awsSecretKey == "" {
fmt.Printf("MY_AWS_ACCESS_KEY or MY_AWS_SECRET_KEY not found... Exiting.\n")
} else {
//authorization
auth := aws.Auth{AccessKey: awsAccessKey, SecretKey: awsSecretKey}
//instanciate the ec2 object for USEast region
ec2use := ec2.New(auth, aws.USEast)
instanceOptions := &ec2.RunInstancesOptions{
ImageId: "ami-000000000", //your ami ID
MinCount: 1,
MaxCount: 1,
KeyName: "your-ec2-key-par-name",
InstanceType: "t1.micro",
SecurityGroups: []ec2.SecurityGroup{}, //default security group
AvailabilityZone: "us-east-1a",
SubnetId: "your-subnet-id"}
resp, err := ec2use.RunInstances(instanceOptions)
if err == nil {
fmt.Printf("Instance created %v \n", resp.Instances[0].InstanceId)
} else {
fmt.Printf("Error while creating instance: %v \n", err)
}
}
}
@filewalkwithme
Copy link
Author

filewalkwithme commented Apr 5, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment