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

Commit f115c5f

Browse files
committed
Initial commit
0 parents  commit f115c5f

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.terraform/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# terraform-aws-nat-instance
2+
3+
This is a Terraform module which provisions a NAT instance using an auto scaling group and spot request.
4+
5+

main.tf

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
resource "aws_security_group" "this" {
2+
name_prefix = var.name
3+
vpc_id = var.vpc_id
4+
}
5+
6+
resource "aws_security_group_rule" "this_egress" {
7+
security_group_id = aws_security_group.this.id
8+
type = "egress"
9+
cidr_blocks = ["0.0.0.0/0"]
10+
from_port = 0
11+
to_port = 65535
12+
protocol = "tcp"
13+
}
14+
15+
resource "aws_security_group_rule" "this_ingress" {
16+
security_group_id = aws_security_group.this.id
17+
type = "ingress"
18+
cidr_blocks = var.private_subnets_cidr_blocks
19+
from_port = 0
20+
to_port = 65535
21+
protocol = "tcp"
22+
}
23+
24+
resource "aws_launch_template" "this" {
25+
name_prefix = var.name
26+
image_id = var.image_id
27+
iam_instance_profile {
28+
arn = aws_iam_instance_profile.this.arn
29+
}
30+
network_interfaces {
31+
associate_public_ip_address = true
32+
security_groups = [aws_security_group.this.id]
33+
}
34+
}
35+
36+
resource "aws_autoscaling_group" "this" {
37+
name_prefix = var.name
38+
desired_capacity = 1
39+
min_size = 1
40+
max_size = 1
41+
vpc_zone_identifier = var.public_subnets
42+
43+
mixed_instances_policy {
44+
instances_distribution {
45+
on_demand_percentage_above_base_capacity = 0
46+
}
47+
launch_template {
48+
launch_template_specification {
49+
launch_template_id = aws_launch_template.this.id
50+
version = "$Latest"
51+
}
52+
dynamic "override" {
53+
for_each = var.instance_types
54+
content {
55+
instance_type = override.value
56+
}
57+
}
58+
}
59+
}
60+
61+
lifecycle {
62+
create_before_destroy = true
63+
}
64+
}
65+
66+
resource "aws_iam_instance_profile" "this" {
67+
name_prefix = var.name
68+
role = aws_iam_role.this.name
69+
}
70+
71+
resource "aws_iam_role" "this" {
72+
name_prefix = var.name
73+
assume_role_policy = <<EOF
74+
{
75+
"Version": "2012-10-17",
76+
"Statement": [
77+
{
78+
"Effect": "Allow",
79+
"Principal": {
80+
"Service": "ec2.amazonaws.com"
81+
},
82+
"Action": "sts:AssumeRole"
83+
}
84+
]
85+
}
86+
EOF
87+
}
88+
89+
resource "aws_iam_role_policy_attachment" "this_ssm" {
90+
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
91+
role = aws_iam_role.this.name
92+
}

variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "name" {
2+
description = "Name of this NAT instance"
3+
}
4+
5+
variable "vpc_id" {
6+
description = "ID of the VPC"
7+
}
8+
9+
variable "public_subnets" {
10+
description = "List of ID of the public subnets"
11+
}
12+
13+
variable "private_subnets_cidr_blocks" {
14+
description = "List of CIDR blocks of the private subnets"
15+
}
16+
17+
variable "image_id" {
18+
description = "AMI of the NAT instance"
19+
# amzn-ami-vpc-nat-hvm-2018.03.0.20181116-x86_64-ebs
20+
default = "ami-0b840e8a1ce4cdf15"
21+
}
22+
23+
variable "instance_types" {
24+
description = "Candidates of instance type of the NAT instance"
25+
default = ["t3.nano", "t3a.nano"]
26+
}
27+
28+
variable "volume_size" {
29+
default = "8"
30+
}
31+
32+
variable "volume_type" {
33+
default = "gp2"
34+
}

0 commit comments

Comments
 (0)