-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
133 lines (109 loc) · 3.42 KB
/
main.tf
File metadata and controls
133 lines (109 loc) · 3.42 KB
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
127
128
129
130
131
132
133
variable "aws_region" {
description = "AWS region to deploy resources"
type = string
default = "eu-west-1"
}
variable "folding_passkey" {
description = "Passkey for Folding@home (get it from https://apps.foldingathome.org/getpasskey)"
type = string
sensitive = true
}
provider "aws" {
region = var.aws_region
}
resource "aws_ecs_cluster" "my_cluster" {
name = "my-ecs-cluster"
}
resource "aws_ecs_cluster_capacity_providers" "example" {
cluster_name = aws_ecs_cluster.my_cluster.name
capacity_providers = [aws_ecs_capacity_provider.ecs_asg_capacity_provider.name]
default_capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.ecs_asg_capacity_provider.name
weight = 1
base = 1
}
}
resource "aws_iam_role" "ecs_instance_role" {
name = "my-ecs-instance-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "ecs_instance_role_attachment" {
role = aws_iam_role.ecs_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_instance_profile" "ecs_instance_profile" {
name = "ecsInstanceProfile"
role = aws_iam_role.ecs_instance_role.name
}
data "aws_ami" "ecs_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-ecs-gpu-hvm-*-x86_64-ebs"]
}
}
resource "aws_launch_template" "ecs_launch_template" {
name_prefix = "ecs-launch-template-"
image_id = data.aws_ami.ecs_ami.id # Update this with the latest ECS-optimized AMI ID for your region
instance_type = "g4dn.xlarge"
iam_instance_profile {
name = aws_iam_instance_profile.ecs_instance_profile.name
}
user_data = base64encode(<<-EOF
#!/bin/bash
echo ECS_CLUSTER=${aws_ecs_cluster.my_cluster.name} >> /etc/ecs/ecs.config
echo ECS_ENABLE_GPU_SUPPORT=true >> /etc/ecs/ecs.config
EOF
)
tag_specifications {
resource_type = "instance"
tags = {
Name = "ECS Instance - g4dn.xlarge"
}
}
}
resource "aws_autoscaling_group" "ecs_asg" {
launch_template {
id = aws_launch_template.ecs_launch_template.id
version = "$Latest"
}
min_size = 0
max_size = 10
desired_capacity = 1
vpc_zone_identifier = [for id in aws_subnet.private_subnet.*.id : id]
tag {
key = "Name"
value = "ECS Instance"
propagate_at_launch = true
}
tag {
key = "ecsCluster"
value = aws_ecs_cluster.my_cluster.name
propagate_at_launch = true
}
}
resource "aws_ecs_capacity_provider" "ecs_asg_capacity_provider" {
name = "my-ecs-capacity-provider"
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.ecs_asg.arn
managed_termination_protection = "DISABLED" # 필요에 따라 "ENABLED" 또는 "DISABLED"
managed_scaling {
status = "ENABLED" # 필요에 따라 "ENABLED" 또는 "DISABLED"
target_capacity = 100 # 원하는 타겟 용량 비율
minimum_scaling_step_size = 1 # 최소 스케일링 단계 크기
maximum_scaling_step_size = 100 # 최대 스케일링 단계 크기
}
}
}