Skip to content

Commit

Permalink
Add instance shutdown behavior retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie authored and YakDriver committed Apr 17, 2021
1 parent 79da3dd commit 7e76931
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func resourceAwsInstance() *schema.Resource {
"instance_initiated_shutdown_behavior": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"instance_state": {
Type: schema.TypeString,
Expand Down Expand Up @@ -940,6 +941,11 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
return err
}

// Retrieve instance shutdown behavior
if err := readInstanceShutdownBehavior(d, conn); err != nil {
return err
}

if err := readBlockDevices(d, instance, conn); err != nil {
return err
}
Expand Down Expand Up @@ -2188,6 +2194,23 @@ func readSecurityGroups(d *schema.ResourceData, instance *ec2.Instance, conn *ec
return nil
}

func readInstanceShutdownBehavior(d *schema.ResourceData, conn *ec2.EC2) error {
out, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
InstanceId: aws.String(d.Id()),
Attribute: aws.String("instanceInitiatedShutdownBehavior"),
})

if err != nil {
return err
}

if err = d.Set("instance_initiated_shutdown_behavior", out.InstanceInitiatedShutdownBehavior.Value); err != nil {
return err
}

return nil
}

func getAwsEc2InstancePasswordData(instanceID string, conn *ec2.EC2) (string, error) {
log.Printf("[INFO] Reading password data for instance %s", instanceID)

Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func TestAccAWSInstance_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &v),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`instance/i-[a-z0-9]+`)),
resource.TestCheckResourceAttr(resourceName, "instance_initiated_shutdown_behavior", "stop"),
),
},
{
Expand Down

0 comments on commit 7e76931

Please sign in to comment.