Skip to content

Resource: awsEc2InstanceState

Provides an EC2 instance state resource. This allows managing an instance power state.

\~> NOTE on Instance State Management: AWS does not currently have an EC2 API operation to determine an instance has finished processing user data. As a result, this resource can interfere with user data processing. For example, this resource may stop an instance while the user data script is in mid run.

Example Usage

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const dataAwsAmiUbuntu = new aws.dataAwsAmi.DataAwsAmi(this, "ubuntu", {
  filter: [
    {
      name: "name",
      values: ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"],
    },
    {
      name: "virtualization-type",
      values: ["hvm"],
    },
  ],
  mostRecent: true,
  owners: ["099720109477"],
});
const awsInstanceTest = new aws.instance.Instance(this, "test", {
  ami: dataAwsAmiUbuntu.id,
  instanceType: "t3.micro",
  tags: {
    Name: "HelloWorld",
  },
});
const awsEc2InstanceStateTest = new aws.ec2InstanceState.Ec2InstanceState(
  this,
  "test_2",
  {
    instanceId: awsInstanceTest.id,
    state: "stopped",
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
awsEc2InstanceStateTest.overrideLogicalId("test");

Argument Reference

The following arguments are required:

  • instanceId - (Required) ID of the instance.
  • state - (Required) - State of the instance. Valid values are stopped, running.

The following arguments are optional:

  • force - (Optional) Whether to request a forced stop when state is stopped. Otherwise (i.e., state is running), ignored. When an instance is forced to stop, it does not flush file system caches or file system metadata, and you must subsequently perform file system check and repair. Not recommended for Windows instances. Defaults to false.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • id - ID of the instance (matches instanceId).

Timeouts

Configuration options:

  • create - (Default 10M)
  • update - (Default 10M)
  • delete - (Default 1M)

Import

awsEc2InstanceState can be imported by using the instanceId attribute, e.g.,

$ terraform import aws_ec2_instance_state.test i-02cae6557dfcf2f96