Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit 11de53a

Browse files
authored
Infer latest Amazon Linux 2 instead of hardcode (#5)
1 parent 4052699 commit 11de53a

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ This is an open source software. Feel free to open issues and pull requests.
102102

103103
| Name | Description | Type | Default | Required |
104104
|------|-------------|:----:|:-----:|:-----:|
105-
| image\_id | AMI of the NAT instance | string | `"ami-04b762b4289fba92b"` | no |
105+
| image\_id | AMI of the NAT instance. Default to the latest Amazon Linux 2 | string | `""` | no |
106106
| instance\_types | Candidates of spot instance type for the NAT instance. This is used in the mixed instances policy | list | `[ "t3.nano", "t3a.nano" ]` | no |
107107
| key\_name | Name of the key pair for the NAT instance. You can set this to assign the key pair to the NAT instance | string | `""` | no |
108108
| name | Name for all the resources as identifier | string | n/a | yes |

main.tf

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,35 @@ resource "aws_route" "this" {
4949
network_interface_id = aws_network_interface.this.id
5050
}
5151

52+
# AMI of the latest Amazon Linux 2
53+
data "aws_ami" "this" {
54+
most_recent = true
55+
owners = ["amazon"]
56+
filter {
57+
name = "architecture"
58+
values = ["x86_64"]
59+
}
60+
filter {
61+
name = "root-device-type"
62+
values = ["ebs"]
63+
}
64+
filter {
65+
name = "name"
66+
values = ["amzn2-ami-hvm-*"]
67+
}
68+
filter {
69+
name = "virtualization-type"
70+
values = ["hvm"]
71+
}
72+
filter {
73+
name = "block-device-mapping.volume-type"
74+
values = ["gp2"]
75+
}
76+
}
77+
5278
resource "aws_launch_template" "this" {
5379
name_prefix = var.name
54-
image_id = var.image_id
80+
image_id = var.image_id != "" ? var.image_id : data.aws_ami.this.id
5581
key_name = var.key_name
5682

5783
iam_instance_profile {

variables.tf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ variable "private_route_table_ids" {
2020
}
2121

2222
variable "image_id" {
23-
description = "AMI of the NAT instance"
24-
# Amazon Linux 2 AMI (HVM), SSD Volume Type
25-
default = "ami-04b762b4289fba92b"
23+
description = "AMI of the NAT instance. Default to the latest Amazon Linux 2"
24+
default = ""
2625
}
2726

2827
variable "instance_types" {

0 commit comments

Comments
 (0)