Skip to content

Commit f8a5316

Browse files
authored
fix: auto-detect the root device name from the AMI (#43)
## what - This PR removes the current hard-coded block device mapping configuration value and allows for automatic detection of the root device name from the provided AMI. ## why - Currently, the block device configuration has the "/dev/xvda" root device name hard-coded. Although this setting is true for newer images, some AMIs use different device names (e.g. Ubuntu often uses "/dev/sda1". - This PR fixes the "incompatibility" of the "ami" variable and this hard-coded setting. For example, if anyone provides an Ubuntu AMI, this leads to AWS autoscaling group being "stuck" and instances won't spawn, with the following error: ``` Launching a new EC2 instance. Status Reason: The request must contain the parameter size or snapshotId. Launching EC2 instance failed. ``` ## references - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - The root volume device name is now set dynamically based on the selected AMI, allowing for compatibility with custom AMIs. - **Improvements** - Enhanced flexibility when specifying a custom AMI by automatically adjusting the root device name in launch templates. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent c2cdfa3 commit f8a5316

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

data.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ data "aws_ami" "amazon_linux_2023" {
2121
values = ["hvm"]
2222
}
2323
}
24+
25+
# A trunk-ignore rule is added here because the "owners" argument for this data resource is optional
26+
# (as per the Terraform provider docs) and is intentionally omitted, since the consumer of this
27+
# module can specify an arbitrary AMI ID as input. Therefore, the security of the AMI is a concern
28+
# for the consumer. According to the AWS docs, if this value is not specified, the results include
29+
# all images for which the caller has launch permissions.
30+
#
31+
# AWS docs: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html.
32+
#
33+
# This rule was introduced in the following PR:
34+
# https://github.com/masterpointio/terraform-aws-ssm-agent/pull/43.
35+
#
36+
# trunk-ignore(trivy/AVD-AWS-0344)
37+
data "aws_ami" "instance" {
38+
count = length(var.ami) > 0 ? 1 : 0
39+
40+
most_recent = true
41+
42+
filter {
43+
name = "image-id"
44+
values = [var.ami]
45+
}
46+
}

main.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ locals {
88
# True if contains 'g' in the third position when architecture is arm64
99
(var.architecture == "arm64" && element(local.instance_type_chars, 2) == "g")
1010
)
11+
12+
# Get the root device name from the provided/default AMI.
13+
root_volume_device_name = (
14+
length(var.ami) > 0 ? element(data.aws_ami.instance, 0).root_device_name : "/dev/xvda"
15+
)
16+
1117
}
1218

1319
resource "null_resource" "validate_instance_type" {
@@ -347,7 +353,7 @@ resource "aws_launch_template" "default" {
347353
}
348354

349355
block_device_mappings {
350-
device_name = "/dev/xvda"
356+
device_name = local.root_volume_device_name
351357
ebs {
352358
encrypted = true
353359
volume_size = var.volume_size

0 commit comments

Comments
 (0)