Skip to content

Commit 91c3200

Browse files
committed
provider/aws: Allow aws_instances to be resized rather than forcing a
new instance Fixes: #9157 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSInstance_changeInstanceType' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/02/16 15:13:21 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSInstance_changeInstanceType -timeout 120m === RUN TestAccAWSInstance_changeInstanceType --- PASS: TestAccAWSInstance_changeInstanceType (303.85s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 303.876s ```
1 parent 125641e commit 91c3200

File tree

2 files changed

+165
-27
lines changed

2 files changed

+165
-27
lines changed

builtin/providers/aws/resource_aws_instance.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func resourceAwsInstance() *schema.Resource {
6363
"instance_type": {
6464
Type: schema.TypeString,
6565
Required: true,
66-
ForceNew: true,
6766
},
6867

6968
"key_name": {
@@ -606,6 +605,60 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
606605
}
607606
}
608607

608+
if d.HasChange("instance_type") && !d.IsNewResource() {
609+
log.Printf("[INFO] Stopping Instance %q for instance_type change", d.Id())
610+
_, err := conn.StopInstances(&ec2.StopInstancesInput{
611+
InstanceIds: []*string{aws.String(d.Id())},
612+
})
613+
614+
stateConf := &resource.StateChangeConf{
615+
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
616+
Target: []string{"stopped"},
617+
Refresh: InstanceStateRefreshFunc(conn, d.Id()),
618+
Timeout: 10 * time.Minute,
619+
Delay: 10 * time.Second,
620+
MinTimeout: 3 * time.Second,
621+
}
622+
623+
_, err = stateConf.WaitForState()
624+
if err != nil {
625+
return fmt.Errorf(
626+
"Error waiting for instance (%s) to stop: %s", d.Id(), err)
627+
}
628+
629+
log.Printf("[INFO] Modifying instance type %s", d.Id())
630+
_, err = conn.ModifyInstanceAttribute(&ec2.ModifyInstanceAttributeInput{
631+
InstanceId: aws.String(d.Id()),
632+
InstanceType: &ec2.AttributeValue{
633+
Value: aws.String(d.Get("instance_type").(string)),
634+
},
635+
})
636+
if err != nil {
637+
return err
638+
}
639+
640+
log.Printf("[INFO] Starting Instance %q after instance_type change", d.Id())
641+
_, err = conn.StartInstances(&ec2.StartInstancesInput{
642+
InstanceIds: []*string{aws.String(d.Id())},
643+
})
644+
645+
stateConf = &resource.StateChangeConf{
646+
Pending: []string{"pending", "stopped"},
647+
Target: []string{"running"},
648+
Refresh: InstanceStateRefreshFunc(conn, d.Id()),
649+
Timeout: 10 * time.Minute,
650+
Delay: 10 * time.Second,
651+
MinTimeout: 3 * time.Second,
652+
}
653+
654+
_, err = stateConf.WaitForState()
655+
if err != nil {
656+
return fmt.Errorf(
657+
"Error waiting for instance (%s) to become ready: %s",
658+
d.Id(), err)
659+
}
660+
}
661+
609662
if d.HasChange("disable_api_termination") {
610663
_, err := conn.ModifyInstanceAttribute(&ec2.ModifyInstanceAttributeInput{
611664
InstanceId: aws.String(d.Id()),

0 commit comments

Comments
 (0)