create_instance.go - AWS Code Sample


Creates an Amazon EC2 instance with tags.

/* Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "" "" "" "fmt" "log" ) func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create EC2 service client svc := ec2.New(sess) // Specify the details of the instance that you want to create. runResult, err := svc.RunInstances(&ec2.RunInstancesInput{ // An Amazon Linux AMI ID for t2.micro instances in the us-west-2 region ImageId: aws.String("ami-e7527ed7"), InstanceType: aws.String("t2.micro"), MinCount: aws.Int64(1), MaxCount: aws.Int64(1), }) if err != nil { fmt.Println("Could not create instance", err) return } fmt.Println("Created instance", *runResult.Instances[0].InstanceId) // Add tags to the created instance _, errtag := svc.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{runResult.Instances[0].InstanceId}, Tags: []*ec2.Tag{ { Key: aws.String("Name"), Value: aws.String("MyFirstInstance"), }, }, }) if errtag != nil { log.Println("Could not create tags for instance", runResult.Instances[0].InstanceId, errtag) return } fmt.Println("Successfully tagged instance") }

Sample Details

Service: ec2

Last tested: 2018-11-08

Author: Doug-AWS

Type: full-example