Ubuntu server 18.04 (HVM) 64bits x86: ami-0a63f96e85105c6d3
Recipes
Setup AWS instances
Get AWS access key/AWS secret key
execute aws configure using credentials from 1.
execute terraform init when starting new project at the root of the directory
Get list of AMI IDs matching criteria
variable"image_name"{description ="The name of the image to use"default ="ubuntu-*-18.04*"}provider"aws"{region ="us-east-2"}data"aws_ami""images"{owners =["amazon"]most_recent =truefilter{name ="name"values =[var.image_name]}}output"ids"{value ="\nName: ${data.aws_ami.images.name}\nId: ${data.aws_ami.images.id}"
Single ec2 with ubuntu 18.04 x86
Create and attach ebs storage
aws_ebs_volume and aws_instance ideally belong to the same availability_zone
Define storage from ec2 creation
Create a security group and use it afterwards (Allow income from 8080)
Create a security group for ssh access (Experimental)
Stoping/Starting instances with AWS CLI
References
[Terraform up and running (local book)](/media/w/6529BB496A1EC696/Yevgeniy Brikman - Terraform Up and Running (Early Release)-O'Reilly Media (2017).pdf)
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}"
}
}
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
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
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