Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable hibernation attribute for aws_instance resource #6961

Merged
merged 3 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ func resourceAwsInstance() *schema.Resource {
Optional: true,
},

"hibernation": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we will want to follow the API structure in Terraform (e.g. it would look like the below), however this resource already goes against that in other attributes and this implementation won't prevent that later potential refactoring, so merging this as-is.

resource "aws_instance" "example" {
  # ... other configuration ...
  hibernation_options {
    configured = true
  }
}

Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"monitoring": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -565,6 +571,7 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
UserData: instanceOpts.UserData64,
CreditSpecification: instanceOpts.CreditSpecification,
CpuOptions: instanceOpts.CpuOptions,
HibernationOptions: instanceOpts.HibernationOptions,
TagSpecifications: tagSpecifications,
}

Expand Down Expand Up @@ -712,6 +719,10 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("cpu_threads_per_core", instance.CpuOptions.ThreadsPerCore)
}

if instance.HibernationOptions != nil {
d.Set("hibernation", instance.HibernationOptions.Configured)
}

d.Set("ami", instance.ImageId)
d.Set("instance_type", instance.InstanceType)
d.Set("key_name", instance.KeyName)
Expand Down Expand Up @@ -1798,6 +1809,7 @@ type awsInstanceOpts struct {
UserData64 *string
CreditSpecification *ec2.CreditSpecificationRequest
CpuOptions *ec2.CpuOptionsRequest
HibernationOptions *ec2.HibernationOptionsRequest
}

func buildAwsInstanceOpts(
Expand Down Expand Up @@ -1889,6 +1901,12 @@ func buildAwsInstanceOpts(
}
}

if v := d.Get("hibernation"); v != "" {
opts.HibernationOptions = &ec2.HibernationOptionsRequest{
Configured: aws.Bool(v.(bool)),
}
}

var groups []*string
if v := d.Get("security_groups"); v != nil {
// Security group names.
Expand Down
56 changes: 56 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,25 @@ func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) {
})
}

func TestAccAWSInstance_hibernation(t *testing.T) {
var instance ec2.Instance

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigHibernation,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &instance),
resource.TestCheckResourceAttr("aws_instance.foo", "hibernation", "true"),
),
},
},
})
}

func testAccCheckInstanceNotRecreated(t *testing.T,
before, after *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -4170,3 +4189,40 @@ resource "aws_subnet" "test" {
}
`, rName)
}

// must be >= m3 and have an encrypted root volume to eanble hibernation
const testAccInstanceConfigHibernation = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-instance-hibernation"
}
}

resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
tags = {
Name = "tf-acc-instance-hibernation"
}
}

resource "aws_instance" "foo" {
ami = "${aws_ami_copy.encrypted_ami.id}"
instance_type = "m3.medium"
subnet_id = "${aws_subnet.foo.id}"
hibernation = "true"
}

resource "aws_ami_copy" "encrypted_ami" {
name = "terraform-testacc-encrypted-ami"
description = "An encrypted AMI for Terraform acceptance testing"
source_ami_id = "ami-01e24be29428c15b2"
encrypted = "true"
source_ami_region = "us-west-2"

tags {
Name = "terraform-testacc-instance-hibernation"
}
}
Comment on lines +4210 to +4227
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the introduction of this pull request, EC2 now supports launching encrypted EBS volumes from unencrypted AMIs. Will refactor this on merge. 👍

`
1 change: 1 addition & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ instances. See [Shutdown Behavior](https://docs.aws.amazon.com/AWSEC2/latest/Use
"Instance Store") volumes on the instance. See [Block Devices](#block-devices) below for details.
* `network_interface` - (Optional) Customize network interfaces to be attached at instance boot time. See [Network Interfaces](#network-interfaces) below for more details.
* `credit_specification` - (Optional) Customize the credit specification of the instance. See [Credit Specification](#credit-specification) below for more details.
* `hibernation` - (Optional) If true, the launched EC2 instance will support hibernation.

### Timeouts

Expand Down