Created
July 27, 2022 20:55
-
-
Save ned1313/2b4918e6d3b2b50d1d5c1c6d249e614b to your computer and use it in GitHub Desktop.
AWS Terraform Example with multiple ec2 instance and data disks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main.tf | |
variable "ec2_count" { | |
type = number | |
default = 1 | |
description = "Number of EC2 instances to provision." | |
} | |
variable "provision_ec2" { | |
type = bool | |
default = false | |
description = "Provision EC2 instances or not." | |
} | |
variable "ec2_tags" { | |
type = map(string) | |
default = { | |
Provisioned_With = "TeRRaFoRM", | |
Type = "web", | |
Type_2 = "API" | |
} | |
description = "A map of tags for the EC2 instance(s)" | |
} | |
variable "host_type_list" { | |
type = list(string) | |
default = ["t3.micro", "t3.small"] | |
description = "A list of host types" | |
} | |
variable "block_device" { | |
type = list(string) | |
default = ["sdf", "sdg"] | |
description = "A list of block device names." | |
} | |
locals { | |
block_devices = flatten([ | |
for ec2 in var.host_type_list : [for device in var.block_device : | |
{ | |
instance = ec2 | |
block_device = device | |
} | |
] | |
]) | |
block_devices_map = { for d in local.block_devices : | |
"${d.instance}-${d.block_device}" => d | |
} | |
} | |
resource "aws_ebs_volume" "ec2" { | |
for_each = local.block_devices_map | |
availability_zone = aws_instance.ec2[each.value.instance].availability_zone | |
size = 1 | |
} | |
resource "aws_volume_attachment" "ec2" { | |
for_each = local.block_devices_map | |
device_name = each.value.block_device | |
volume_id = aws_ebs_volume.ec2[each.key].id | |
instance_id = aws_instance.ec2[each.value.instance].id | |
} | |
resource "aws_instance" "ec2" { | |
for_each = toset(var.host_type_list) | |
ami = data.aws_ami.ubuntu.id | |
instance_type = each.key | |
tags = { | |
for key, value in var.ec2_tags : | |
key => lower(value) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# provider.tf | |
provider "aws" { | |
region = "us-east-2" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# terraform.tfvars | |
ec2_tags = { | |
Provisioned_With = "TeRRaFoRM", | |
Type = "web", | |
Type_2 = "API" | |
Type_3 = "ReSt" | |
} | |
provision_ec2 = true | |
host_type_list = ["t3.micro", "t3.small", "t3.medium"] | |
block_device = ["sdf", "sdh"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment