diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index bacfe14d3f5..563a4b326c6 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -254,6 +254,12 @@ func resourceAwsInstance() *schema.Resource { Optional: true, }, + "hibernation": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "monitoring": { Type: schema.TypeBool, Optional: true, @@ -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, } @@ -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) @@ -1798,6 +1809,7 @@ type awsInstanceOpts struct { UserData64 *string CreditSpecification *ec2.CreditSpecificationRequest CpuOptions *ec2.CpuOptionsRequest + HibernationOptions *ec2.HibernationOptionsRequest } func buildAwsInstanceOpts( @@ -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. diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 33bf243c937..18ca52252f1 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -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 { @@ -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" + } +} +` diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index 10c7035f969..b124c889214 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -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