variable "EC2_ROOT_VOLUME_SIZE" {
type = "string"
default = "30"
description = "The volume size for the root volume in GiB"
}
variable "EC2_ROOT_VOLUME_TYPE" {
type = "string"
default = "gp2"
description = "The type of data storage: standard, gp2, io1"
}
variable "EC2_ROOT_VOLUME_DELETE_ON_TERMINATION" {
default = true
description = "Delete the root volume on instance termination."
}
# then
resource "aws_instance" "example" {
ami = "${var.AMI_ID}"
instance_type = "${var.EC2_INSTANCE_SIZE}"
root_block_device {
volume_size = "${var.EC2_ROOT_VOLUME_SIZE}"
volume_type = "${var.EC2_ROOT_VOLUME_TYPE}"
delete_on_termination = "${var.EC2_ROOT_VOLUME_DELETE_ON_TERMINATION}"
}
}
Create a security group and use it afterwards (Allow income from 8080)
resource "aws_security_group" "http_group" {
name = "terraform-example-instance"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# add the property
# vpc_security_group_ids = ["${aws_security_group.http_group.id}"]
# in the aws_instance
Create a security group for ssh access (Experimental)
resource "aws_security_group" "ssh_group" {
name = "terraform-example-instance"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# add the property
# vpc_security_group_ids = ["${aws_security_group.ssh_group.id}"]
# in the aws_instance
Stoping/Starting instances with AWS CLI
aws ec2 stop-instances --region us-east-2 --instance-ids i-0123456789abcdef
aws ec2 start-instances --region us-east-2 --instance-ids i-0123456789abcdef
#If you need to fetch the instance ID quickly, you can define a TF output and get at it that way:
terraform output id
# i-0123456789abcdef
References
[Terraform up and running (local book)](/media/w/6529BB496A1EC696/Yevgeniy Brikman - Terraform Up and Running (Early Release)-O'Reilly Media (2017).pdf)