Skip to content

Commit c59b323

Browse files
Jordan SeveranceJordan Severance
authored andcommitted
SCE-345 - Standardized name format, AMI OS lookup function
update README remove unneeded file
1 parent 80b74fd commit c59b323

File tree

7 files changed

+161
-33
lines changed

7 files changed

+161
-33
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ The following combinations are supported to conditionally create resources:
173173

174174
## Modules
175175

176-
No modules.
176+
| Name | Source | Version |
177+
|------|--------|---------|
178+
| <a name="module_key-pair"></a> [key-pair](#module\_key-pair) | app.terraform.io/sccm/key-pair-creation/aws | 0.0.1 |
177179

178180
## Resources
179181

@@ -186,6 +188,8 @@ No modules.
186188
| [aws_instance.ignore_ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
187189
| [aws_instance.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
188190
| [aws_spot_instance_request.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/spot_instance_request) | resource |
191+
| [aws_ami.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
192+
| [aws_ami.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
189193
| [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
190194
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
191195
| [aws_ssm_parameter.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
@@ -195,7 +199,9 @@ No modules.
195199
| Name | Description | Type | Default | Required |
196200
|------|-------------|------|---------|:--------:|
197201
| <a name="input_ami"></a> [ami](#input\_ami) | ID of AMI to use for the instance | `string` | `null` | no |
202+
| <a name="input_ami_os"></a> [ami\_os](#input\_ami\_os) | value | `string` | `"override"` | no |
198203
| <a name="input_ami_ssm_parameter"></a> [ami\_ssm\_parameter](#input\_ami\_ssm\_parameter) | SSM parameter name for the AMI ID. For Amazon Linux AMI SSM parameters see [reference](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-ami.html) | `string` | `"/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"` | no |
204+
| <a name="input_application"></a> [application](#input\_application) | Identifier to be added to the resources created which represents the application they belong to | `string` | n/a | yes |
199205
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Whether to associate a public IP address with an instance in a VPC | `bool` | `null` | no |
200206
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | AZ to start the instance in | `string` | `null` | no |
201207
| <a name="input_capacity_reservation_specification"></a> [capacity\_reservation\_specification](#input\_capacity\_reservation\_specification) | Describes an instance's Capacity Reservation targeting option | `any` | `{}` | no |
@@ -215,11 +221,12 @@ No modules.
215221
| <a name="input_eip_tags"></a> [eip\_tags](#input\_eip\_tags) | A map of additional tags to add to the eip | `map(string)` | `{}` | no |
216222
| <a name="input_enable_volume_tags"></a> [enable\_volume\_tags](#input\_enable\_volume\_tags) | Whether to enable volume tags (if enabled it conflicts with root\_block\_device tags) | `bool` | `true` | no |
217223
| <a name="input_enclave_options_enabled"></a> [enclave\_options\_enabled](#input\_enclave\_options\_enabled) | Whether Nitro Enclaves will be enabled on the instance. Defaults to `false` | `bool` | `null` | no |
224+
| <a name="input_environment"></a> [environment](#input\_environment) | Application environment (dev, qa, stg, uat, prod) | `string` | n/a | yes |
218225
| <a name="input_ephemeral_block_device"></a> [ephemeral\_block\_device](#input\_ephemeral\_block\_device) | Customize Ephemeral (also known as Instance Store) volumes on the instance | `list(map(string))` | `[]` | no |
219226
| <a name="input_get_password_data"></a> [get\_password\_data](#input\_get\_password\_data) | If true, wait for password data to become available and retrieve it | `bool` | `null` | no |
220227
| <a name="input_hibernation"></a> [hibernation](#input\_hibernation) | If true, the launched EC2 instance will support hibernation | `bool` | `null` | no |
221228
| <a name="input_host_id"></a> [host\_id](#input\_host\_id) | ID of a dedicated host that the instance will be assigned to. Use when an instance is to be launched on a specific dedicated host | `string` | `null` | no |
222-
| <a name="input_iam_instance_profile"></a> [iam\_instance\_profile](#input\_iam\_instance\_profile) | IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile | `string` | `null` | no |
229+
| <a name="input_iam_instance_profile"></a> [iam\_instance\_profile](#input\_iam\_instance\_profile) | IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile | `string` | `"EC2DefaultProfile"` | no |
223230
| <a name="input_iam_role_description"></a> [iam\_role\_description](#input\_iam\_role\_description) | Description of the role | `string` | `null` | no |
224231
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name to use on IAM role created | `string` | `null` | no |
225232
| <a name="input_iam_role_path"></a> [iam\_role\_path](#input\_iam\_role\_path) | IAM role path | `string` | `null` | no |
@@ -229,6 +236,7 @@ No modules.
229236
| <a name="input_iam_role_use_name_prefix"></a> [iam\_role\_use\_name\_prefix](#input\_iam\_role\_use\_name\_prefix) | Determines whether the IAM role name (`iam_role_name` or `name`) is used as a prefix | `bool` | `true` | no |
230237
| <a name="input_ignore_ami_changes"></a> [ignore\_ami\_changes](#input\_ignore\_ami\_changes) | Whether changes to the AMI ID changes should be ignored by Terraform. Note - changing this value will result in the replacement of the instance | `bool` | `false` | no |
231238
| <a name="input_instance_initiated_shutdown_behavior"></a> [instance\_initiated\_shutdown\_behavior](#input\_instance\_initiated\_shutdown\_behavior) | Shutdown behavior for the instance. Amazon defaults this to stop for EBS-backed instances and terminate for instance-store instances. Cannot be set on instance-store instance | `string` | `null` | no |
239+
| <a name="input_instance_number"></a> [instance\_number](#input\_instance\_number) | This is an identifier, not a count | `string` | `"01"` | no |
232240
| <a name="input_instance_tags"></a> [instance\_tags](#input\_instance\_tags) | Additional tags for the instance | `map(string)` | `{}` | no |
233241
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The type of instance to start | `string` | `"t3.micro"` | no |
234242
| <a name="input_ipv6_address_count"></a> [ipv6\_address\_count](#input\_ipv6\_address\_count) | A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet | `number` | `null` | no |
@@ -238,13 +246,13 @@ No modules.
238246
| <a name="input_maintenance_options"></a> [maintenance\_options](#input\_maintenance\_options) | The maintenance options for the instance | `any` | `{}` | no |
239247
| <a name="input_metadata_options"></a> [metadata\_options](#input\_metadata\_options) | Customize the metadata options of the instance | `map(string)` | <pre>{<br/> "http_endpoint": "enabled",<br/> "http_put_response_hop_limit": 1,<br/> "http_tokens": "optional"<br/>}</pre> | no |
240248
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled | `bool` | `null` | no |
241-
| <a name="input_name"></a> [name](#input\_name) | Name to be used on EC2 instance created | `string` | `""` | no |
242249
| <a name="input_network_interface"></a> [network\_interface](#input\_network\_interface) | Customize network interfaces to be attached at instance boot time | `list(map(string))` | `[]` | no |
250+
| <a name="input_org"></a> [org](#input\_org) | n/a | `string` | `"sccm"` | no |
243251
| <a name="input_placement_group"></a> [placement\_group](#input\_placement\_group) | The Placement Group to start the instance in | `string` | `null` | no |
244252
| <a name="input_private_dns_name_options"></a> [private\_dns\_name\_options](#input\_private\_dns\_name\_options) | Customize the private DNS name options of the instance | `map(string)` | `{}` | no |
245253
| <a name="input_private_ip"></a> [private\_ip](#input\_private\_ip) | Private IP address to associate with the instance in a VPC | `string` | `null` | no |
246254
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
247-
| <a name="input_root_block_device"></a> [root\_block\_device](#input\_root\_block\_device) | Customize details about the root block device of the instance. See [Block Devices](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#ebs-ephemeral-and-root-block-devices) for details | `list(any)` | `[]` | no |
255+
| <a name="input_root_block_device"></a> [root\_block\_device](#input\_root\_block\_device) | Customize details about the root block device of the instance. See Block Devices below for details | `list(any)` | `[]` | no |
248256
| <a name="input_secondary_private_ips"></a> [secondary\_private\_ips](#input\_secondary\_private\_ips) | A list of secondary private IPv4 addresses to assign to the instance's primary network interface (eth0) in a VPC. Can only be assigned to the primary network interface (eth0) attached at instance creation, not a pre-existing network interface i.e. referenced in a `network_interface block` | `list(string)` | `null` | no |
249257
| <a name="input_source_dest_check"></a> [source\_dest\_check](#input\_source\_dest\_check) | Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs | `bool` | `null` | no |
250258
| <a name="input_spot_block_duration_minutes"></a> [spot\_block\_duration\_minutes](#input\_spot\_block\_duration\_minutes) | The required duration for the Spot instances, in minutes. This value must be a multiple of 60 (60, 120, 180, 240, 300, or 360) | `number` | `null` | no |

data.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
data "aws_ami" "this" {
2+
count = var.ami == null ? 0 : 1
3+
filter {
4+
name = "image-id"
5+
values = [var.ami]
6+
}
7+
}
8+
9+
data "aws_partition" "current" {}
10+
11+
data "aws_ssm_parameter" "this" {
12+
count = local.create && var.ami == null ? 1 : 0
13+
14+
name = var.ami_ssm_parameter
15+
}
16+
17+
data "aws_ami" "selected" {
18+
count = var.ami_os != "override" ? 1 : 0
19+
20+
most_recent = true
21+
owners = ["amazon"]
22+
filter {
23+
name = "name"
24+
values = [local.os_search]
25+
}
26+
filter {
27+
name = "root-device-type"
28+
values = ["ebs"]
29+
}
30+
31+
filter {
32+
name = "virtualization-type"
33+
values = ["hvm"]
34+
}
35+
}

examples/customizations/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.66 |
8+
9+
## Providers
10+
11+
No providers.
12+
13+
## Modules
14+
15+
| Name | Source | Version |
16+
|------|--------|---------|
17+
| <a name="module_instance"></a> [instance](#module\_instance) | ../../ | n/a |
18+
19+
## Resources
20+
21+
No resources.
22+
23+
## Inputs
24+
25+
No inputs.
26+
27+
## Outputs
28+
29+
No outputs.
30+
<!-- END_TF_DOCS -->

examples/customizations/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module "instance" {
2-
source = "../../"
2+
source = "../../"
33

4-
name = "example name"
4+
application = "exampleapp"
5+
environment = "dev"
6+
ami_os = "Amazon_Linux"
57
instance_type = "r5.large"
6-
ami = "ami-04426a869f59d0d45"
78
key_name = "example_key"
89
vpc_security_group_ids = ["sg-07b4edce8a1a6eb24"]
910
subnet_id = "subnet-067f45f707b2dc297"

locals.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
locals {
2+
create = var.create && var.putin_khuylo
3+
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false
4+
ami = try(coalesce(var.ami, try(data.aws_ami.selected[0].id, null), try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
5+
name = "${var.org}_${var.application}_${local.os_abv}_${var.instance_number}${local.env_abv}"
6+
windows_instance = var.ami != null && var.ami_os == "override" ? (data.aws_ami.this[0].platform != "" ? "WIN" : "LX") : "ovr"
7+
env_abv = lookup(
8+
{
9+
dev = "D",
10+
qa = "Q",
11+
stg = "S",
12+
uat = "U",
13+
prod = "P"
14+
},
15+
var.environment,
16+
var.environment
17+
)
18+
os_abv = lookup(
19+
{
20+
Windows = "WIN",
21+
Amazon_Linux = "AL",
22+
RHEL = "RHEL",
23+
Ubuntu = "UB",
24+
override = local.windows_instance
25+
},
26+
var.ami_os,
27+
var.ami_os
28+
)
29+
os_search = lookup(
30+
{
31+
Windows = "Windows_Server-2025-English-Full-Base-*"
32+
Amazon_Linux = "amzn2-ami-kernel-5.10-hvm-*",
33+
RHEL = "RHEL-9.5.0_HVM-*",
34+
Ubuntu = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*",
35+
override = ""
36+
},
37+
var.ami_os
38+
)
39+
}
40+

main.tf

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
data "aws_partition" "current" {}
2-
3-
locals {
4-
create = var.create && var.putin_khuylo
5-
6-
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false
7-
8-
ami = try(coalesce(var.ami, try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
9-
}
10-
11-
data "aws_ssm_parameter" "this" {
12-
count = local.create && var.ami == null ? 1 : 0
13-
14-
name = var.ami_ssm_parameter
15-
}
16-
171
################################################################################
182
# Instance
193
################################################################################
@@ -188,8 +172,8 @@ resource "aws_instance" "this" {
188172
delete = try(var.timeouts.delete, null)
189173
}
190174

191-
tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
192-
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
175+
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
176+
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null
193177
}
194178

195179
################################################################################
@@ -366,8 +350,8 @@ resource "aws_instance" "ignore_ami" {
366350
delete = try(var.timeouts.delete, null)
367351
}
368352

369-
tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
370-
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
353+
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
354+
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null
371355

372356
lifecycle {
373357
ignore_changes = [
@@ -540,16 +524,16 @@ resource "aws_spot_instance_request" "this" {
540524
delete = try(var.timeouts.delete, null)
541525
}
542526

543-
tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
544-
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
527+
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
528+
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null
545529
}
546530

547531
################################################################################
548532
# IAM Role / Instance Profile
549533
################################################################################
550534

551535
locals {
552-
iam_role_name = try(coalesce(var.iam_role_name, var.name), "")
536+
iam_role_name = try(coalesce(var.iam_role_name, local.name), "")
553537
}
554538

555539
data "aws_iam_policy_document" "assume_role_policy" {

variables.tf

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@ variable "create" {
44
default = true
55
}
66

7-
variable "name" {
8-
description = "Name to be used on EC2 instance created"
7+
variable "instance_number" {
98
type = string
10-
default = ""
9+
default = "01"
10+
description = "This is an identifier, not a count"
11+
}
12+
13+
variable "org" {
14+
type = string
15+
default = "sccm"
16+
}
17+
18+
variable "application" {
19+
description = "Identifier to be added to the resources created which represents the application they belong to"
20+
type = string
21+
}
22+
23+
variable "environment" {
24+
type = string
25+
description = "Application environment (dev, qa, stg, uat, prod)"
26+
validation {
27+
condition = contains(["dev", "qa", "stg", "uat", "prod"], var.environment)
28+
error_message = "Valid values for environment: dev, qa, stg, uat, prod"
29+
}
1130
}
1231

1332
variable "ami_ssm_parameter" {
@@ -16,6 +35,16 @@ variable "ami_ssm_parameter" {
1635
default = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
1736
}
1837

38+
variable "ami_os" {
39+
description = "value"
40+
type = string
41+
default = "override"
42+
validation {
43+
condition = contains(["Windows", "Amazon_Linux", "RHEL", "Ubuntu", "override"], var.ami_os)
44+
error_message = "Valid values for ami_os: Windows, Amazon_Linux, RHEL, Ubuntu, override. If you select override, provide a value for ami variable"
45+
}
46+
}
47+
1948
variable "ami" {
2049
description = "ID of AMI to use for the instance"
2150
type = string
@@ -430,3 +459,4 @@ variable "eip_tags" {
430459
type = map(string)
431460
default = {}
432461
}
462+

0 commit comments

Comments
 (0)