github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ami_from_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  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceAwsAmiFromInstance() *schema.Resource {
    11  	// Inherit all of the common AMI attributes from aws_ami, since we're
    12  	// implicitly creating an aws_ami resource.
    13  	resourceSchema := resourceAwsAmiCommonSchema(true)
    14  
    15  	// Additional attributes unique to the copy operation.
    16  	resourceSchema["source_instance_id"] = &schema.Schema{
    17  		Type:     schema.TypeString,
    18  		Required: true,
    19  		ForceNew: true,
    20  	}
    21  	resourceSchema["snapshot_without_reboot"] = &schema.Schema{
    22  		Type:     schema.TypeBool,
    23  		Optional: true,
    24  		ForceNew: true,
    25  	}
    26  
    27  	return &schema.Resource{
    28  		Create: resourceAwsAmiFromInstanceCreate,
    29  
    30  		Schema: resourceSchema,
    31  
    32  		// The remaining operations are shared with the generic aws_ami resource,
    33  		// since the aws_ami_copy resource only differs in how it's created.
    34  		Read:   resourceAwsAmiRead,
    35  		Update: resourceAwsAmiUpdate,
    36  		Delete: resourceAwsAmiDelete,
    37  	}
    38  }
    39  
    40  func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*AWSClient).ec2conn
    42  
    43  	req := &ec2.CreateImageInput{
    44  		Name:        aws.String(d.Get("name").(string)),
    45  		Description: aws.String(d.Get("description").(string)),
    46  		InstanceId:  aws.String(d.Get("source_instance_id").(string)),
    47  		NoReboot:    aws.Bool(d.Get("snapshot_without_reboot").(bool)),
    48  	}
    49  
    50  	res, err := client.CreateImage(req)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	id := *res.ImageId
    56  	d.SetId(id)
    57  	d.Partial(true) // make sure we record the id even if the rest of this gets interrupted
    58  	d.Set("id", id)
    59  	d.Set("manage_ebs_snapshots", true)
    60  	d.SetPartial("id")
    61  	d.SetPartial("manage_ebs_snapshots")
    62  	d.Partial(false)
    63  
    64  	_, err = resourceAwsAmiWaitForAvailable(id, client)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	return resourceAwsAmiUpdate(d, meta)
    70  }