/** * Create new volume */ func Create(ec2Ref *ec2.EC2, volume *Volume) (ec2.Volume, error) { options := ec2.CreateVolume{ VolumeType: volume.Type, VolumeSize: volume.Size, } if volume.AvailableZone == "" { options.AvailZone = DefaultAvailableZone } if volume.Type == "io1" { options.IOPS = volume.IOPS } resp, err := ec2Ref.CreateVolume(options) if err != nil { return ec2.Volume{}, err } volumeRef := resp.Volume _, err = ec2Ref.CreateTags([]string{volumeRef.Id}, []ec2.Tag{{"Name", volume.Name}}) if err != nil { return ec2.Volume{}, err } mergeVolumes(volume, &volumeRef) err = WaitUntilState(ec2Ref, volume, "available") if err != nil { return ec2.Volume{}, err } return volumeRef, nil }
// Create new instance func Create(ec2Ref *ec2.EC2, instance *Instance) (ec2.Instance, error) { options := ec2.RunInstances{ ImageId: instance.ImageID, InstanceType: instance.Type, KeyName: instance.KeyName, SecurityGroups: make([]ec2.SecurityGroup, len(instance.SecurityGroups)), SubnetId: instance.SubnetID, EBSOptimized: instance.EBSOptimized, DisableAPITermination: !instance.EnableAPITermination, AvailZone: instance.AvailableZone, } if instance.CloudConfig != "" { userdata, err := ioutil.ReadFile(instance.CloudConfig) if err != nil { panic(err.Error()) } options.UserData = userdata } if instance.ShutdownBehavior != "" { options.ShutdownBehavior = instance.ShutdownBehavior } if instance.PlacementGroupName != "" { options.PlacementGroupName = instance.PlacementGroupName } for i, securityGroup := range instance.SecurityGroups { options.SecurityGroups[i] = ec2.SecurityGroup{Id: securityGroup} } resp, err := ec2Ref.RunInstances(&options) if err != nil { return ec2.Instance{}, err } else if len(resp.Instances) == 0 { return ec2.Instance{}, errors.New("Any instance was created!") } ec2Instance := resp.Instances[0] tags := append(instance.Tags, ec2.Tag{"Name", instance.Name}) _, err = ec2Ref.CreateTags([]string{ec2Instance.InstanceId}, tags) if err != nil { return ec2.Instance{}, err } mergeInstances(instance, &ec2Instance) err = WaitUntilState(ec2Ref, instance, "running") if err != nil { return ec2.Instance{}, err } return ec2Instance, nil }
// Create new volume func Create(ec2Ref *ec2.EC2, volume *Volume) (ec2.Volume, error) { options := ec2.CreateVolume{ VolumeType: volume.Type, AvailZone: volume.AvailableZone, } if volume.Size > 0 { options.VolumeSize = volume.Size } if volume.SnapshotID != "" { options.SnapshotId = volume.SnapshotID } if volume.Type == "io1" { options.IOPS = volume.IOPS } resp, err := ec2Ref.CreateVolume(options) if err != nil { return ec2.Volume{}, err } ec2Volume := resp.Volume tags := append(volume.Tags, ec2.Tag{"Name", volume.Name}) _, err = ec2Ref.CreateTags([]string{ec2Volume.Id}, tags) if err != nil { return ec2.Volume{}, err } mergeVolumes(volume, &ec2Volume) err = WaitUntilState(ec2Ref, volume, "available") if err != nil { return ec2.Volume{}, err } return ec2Volume, nil }
/** * Create new instance */ func Create(ec2Ref *ec2.EC2, instance *Instance) (ec2.Instance, error) { options := ec2.RunInstances{ ImageId: instance.ImageId, InstanceType: instance.Type, KeyName: instance.KeyName, SecurityGroups: make([]ec2.SecurityGroup, len(instance.SecurityGroups)), SubnetId: instance.SubnetId, EBSOptimized: instance.EBSOptimized, DisableAPITermination: !instance.EnableAPITermination, } if instance.CloudConfig != "" { cloudConfigTemplate, err := ioutil.ReadFile(instance.CloudConfig) if err != nil { panic(err.Error()) } tpl := template.Must(template.New("cloudConfig").Parse(string(cloudConfigTemplate))) cloudConfig := new(bytes.Buffer) if err = tpl.Execute(cloudConfig, instance); err != nil { panic(err.Error()) } options.UserData = cloudConfig.Bytes() } if instance.ShutdownBehavior != "" { options.ShutdownBehavior = instance.ShutdownBehavior } if instance.PlacementGroupName != "" { options.PlacementGroupName = instance.PlacementGroupName } for i, securityGroup := range instance.SecurityGroups { options.SecurityGroups[i] = ec2.SecurityGroup{Id: securityGroup} } resp, err := ec2Ref.RunInstances(&options) if err != nil { return ec2.Instance{}, err } else if len(resp.Instances) == 0 { return ec2.Instance{}, errors.New("Any instance was created!") } instanceRef := resp.Instances[0] _, err = ec2Ref.CreateTags([]string{instanceRef.InstanceId}, []ec2.Tag{{"Name", instance.Name}}) if err != nil { return ec2.Instance{}, err } mergeInstances(instance, &instanceRef) err = WaitUntilState(ec2Ref, instance, "running") if err != nil { return ec2.Instance{}, err } return instanceRef, nil }