Provides an EC2 instance state resource. This allows managing an instance power state.
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "test" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
resource "aws_ec2_instance_state" "test" {
instance_id = aws_instance.test.id
state = "stopped"
}
The following arguments are required:
instance_id
- (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
.This resource exports the following attributes in addition to the arguments above:
id
- ID of the instance (matches instance_id
).create
- (Default 10m
)update
- (Default 10m
)delete
- (Default 1m
)In Terraform v1.5.0 and later, use an import
block to import aws_ec2_instance_state
using the instance_id
attribute. For example:
import {
to = aws_ec2_instance_state.test
id = "i-02cae6557dfcf2f96"
}
Using terraform import
, import aws_ec2_instance_state
using the instance_id
attribute. For example:
% terraform import aws_ec2_instance_state.test i-02cae6557dfcf2f96