FIX: corrected issue with root volume tags and updated documentation

This commit is contained in:
xpk 2024-01-30 17:00:36 +08:00
parent c2a73ed6fa
commit a1957d7de5
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
4 changed files with 145 additions and 98 deletions

View File

@ -1,45 +1,65 @@
# ec2 module <!-- This readme file is generated with terraform-docs -->
This module deploys EC2 instance. ## Requirements
# Input | Name | Version |
Below is a sample config in the root module, which shows all of the inputs |------|---------|
``` | terraform | >= 1.3.0 |
module "deployer-ec2" { | aws | ~> 5.0.0 |
source = "../../../../whk1-bea-sys-ss-dev-codecommit-sharedmodules/Compute/ec2"
ami-id = data.aws_ami.al2-ami.id ## Providers
asso-eip = false
asso-public-ip = false
default-tags = local.default_tags
ebs-encrypted = true
instance-name = "whk1-bea-sys-ss-${var.environment}-test"
instance-type = "t3.micro"
key-name = aws_key_pair.deployer-sshkey.key_name
kms-key-id = var.kms-key-arn
root-volume-size = "15"
security-groups = [aws_security_group.deployer-sg.id]
subnet-id = var.subnet-id
instance-profile = "example-instanec-profile"
additional_tags = {
"AwsBackup" : "Daily14"
"ssm-patching" : "group1"
}
data-volumes = {
volume1 = {
size : "10"
type : "gp3"
}
}
}
```
# Outputs | Name | Version |
| Name | Value | |------|---------|
| - | - | | aws | ~> 5.0.0 |
| instance-id | Instance ID |
| private-ip | Private IP of instance |
# Limitation ## Modules
Up to 26 data volumes can be attached to the ec2 instance. To attach even more volumes, please do it in
your root module
No modules.
## Resources
| Name | Type |
|------|------|
| [aws_ebs_volume.data-volumes](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ebs_volume) | resource |
| [aws_eip.ec2-eip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource |
| [aws_instance.ec2-instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
| [aws_volume_attachment.data-volume-attachments](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/volume_attachment) | resource |
| [aws_default_tags.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/default_tags) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| additional-tags | Additional tags to be assigned on top of provider default tags. Useful for setting backup tags. | `map(string)` | n/a | yes |
| ami-id | Image id of EC2 instance | `string` | n/a | yes |
| asso-eip | Whether to associate Elastic IP | `bool` | n/a | yes |
| asso-public-ip | Whether to associate ephemeral public IP | `bool` | n/a | yes |
| data-volumes | Attach additional data volumes | `map(object)` | n/a | yes |
| delete-on-termination | Whether to delete volumes on termination | `bool` | `true` | no |
| disable\_secure\_idmsv2 | If set to true, the insecure IDMSv1 will be used. | `bool` | `false` | no |
| ebs-encrypted | Whether to enable EBS encryption | `bool` | `true` | no |
| enable-detail-monitoring | Set true to enable detail monitoring | `bool` | `false` | no |
| enable-termination-protection | Whether to enable prevent accidential deletion of instance | `bool` | `false` | no |
| instance-name | Name of ec2 instance | `string` | n/a | yes |
| instance-profile | Ec2 instance profile name | `string` | `""` | no |
| instance-type | Instance type | `string` | n/a | yes |
| key-name | Instance ssh key name | `string` | `""` | no |
| kms-key-id | Disk encryption KMS key id | `string` | n/a | yes |
| private-ip | Specify private IP to be used on this instance | `string` | `null` | no |
| root-volume-size | Size of root volume | `number` | n/a | yes |
| root-volume-type | Root volume type | `string` | `"gp3"` | no |
| security-groups | List of security groups for Ec2 instance | `list(string)` | n/a | yes |
| subnet-id | Id of subnet to deploy Ec2 instance to | `string` | n/a | yes |
| user-data | Ec2 user-data | `string` | `""` | no |
## Outputs
| Name | Description |
|------|-------------|
| ec2-id-ip | n/a |
| instance-id | n/a |
| private-ip | n/a |
---
## Authorship
This module was developed by Rackspace.

View File

@ -27,27 +27,12 @@ resource "aws_instance" "ec2-instance" {
} }
} }
# spot instance option
dynamic "instance_market_options" {
for_each = var.spot-max-price > 0 ? { use_spot : true } : {}
content {
market_type = "spot"
dynamic "spot_options" {
for_each = { use_spot : true }
content {
max_price = var.spot-max-price
}
}
}
}
disable_api_termination = var.enable-termination-protection disable_api_termination = var.enable-termination-protection
user_data = var.user-data user_data = var.user-data
monitoring = var.enable-detail-monitoring monitoring = var.enable-detail-monitoring
tags = merge(var.additional-tags, { "Name" : var.instance-name }) tags = merge(var.additional-tags, { "Name" : var.instance-name })
volume_tags = merge({ "Name" : var.instance-name }, data.aws_default_tags.this.tags) volume_tags = merge({ "Name" : "${var.instance-name}-root" }, data.aws_default_tags.this.tags)
# do not redeploy instance when a new ami is released # do not redeploy instance when a new ami is released
lifecycle { lifecycle {
@ -60,8 +45,13 @@ resource "aws_ebs_volume" "data-volumes" {
availability_zone = aws_instance.ec2-instance.availability_zone availability_zone = aws_instance.ec2-instance.availability_zone
size = each.value["size"] size = each.value["size"]
type = each.value["type"] type = each.value["type"]
iops = try(each.value["iops"], null)
kms_key_id = aws_instance.ec2-instance.root_block_device[0].kms_key_id kms_key_id = aws_instance.ec2-instance.root_block_device[0].kms_key_id
encrypted = aws_instance.ec2-instance.root_block_device[0].encrypted encrypted = aws_instance.ec2-instance.root_block_device[0].encrypted
tags = merge(
{ Name : "${var.instance-name}-${each.key}" },
data.aws_default_tags.this.tags
)
} }
locals { locals {
@ -71,7 +61,6 @@ locals {
resource "aws_volume_attachment" "data-volume-attachments" { resource "aws_volume_attachment" "data-volume-attachments" {
count = length(aws_ebs_volume.data-volumes) count = length(aws_ebs_volume.data-volumes)
# for_each = aws_ebs_volume.data-volumes.id
volume_id = [for v in aws_ebs_volume.data-volumes : v.id][count.index] volume_id = [for v in aws_ebs_volume.data-volumes : v.id][count.index]
instance_id = aws_instance.ec2-instance.id instance_id = aws_instance.ec2-instance.id
device_name = "/dev/xvda${element(local.a_to_z, count.index)}" device_name = "/dev/xvda${element(local.a_to_z, count.index)}"

View File

@ -4,7 +4,7 @@ terraform {
required_providers { required_providers {
aws = { aws = {
source = "hashicorp/aws" source = "hashicorp/aws"
version = "~> 5.34.0" version = "~> 5.0.0"
} }
} }
} }

View File

@ -1,59 +1,97 @@
variable "instance-type" {} variable "instance-type" {
variable "ami-id" {} type = string
variable "asso-public-ip" {} description = "Instance type"
// variable az {} }
variable "ami-id" {
type = string
description = "Image id of EC2 instance"
}
variable "asso-public-ip" {
type = bool
description = "Whether to associate ephemeral public IP"
}
variable "instance-profile" { variable "instance-profile" {
type = string type = string
default = "" default = ""
description = "Ec2 instance profile name"
}
variable "key-name" {
type = string
description = "Instance ssh key name"
default = ""
}
variable "ebs-encrypted" {
type = bool
default = true
description = "Whether to enable EBS encryption"
}
variable "root-volume-size" {
type = number
description = "Size of root volume"
} }
variable "key-name" {}
variable "ebs-encrypted" {}
variable "root-volume-size" {}
variable "root-volume-type" { variable "root-volume-type" {
type = string type = string
default = "gp3" default = "gp3"
description = "Root volume type"
}
variable "kms-key-id" {
type = string
description = "Disk encryption KMS key id"
} }
variable "kms-key-id" {}
variable "delete-on-termination" { variable "delete-on-termination" {
type = bool type = bool
default = true default = true
description = "Whether to delete volumes on termination"
}
variable "subnet-id" {
type = string
description = "Id of subnet to deploy Ec2 instance to"
} }
variable "subnet-id" {}
variable "security-groups" { variable "security-groups" {
type = list(any) type = list(string)
description = "List of security groups for Ec2 instance"
}
variable "instance-name" {
type = string
description = "Name of ec2 instance"
} }
variable "instance-name" {}
variable "asso-eip" { variable "asso-eip" {
type = bool type = bool
description = "Whether to associate Elastic IP"
}
variable "data-volumes" {
type = map(object({
size = number
type = string
}))
description = "Attach additional data volumes"
} }
variable "data-volumes" {}
variable "private-ip" { variable "private-ip" {
type = string type = string
default = null default = null
description = "Specify private IP to be used on this instance"
}
variable "additional-tags" {
type = map(string)
description = "Additional tags to be assigned on top of provider default tags. Useful for setting backup tags."
} }
variable "additional-tags" {}
variable "disable_secure_idmsv2" { variable "disable_secure_idmsv2" {
type = bool type = bool
default = false default = false
description = "If set to true, the insecure IDMSv1 will be used."
} }
variable "enable-termination-protection" { variable "enable-termination-protection" {
type = bool type = bool
default = false default = false
description = "Whether to enable prevent accidential deletion of instance"
} }
variable "user-data" { variable "user-data" {
type = string type = string
default = "" default = ""
description = "Ec2 user-data"
} }
variable "enable-detail-monitoring" { variable "enable-detail-monitoring" {
type = bool type = bool
default = false default = false
} description = "Set true to enable detail monitoring"
variable spot-max-price {
type = number
default = 0
description = "Specify max price for spot instance."
} }