1
0

initial commit

This commit is contained in:
xpk
2026-02-13 15:44:24 +08:00
parent 66be8224f4
commit 09ce4c881a
570 changed files with 61807 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
<!-- This readme file is generated with terraform-docs -->
# LaunchTemplate
This module created EC2 launch template. If a single instance type is specified
it will create launch template with that instance type. If multiple types are specified
then a launch template with instance\_requirements will be created.
Root ebs volume is always encrypted - either with the aws/ebs key or a customer managed key
## Requirements
| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| aws | >= 5.0 |
## Providers
| Name | Version |
|------|---------|
| aws | >= 5.0 |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [aws_launch_template.lt](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
| [aws_ami.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| cpu\_count\_max | Maximum vcpu count for setting up instance\_requirements | `number` | `null` | no |
| cpu\_count\_min | Minimum vcpu count for setting up instance\_requirements | `number` | `null` | no |
| description | Description of launch template | `string` | n/a | yes |
| ebs\_volume\_kms\_key\_id | KMS key id for EBS encryption - a default key will be used if not specified | `string` | `null` | no |
| image\_id | AMI id of launch template | `string` | n/a | yes |
| imdsv2\_required | Use IMDSv2 for ec2 instance | `bool` | `true` | no |
| instance\_initiated\_shutdown\_behavior | Shutdown behavior for the instance - stop (default) or terminate | `string` | `"stop"` | no |
| instance\_profile\_name | Name of iam instance profile | `string` | `null` | no |
| instance\_types | Types of instances allowed for this launch template | `list(string)` | n/a | yes |
| key\_name | Name of keypair | `string` | `null` | no |
| mem\_mib\_max | Maximum memory size (mib) for setting up instance\_requirements | `number` | `null` | no |
| mem\_mib\_min | Minimum memory size (mib) for setting up instance\_requirements | `number` | `null` | no |
| name | Name of launch template | `string` | n/a | yes |
| root\_volume\_size | Size of root volume in GB | `number` | n/a | yes |
| root\_volume\_type | Root volume type - default gp3 | `string` | `"gp3"` | no |
| security\_grouo\_ids | List of security group ids | `list(string)` | `[]` | no |
| tag\_specifications | Tags to be added to instance and volume | `map(string)` | n/a | yes |
| update\_default\_version | Point default version to the latest | `bool` | `true` | no |
| userdata\_base64 | Base64 encoded userdata | `string` | n/a | yes |
## Outputs
| Name | Description |
|------|-------------|
| launch\_template\_id | ID of launch template |
---
## Authorship
This module was developed by xpk.
+84
View File
@@ -0,0 +1,84 @@
/**
* # LaunchTemplate
*
* This module created EC2 launch template. If a single instance type is specified
* it will create launch template with that instance type. If multiple types are specified
* then a launch template with instance_requirements will be created.
*
* Root ebs volume is always encrypted - either with the aws/ebs key or a customer managed key
*/
data "aws_ami" "this" {
filter {
name = "image-id"
values = [var.image_id]
}
}
resource "aws_launch_template" "template" {
name = var.name
description = var.description
image_id = var.image_id
instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
key_name = var.key_name
vpc_security_group_ids = var.security_grouo_ids
user_data = var.userdata_base64
update_default_version = var.update_default_version
iam_instance_profile {
name = var.instance_profile_name
}
monitoring {
enabled = true
}
dynamic "tag_specifications" {
for_each = toset(["instance", "volume"])
content {
resource_type = tag_specifications.value
tags = merge(var.tag_specifications, {
os_platform = coalesce(data.aws_ami.this.platform, "Linux")
architecture = data.aws_ami.this.architecture
ami_name = data.aws_ami.this.name
})
}
}
block_device_mappings {
device_name = data.aws_ami.this.platform == "Windows" ? "/dev/sda1" : "/dev/xvda"
ebs {
volume_size = var.root_volume_size
volume_type = var.root_volume_type
delete_on_termination = true
encrypted = true
kms_key_id = var.ebs_volume_kms_key_id
}
}
dynamic "metadata_options" {
for_each = var.imdsv2_required ? [1] : []
content {
http_endpoint = "enabled" # Enables instance metadata service endpoint
http_tokens = "required" # Enforces IMDSv2
http_put_response_hop_limit = 2 # 1 default, 2 for containers
}
}
instance_type = length(var.instance_types) == 1 ? var.instance_types[0] : null
dynamic "instance_requirements" {
for_each = length(var.instance_types) > 1 ? [1] : []
content {
vcpu_count {
min = var.cpu_count_min
max = var.cpu_count_max
}
memory_mib {
min = var.mem_mib_min
max = var.mem_mib_max
}
allowed_instance_types = var.instance_types
}
}
}
@@ -0,0 +1,4 @@
output launch_template_id {
description = "ID of launch template"
value = aws_launch_template.template.id
}
+110
View File
@@ -0,0 +1,110 @@
variable "instance_initiated_shutdown_behavior" {
default = "stop"
type = string
description = "Shutdown behavior for the instance - stop (default) or terminate"
}
variable "name" {
type = string
description = "Name of launch template"
}
variable "description" {
type = string
description = "Description of launch template"
}
variable "image_id" {
type = string
description = "AMI id of launch template"
}
variable "key_name" {
type = string
description = "Name of keypair"
default = null
}
variable "security_grouo_ids" {
type = list(string)
description = "List of security group ids"
default = []
}
variable "userdata_base64" {
type = string
description = "Base64 encoded userdata"
validation {
condition = can(base64decode(var.userdata_base64))
error_message = "Userdata must be encoded in base64"
}
}
variable "tag_specifications" {
type = map(string)
description = "Tags to be added to instance and volume"
}
variable "root_volume_size" {
type = number
description = "Size of root volume in GB"
}
variable "root_volume_type" {
default = "gp3"
type = string
description = "Root volume type - default gp3"
}
variable "ebs_volume_kms_key_id" {
type = string
description = "KMS key id for EBS encryption - a default key will be used if not specified"
default = null
}
variable "imdsv2_required" {
default = true
type = bool
description = "Use IMDSv2 for ec2 instance"
}
variable "instance_types" {
type = list(string)
description = "Types of instances allowed for this launch template"
}
variable "cpu_count_min" {
type = number
description = "Minimum vcpu count for setting up instance_requirements"
default = null
}
variable "cpu_count_max" {
type = number
description = "Maximum vcpu count for setting up instance_requirements"
default = null
}
variable "mem_mib_min" {
type = number
description = "Minimum memory size (mib) for setting up instance_requirements"
default = null
}
variable "mem_mib_max" {
type = number
description = "Maximum memory size (mib) for setting up instance_requirements"
default = null
}
variable "update_default_version" {
type = bool
default = true
description = "Point default version to the latest"
}
variable "instance_profile_name" {
type = string
description = "Name of iam instance profile"
default = null
}
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.3.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}