github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/delete_instance.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/ec2"
     6  	"github.com/emc-advanced-dev/pkg/errors"
     7  	"github.com/solo-io/unik/pkg/types"
     8  )
     9  
    10  func (p *AwsProvider) DeleteInstance(id string, force bool) error {
    11  	instance, err := p.GetInstance(id)
    12  	if err != nil {
    13  		return errors.New("retrieving instance "+id, err)
    14  	}
    15  	if instance.State == types.InstanceState_Running && !force {
    16  		return errors.New("instance "+instance.Id+"is still running. try again with --force or power off instance first", err)
    17  	}
    18  	param := &ec2.TerminateInstancesInput{
    19  		InstanceIds: []*string{
    20  			aws.String(instance.Id),
    21  		},
    22  	}
    23  	_, err = p.newEC2().TerminateInstances(param)
    24  	if err != nil {
    25  		return errors.New("failed to terminate instance "+instance.Id, err)
    26  	}
    27  	return p.state.RemoveInstance(instance)
    28  }