Skip to content

Commit 4fcfe49

Browse files
authored
Merge branch 'main' into fix/update-ami-filter
2 parents 27ad668 + c7953f1 commit 4fcfe49

File tree

6 files changed

+157
-7
lines changed

6 files changed

+157
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Masterpoint Logo](https://i.imgur.com/RDLnuQO.png)](https://masterpoint.io)
22

3-
[![Release](https://img.shields.io/github/release/masterpointio/ecsrun.svg)](https://github.com/masterpointio/ecsrun/releases/latest)
3+
[![Release](https://img.shields.io/github/v/release/masterpointio/terraform-aws-ssm-agent.svg)](https://github.com/masterpointio/terraform-aws-ssm-agent/releases/latest)
44

55
# terraform-aws-ssm-agent
66

@@ -120,7 +120,7 @@ Use [the awesome `gossm` project](https://github.com/gjbae1212/gossm).
120120
|------|-------------|------|---------|:--------:|
121121
| <a name="input_additional_security_group_ids"></a> [additional\_security\_group\_ids](#input\_additional\_security\_group\_ids) | Security groups that will be attached to the app instances | `list(string)` | `[]` | no |
122122
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
123-
| <a name="input_ami"></a> [ami](#input\_ami) | The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2 AMI. Pin to a specific AMI if you would like to avoid these updates. | `string` | `""` | no |
123+
| <a name="input_ami"></a> [ami](#input\_ami) | The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2023 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2023 AMI. Pin to a specific AMI if you would like to avoid these updates. | `string` | `""` | no |
124124
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Associate public IP address | `bool` | `null` | no |
125125
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
126126
| <a name="input_cloudwatch_retention_in_days"></a> [cloudwatch\_retention\_in\_days](#input\_cloudwatch\_retention\_in\_days) | The number of days to retain session logs in CloudWatch. This is only relevant if the session\_logging\_enabled variable is `true`. | `number` | `365` | no |

data.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
data "aws_region" "current" {}
22
data "aws_caller_identity" "current" {}
33

4-
# Most recent Amazon Linux 2 AMI
5-
data "aws_ami" "amazon_linux_2" {
4+
# Most recent Amazon Linux 2023 AMI
5+
data "aws_ami" "amazon_linux_2023" {
66
most_recent = true
77
owners = ["amazon"]
88

@@ -13,6 +13,11 @@ data "aws_ami" "amazon_linux_2" {
1313

1414
filter {
1515
name = "architecture"
16-
values = ["x86_64"]
16+
values = [var.architecture]
17+
}
18+
19+
filter {
20+
name = "virtualization-type"
21+
values = ["hvm"]
1722
}
1823
}

main.tf

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
locals {
2+
instance_type_chars = split("", var.instance_type)
3+
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
4+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
5+
is_instance_compatible = (
6+
# True if does not contain 'g' in the third position when architecture is x86_64
7+
(var.architecture == "x86_64" && element(local.instance_type_chars, 2) != "g") ||
8+
# True if contains 'g' in the third position when architecture is arm64
9+
(var.architecture == "arm64" && element(local.instance_type_chars, 2) == "g")
10+
)
11+
}
12+
13+
resource "null_resource" "validate_instance_type" {
14+
count = local.is_instance_compatible ? 0 : 1
15+
16+
lifecycle {
17+
precondition {
18+
condition = local.is_instance_compatible
19+
error_message = "The instance_type must be compatible with the specified architecture. For x86_64, you cannot use instance types with ARM processors (e.g., t3, m5, c5). For arm64, use instance types with 'g' indicating ARM processor (e.g., t4g, c6g, m6g)."
20+
}
21+
}
22+
}
23+
124
module "role_label" {
225
source = "cloudposse/label/null"
326
version = "0.25.0"
@@ -132,6 +155,7 @@ resource "aws_security_group_rule" "allow_all_egress" {
132155
to_port = 0
133156
protocol = "-1"
134157
cidr_blocks = ["0.0.0.0/0"]
158+
ipv6_cidr_blocks = ["::/0"]
135159
security_group_id = aws_security_group.default.id
136160
}
137161

@@ -270,7 +294,7 @@ DOC
270294

271295
resource "aws_launch_template" "default" {
272296
name_prefix = module.this.id
273-
image_id = length(var.ami) > 0 ? var.ami : data.aws_ami.amazon_linux_2.id
297+
image_id = coalesce(var.ami, data.aws_ami.amazon_linux_2023.id)
274298
instance_type = var.instance_type
275299
key_name = var.key_pair_name
276300
user_data = base64encode(var.user_data)
@@ -305,6 +329,13 @@ resource "aws_launch_template" "default" {
305329
create_before_destroy = true
306330
}
307331

332+
block_device_mappings {
333+
device_name = "/dev/xvda"
334+
ebs {
335+
encrypted = true
336+
}
337+
}
338+
308339
metadata_options {
309340
http_endpoint = var.metadata_http_endpoint_enabled ? "enabled" : "disabled"
310341
http_tokens = var.metadata_imdsv2_enabled ? "required" : "optional"

tests/main.tftest.hcl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
variables {
2+
vpc_id = "vpc-12345678"
3+
subnet_ids = ["subnet-12345678", "subnet-87654321"]
4+
stage = "test"
5+
namespace = "mp"
6+
name = "ssm-agent"
7+
region = "us-east-1"
8+
availability_zones = ["us-east-1a"]
9+
nat_gateway_enabled = true
10+
ipv6_enabled = true
11+
}
12+
13+
### TESTING INSTANCE and ARCHITECTURE COMPATIBILITY ###
14+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
15+
# https://aws.amazon.com/ec2/instance-types/
16+
17+
# Test valid x86_64 instance type
18+
run "valid_x86_64_instance" {
19+
command = plan
20+
21+
variables {
22+
instance_type = "t3.micro"
23+
architecture = "x86_64"
24+
}
25+
26+
assert {
27+
condition = local.is_instance_compatible
28+
error_message = "Expected instance type t3.micro to be compatible with x86_64 architecture"
29+
}
30+
}
31+
32+
# Test valid arm64 instance type
33+
run "valid_arm64_instance" {
34+
command = plan
35+
36+
variables {
37+
instance_type = "t4g.micro"
38+
architecture = "arm64"
39+
}
40+
41+
assert {
42+
condition = local.is_instance_compatible
43+
error_message = "Expected instance type t4g.micro to be compatible with arm64 architecture"
44+
}
45+
}
46+
47+
# Test invalid x86_64 instance type (using arm64 instance type)
48+
run "invalid_x86_64_instance" {
49+
command = plan
50+
51+
variables {
52+
instance_type = "t4g.micro"
53+
architecture = "x86_64"
54+
}
55+
56+
expect_failures = [
57+
null_resource.validate_instance_type
58+
]
59+
}
60+
61+
# Test invalid arm64 instance type (using x86_64 instance type)
62+
run "invalid_arm64_instance" {
63+
command = plan
64+
65+
variables {
66+
instance_type = "t3.micro"
67+
architecture = "arm64"
68+
}
69+
70+
expect_failures = [
71+
null_resource.validate_instance_type
72+
]
73+
}
74+
75+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
76+
# It has 'g' in the name, but it's still an x86_64 instance type because the 'g' is the instance family
77+
run "graphics_instance_arm_incompatiblity_edge_case" {
78+
command = plan
79+
80+
variables {
81+
instance_type = "g3s.xlarge"
82+
architecture = "arm64"
83+
}
84+
85+
expect_failures = [
86+
null_resource.validate_instance_type
87+
]
88+
}
89+
90+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
91+
# It has 'g' in the name, but it still is compatible with x86_64 since the 'g' is the instance family
92+
run "graphics_instance_x86_compatibility_edge_case" {
93+
command = plan
94+
95+
variables {
96+
instance_type = "g4dn.xlarge"
97+
architecture = "x86_64"
98+
}
99+
100+
assert {
101+
condition = local.is_instance_compatible
102+
error_message = "Expected instance type g3s.xlarge to be compatible with x86_64 architecture"
103+
}
104+
}

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ variable "instance_type" {
2727
variable "ami" {
2828
default = ""
2929
type = string
30-
description = "The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2 AMI. Pin to a specific AMI if you would like to avoid these updates."
30+
description = "The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2023 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2023 AMI. Pin to a specific AMI if you would like to avoid these updates."
31+
}
32+
33+
variable "architecture" {
34+
description = "The architecture of the AMI (e.g., x86_64, arm64)"
35+
type = string
36+
default = "arm64"
3137
}
3238

3339
variable "user_data" {

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ terraform {
1111
source = "hashicorp/time"
1212
version = ">= 0.7"
1313
}
14+
null = {
15+
source = "hashicorp/null"
16+
version = ">= 3.2"
17+
}
1418
}
1519
}

0 commit comments

Comments
 (0)