From 1ed9590757efd4da7dd31be4a82900adf629bc73 Mon Sep 17 00:00:00 2001 From: xpk Date: Mon, 5 Sep 2022 18:32:37 +0800 Subject: [PATCH] NEW: ec2 module --- modules/compute/ec2/main.tf | 31 +++++++++++++++++++++++++++++++ modules/compute/ec2/outputs.tf | 6 ++++++ modules/compute/ec2/variable.tf | 25 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 modules/compute/ec2/main.tf create mode 100644 modules/compute/ec2/outputs.tf create mode 100644 modules/compute/ec2/variable.tf diff --git a/modules/compute/ec2/main.tf b/modules/compute/ec2/main.tf new file mode 100644 index 0000000..893725a --- /dev/null +++ b/modules/compute/ec2/main.tf @@ -0,0 +1,31 @@ +resource "aws_instance" "ec2-instance" { + ami = var.ami-id + instance_type = var.instance-type + associate_public_ip_address = var.asso-public-ip + // availability_zone = var.az + iam_instance_profile = var.instance-profile + key_name = var.key-name + root_block_device { + encrypted = var.ebs-encrypted + volume_size = var.root-volume-size + volume_type = var.root-volume-type + } + ebs_optimized = true + subnet_id = var.subnet-id + vpc_security_group_ids = var.security-groups + tags = merge(var.additional_tags, var.default-tags, + { Name = var.instance-name } + ) + volume_tags = merge(var.additional_tags, var.default-tags, + { Name = var.instance-name } + ) +} + +resource "aws_eip" "ec2-eip" { + count = var.asso-eip ? 1 : 0 + instance = aws_instance.ec2-instance.id + vpc = true + tags = merge(var.default-tags, + { Name = var.instance-name } + ) +} \ No newline at end of file diff --git a/modules/compute/ec2/outputs.tf b/modules/compute/ec2/outputs.tf new file mode 100644 index 0000000..5f8b258 --- /dev/null +++ b/modules/compute/ec2/outputs.tf @@ -0,0 +1,6 @@ +output ec2-id-ip { + value = { + instance-id = aws_instance.ec2-instance.id + private-ip = aws_instance.ec2-instance.private_ip + } +} diff --git a/modules/compute/ec2/variable.tf b/modules/compute/ec2/variable.tf new file mode 100644 index 0000000..5afd27f --- /dev/null +++ b/modules/compute/ec2/variable.tf @@ -0,0 +1,25 @@ +variable instance-type {} +variable ami-id {} +variable asso-public-ip {} +// variable az {} +variable instance-profile { + type = string + default = "" +} +variable key-name {} +variable ebs-encrypted {} +variable root-volume-size {} +variable root-volume-type { + type = string + default = "gp3" +} +variable subnet-id {} +variable security-groups { + type = list +} +variable instance-name {} +variable additional_tags {} +variable asso-eip { + type = bool +} +variable default-tags {} \ No newline at end of file