Skip to content

Commit

Permalink
updated the module
Browse files Browse the repository at this point in the history
  • Loading branch information
Khalid Ahmadzai committed Aug 31, 2021
1 parent 6587b9a commit cebc81b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 20 deletions.
33 changes: 24 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ locals {
enabled = var.enabled == "true"
}

data "aws_ami" "search" {
most_recent = true

filter {
name = "virtualization-type"
values = ["hvm"]
}

name_regex = "${lookup("${var.amis_os_map_regex}", "${var.os}")}"
owners = ["${length(var.amis_primary_owners) == 0 ? lookup(var.amis_os_map_owners, var.os) : var.amis_primary_owners}"]
}

resource "aws_instance" "this" {
count = local.enabled ? 1 : 0
ami = var.ami
ami = data.aws_ami.search.id
associate_public_ip_address = var.associate_public_ip_address
disable_api_termination = var.disable_api_termination
ebs_optimized = var.ebs_optimized
iam_instance_profile = var.aws_iam_instance_profile
instance_type = var.instance_type
monitoring = var.monitoring
subnet_id = var.subnet_id
tags = var.tags
vpc_security_group_ids = var.vpc_security_group_ids
Expand All @@ -23,18 +38,18 @@ resource "aws_instance" "this" {


resource "aws_ebs_volume" "this" {
count= var.enabled_ebs_volume ? 1 : 0
size = var.ebs_volume_size
type= var.volume_type
count = var.enabled_ebs_volume ? 1 : 0
size = var.ebs_volume_size
type = var.volume_type
availability_zone = aws_instance.this.*.availability_zone[0]

}

resource "aws_volume_attachment" "this" {
count = var.enable_ebs_volume_attachment ? 1 : 0
count = var.enable_ebs_volume_attachment ? 1 : 0
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.this.*.id[0]
volume_id = aws_ebs_volume.this.*.id[0]
instance_id = aws_instance.this.*.id[0]

}

}
91 changes: 80 additions & 11 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ variable "root_volume_size" {
}

variable "ebs_volume_size" {
type = number
description = "size of ebs volume"
default = null
type = number
description = "size of ebs volume"
default = null
}

variable "volume_type" {
type = string
type = string
description = "volume_type"
default = "gp3"
default = "gp3"
}

variable "role_name" {
Expand All @@ -90,19 +90,88 @@ variable "role_name" {
}

variable "key_name" {
type = string
type = string
description = "EC2 key"
default = ""
default = ""
}

variable "vpc_security_group_ids" {
type = list(string)
type = list(string)
description = "(optional) describe your variable"
default = []
default = []
}

variable "aws_iam_instance_profile" {
type = string
type = string
description = "(optional) describe your variable"
default = null
default = null
}

variable "ebs_optimized" {
type = bool
default = false
description = "If true, the launched EC2 instance will be EBS-optimized."
}

variable "monitoring" {
type = bool
default = false
description = "If true, the launched EC2 instance will have detailed monitoring enabled. (Available since v0.6.0)."
}

variable "disable_api_termination" {
type = bool
default = false
description = "If true, enables EC2 Instance Termination Protection."
}

# AMI search

variable "os" {
description = "The Os reference to search for"
}

variable "amis_primary_owners" {
description = "Force the ami Owner, could be (self) or specific (id)"
default = ""
}

variable "amis_os_map_regex" {
description = "Map of regex to search amis"
type = map

default = {
ubuntu1804 = "^ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-.*"
ubuntu1810 = "^ubuntu/images/hvm-ssd/ubuntu-cosmic-18.10-amd64-server-.*"
ubuntu1904 = "^ubuntu/images/hvm-ssd/ubuntu-disco-19.04-amd64-server-.*"
centos7 = "CentOS.Linux.7.*x86_64.*"
centos8 = "CentOS.Linux.8.*x86_64.*"
rhel6 = "^RHEL-6.*x86_64.*"
rhel7 = "^RHEL-7.*x86_64.*"
rhel8 = "^RHEL-8.*x86_64.*"
amazon-2 = "^amzn2-ami-hvm-.*x86_64-gp2"
windows-2019-base = "^Windows_Server-2019-English-Full-Base-.*"
windows2016 = "^Windows_Server-2016-English-Full-Base-.*"
windows2012r2 = "^Windows_Server-2012-R2_RTM-English-64Bit-Base-.*"
}
}

variable "amis_os_map_owners" {
description = "Map of amis owner to filter only official amis"
type = map
default = {
ubuntu1804 = "099720109477" #CANONICAL
ubuntu1810 = "099720109477" #CANONICAL
ubuntu1904 = "099720109477" #CANONICAL
rhel6 = "309956199498" #Amazon Web Services
rhel7 = "309956199498" #Amazon Web Services
rhel8 = "309956199498" #Amazon Web Services
centos7 = "679593333241"
centos8 = "679593333241"
amazon = "137112412989" #amazon
amazon-2 = "137112412989" #amazon
windows2019 = "801119661308" #amazon
windows2016 = "801119661308" #amazon
windows2012r2 = "801119661308" #amazon
}
}

0 comments on commit cebc81b

Please sign in to comment.