From 521451db4d3e85f46ba138ed5805863582ff0f14 Mon Sep 17 00:00:00 2001 From: Marc Tamsky Date: Sat, 18 Jul 2015 09:45:34 -0700 Subject: [PATCH 1/2] provider/aws/aws_instance: add new argument `instance_initiated_shutdown_behavior`, accepts string values of 'stop' or 'terminate'. Signed-off-by: Marc Tamsky --- .../providers/aws/resource_aws_instance.go | 22 +++++++++++++++++++ .../providers/aws/r/instance.html.markdown | 2 ++ 2 files changed, 24 insertions(+) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 9a4306acb..ef00bf180 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -148,6 +148,12 @@ func resourceAwsInstance() *schema.Resource { Optional: true, }, + "instance_initiated_shutdown_behavior": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "stop", + }, + "monitoring": &schema.Schema{ Type: schema.TypeBool, Optional: true, @@ -333,6 +339,7 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error { IAMInstanceProfile: instanceOpts.IAMInstanceProfile, ImageID: instanceOpts.ImageID, InstanceType: instanceOpts.InstanceType, + InstanceInitiatedShutdownBehavior: instanceOpts.InstanceInitiatedShutdownBehavior, KeyName: instanceOpts.KeyName, MaxCount: aws.Long(int64(1)), MinCount: aws.Long(int64(1)), @@ -579,6 +586,19 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error { } } + if d.HasChange("instance_initiated_shutdown_behavior") { + log.Printf("[INFO] Modifying instance %s", d.Id()) + _, err := conn.ModifyInstanceAttribute(&ec2.ModifyInstanceAttributeInput{ + InstanceID: aws.String(d.Id()), + InstanceInitiatedShutdownBehavior: &ec2.AttributeValue{ + Value: aws.String(d.Get("instance_initiated_shutdown_behavior").(string)), + }, + }) + if err != nil { + return err + } + } + // TODO(mitchellh): wait for the attributes we modified to // persist the change... @@ -864,6 +884,7 @@ type awsInstanceOpts struct { IAMInstanceProfile *ec2.IAMInstanceProfileSpecification ImageID *string InstanceType *string + InstanceInitiatedShutdownBehavior *string KeyName *string NetworkInterfaces []*ec2.InstanceNetworkInterfaceSpecification Placement *ec2.Placement @@ -884,6 +905,7 @@ func buildAwsInstanceOpts( EBSOptimized: aws.Boolean(d.Get("ebs_optimized").(bool)), ImageID: aws.String(d.Get("ami").(string)), InstanceType: aws.String(d.Get("instance_type").(string)), + InstanceInitiatedShutdownBehavior: aws.String(d.Get("instance_initiated_shutdown_behavior").(string)), } opts.Monitoring = &ec2.RunInstancesMonitoringEnabled{ diff --git a/website/source/docs/providers/aws/r/instance.html.markdown b/website/source/docs/providers/aws/r/instance.html.markdown index 22cbe68cd..3146fe7ac 100644 --- a/website/source/docs/providers/aws/r/instance.html.markdown +++ b/website/source/docs/providers/aws/r/instance.html.markdown @@ -36,6 +36,8 @@ The following arguments are supported: EBS-optimized. * `disable_api_termination` - (Optional) If true, enables [EC2 Instance Termination Protection](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingDisableAPITermination) +* `instance_initiated_shutdown_behavior` - (Optional) [Shutdown Behavior](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior) Can be `"stop"` +or `"terminate"`. (Default: `"stop"`) * `instance_type` - (Required) The type of instance to start * `key_name` - (Optional) The key name to use for the instance. * `monitoring` - (Optional) If true, the launched EC2 instance will have detailed monitoring enabled. (Available since v0.6.0) From 8d5fe931522105ed2791c5c4b88c4a72c1122638 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Tue, 4 Aug 2015 06:09:47 -0500 Subject: [PATCH 2/2] conditionally set InstanceInitiatedShutdownBehavior --- builtin/providers/aws/resource_aws_instance.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 8d3c24e1f..c04f0ceb5 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -150,7 +150,6 @@ func resourceAwsInstance() *schema.Resource { "instance_initiated_shutdown_behavior": &schema.Schema{ Type: schema.TypeString, Optional: true, - Default: "stop", }, "monitoring": &schema.Schema{ @@ -922,11 +921,14 @@ func buildAwsInstanceOpts( conn := meta.(*AWSClient).ec2conn opts := &awsInstanceOpts{ - DisableAPITermination: aws.Bool(d.Get("disable_api_termination").(bool)), - EBSOptimized: aws.Bool(d.Get("ebs_optimized").(bool)), - ImageID: aws.String(d.Get("ami").(string)), - InstanceType: aws.String(d.Get("instance_type").(string)), - InstanceInitiatedShutdownBehavior: aws.String(d.Get("instance_initiated_shutdown_behavior").(string)), + DisableAPITermination: aws.Bool(d.Get("disable_api_termination").(bool)), + EBSOptimized: aws.Bool(d.Get("ebs_optimized").(bool)), + ImageID: aws.String(d.Get("ami").(string)), + InstanceType: aws.String(d.Get("instance_type").(string)), + } + + if v := d.Get("instance_initiated_shutdown_behavior").(string); v != "" { + opts.InstanceInitiatedShutdownBehavior = aws.String(v) } opts.Monitoring = &ec2.RunInstancesMonitoringEnabled{