-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.tf
126 lines (106 loc) · 3.6 KB
/
terraform.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.22.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_key_pair" "main" {
key_name = "opentransplant_cluster_key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDDSAuI5zDp6aGlFLloGdjObF29ecadkzWeVPyqEgJzdd715XHgIzklg0VZXbJnT3zmkfWsJWYlfUYc3dQPFVMRKI9+IeORzI6dMU2YxmNyC6dR3vld4tVbu6bcRwvyl+doF/kYWZDPsylMC74n1PbfUMvrQb1nDjoFMEphfTKeR4QI+sZAxahymtuw+NF6NLmM2+AXgKfgq6c1bePyvIswNpIXvn74v4zHiHhBM9Nk5GnF4EA+Vs2MJ86d3qUjrCoVgiwrC0jXFJRUK9pWfwFwB3HwD1HCJc+a7YakX6D6fMwjHfpxGddrdUM+aeVl9SLW8ksvPx0R7IAddx40lj/eVmzYbgwInB0h1gftXtPcgtXNBuTS4nCxWvz78oIeHwxFbOo2w5ZBs/fzDZu8rgjYZQrvUBWzmXUxVfK8vbM7D4xZ5eyQ+rUaHC3+y2cO0hOnVRVBySqVpcj7sIyJMM43bDmtRdG39oEzj6bNyLcSVouhvF5KMPhL+vUyOuFoxhRtEIT4Sbi2nKmlmgCdE1WDJeF9+JhIq/nRVeLBjrcj6/V5FnWLEPz+ozzdvzPRvVxg+/qx3xuAZAW4VIHBYwxsqGekMVFKILBpg3ciD8mT0TN03Lun1c9pOsjloUPKA2oS0JfUrddSAr6BTRENMaveYLAozoC/+KQejH3S/lIkWQ=="
}
variable "ec2_instance_no" {
type = number
default = 1
}
variable "ec2_instance_ami" {
type = string
default = "ami-054e49cb26c2fd312"
}
variable "ec2_instance_type" {
type = string
default = "t4g.small"
}
// supported AZs for t4g instances
resource "random_shuffle" "availability_zone" {
input = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", "us-east-1f"]
result_count = 1
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Owner = "OpenTransplant"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Owner = "OpenTransplant"
}
}
resource "aws_route" "main" {
route_table_id = aws_vpc.main.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.0.0/16"
availability_zone = random_shuffle.availability_zone.result[0]
tags = {
Owner = "OpenTransplant"
}
}
resource "aws_security_group" "main" {
name = "main"
revoke_rules_on_delete = true
vpc_id = aws_vpc.main.id
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Owner = "OpenTransplant"
}
}
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html
resource "aws_placement_group" "main" {
name = "main"
strategy = "spread" // strictly places a small group of instances across distinct underlying hardware to reduce correlated failures
tags = {
Owner = "OpenTransplant"
}
}
resource "aws_instance" "ec2_instances" {
ami = var.ec2_instance_ami
instance_type = var.ec2_instance_type
count = var.ec2_instance_no
placement_group = aws_placement_group.main.id
disable_api_termination = false
subnet_id = aws_subnet.main.id
vpc_security_group_ids = [aws_security_group.main.id]
associate_public_ip_address = true
key_name = aws_key_pair.main.key_name
root_block_device {
volume_size = 8
delete_on_termination = true
}
tags = {
Owner = "OpenTransplant"
}
}
output "ec2_instance_public_ip" {
value = aws_instance.ec2_instances[0].public_ip
}