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 module deploys EC2 instance.
<!-- This readme file is generated with terraform-docs -->
## Requirements
# Input
Below is a sample config in the root module, which shows all of the inputs
```
module "deployer-ec2" {
source = "../../../../whk1-bea-sys-ss-dev-codecommit-sharedmodules/Compute/ec2"
| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| aws | ~> 5.0.0 |
ami-id = data.aws_ami.al2-ami.id
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"
}
}
}
```
## Providers
# Outputs
| Name | Value |
| - | - |
| instance-id | Instance ID |
| private-ip | Private IP of instance |
| Name | Version |
|------|---------|
| aws | ~> 5.0.0 |
# Limitation
Up to 26 data volumes can be attached to the ec2 instance. To attach even more volumes, please do it in
your root module
## Modules
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
user_data = var.user-data
monitoring = var.enable-detail-monitoring
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
lifecycle {
@ -60,8 +45,13 @@ resource "aws_ebs_volume" "data-volumes" {
availability_zone = aws_instance.ec2-instance.availability_zone
size = each.value["size"]
type = each.value["type"]
iops = try(each.value["iops"], null)
kms_key_id = aws_instance.ec2-instance.root_block_device[0].kms_key_id
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 {
@ -70,8 +60,7 @@ locals {
resource "aws_volume_attachment" "data-volume-attachments" {
count = length(aws_ebs_volume.data-volumes)
# for_each = aws_ebs_volume.data-volumes.id
count = length(aws_ebs_volume.data-volumes)
volume_id = [for v in aws_ebs_volume.data-volumes : v.id][count.index]
instance_id = aws_instance.ec2-instance.id
device_name = "/dev/xvda${element(local.a_to_z, count.index)}"

View File

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

View File

@ -1,59 +1,97 @@
variable "instance-type" {}
variable "ami-id" {}
variable "asso-public-ip" {}
// variable az {}
variable "instance-type" {
type = string
description = "Instance type"
}
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" {
type = string
default = ""
type = string
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" {
type = string
default = "gp3"
type = string
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" {
type = bool
default = true
type = bool
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" {
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" {
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" {
type = string
default = null
type = string
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" {
type = bool
default = false
type = bool
default = false
description = "If set to true, the insecure IDMSv1 will be used."
}
variable "enable-termination-protection" {
type = bool
default = false
type = bool
default = false
description = "Whether to enable prevent accidential deletion of instance"
}
variable "user-data" {
type = string
default = ""
type = string
default = ""
description = "Ec2 user-data"
}
variable "enable-detail-monitoring" {
type = bool
default = false
}
variable spot-max-price {
type = number
default = 0
description = "Specify max price for spot instance."
type = bool
default = false
description = "Set true to enable detail monitoring"
}