CreateInstance.go - AWS Code Sample

CreateInstance.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2/ec2iface" ) // MakeInstance creates an Amazon Elastic Compute Cloud (Amazon EC2) instance // Inputs: // svc is an Amazon EC2 service client // key is the name of the tag to attach to the instance // value is the value of the tag to attach to the instance // Output: // If success, nil // Otherwise, an error from the call to RunInstances or CreateTags func MakeInstance(svc ec2iface.EC2API, name, value *string) (*ec2.Reservation, error) { result, err := svc.RunInstances(&ec2.RunInstancesInput{ ImageId: aws.String("ami-e7527ed7"), InstanceType: aws.String("t2.micro"), MinCount: aws.Int64(1), MaxCount: aws.Int64(1), }) if err != nil { return nil, err } _, err = svc.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{result.Instances[0].InstanceId}, Tags: []*ec2.Tag{ { Key: name, Value: value, }, }, }) if err != nil { return nil, err } return result, nil } func main() { name := flag.String("n", "", "The name of the tag to attach to the instance") value := flag.String("v", "", "The value of the tag to attach to the instance") flag.Parse() if *name == "" || *value == "" { fmt.Println("You must supply a name and value for the tag (-n NAME -v VALUE)") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := ec2.New(sess) result, err := MakeInstance(svc, name, value) if err != nil { fmt.Println("Got an error creating an instance with tag " + *name) return } fmt.Println("Created tagged instance with ID " + *result.Instances[0].InstanceId) }