Created
April 5, 2015 21:31
-
-
Save filewalkwithme/a424b09147e5953f42f1 to your computer and use it in GitHub Desktop.
Running Amazon EC2 instances with Golang
This file contains hidden or 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
// 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://maicon.io/post.html?id=5