forked from lefnire/diy-cloud-gaming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.tf
70 lines (57 loc) · 1.79 KB
/
ec2.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
data "aws_ami" "nice_dcv" {
owners = ["amazon"]
most_recent = true
filter {
name = "name"
#values = ["ami-062126a20ad482e57"]
values = ["DCV-Windows-*-NVIDIA-gaming-*"]
}
}
resource "tls_private_key" "key" {
algorithm = "RSA"
}
resource "local_file" "private_key" {
filename = "./${var.namespace}.pem"
sensitive_content = tls_private_key.key.private_key_pem
file_permission = "0400"
}
resource "aws_key_pair" "key_pair" {
key_name = var.namespace
public_key = tls_private_key.key.public_key_openssh
tags = local.tags
}
module "ec2" {
count = var.state == "init" ? 1 : 0
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 3.0"
name = var.namespace
ami = var.ami == "default" ? data.aws_ami.nice_dcv.id : var.ami
instance_type = var.instance_type
key_name = aws_key_pair.key_pair.key_name
availability_zone = local.az
subnet_id = element(module.vpc.public_subnets, 0)
vpc_security_group_ids = [module.sg_ec2.security_group_id]
associate_public_ip_address = true
get_password_data = var.ami == "default"
create_spot_instance = var.spot_price == null ? false : true
spot_price = var.spot_price
spot_instance_interruption_behavior = var.spot_price == null ? null : "stop"
root_block_device = [
{
volume_type = var.volume_type
volume_size = var.volume_size
},
]
tags = local.tags
}
resource "aws_eip" "eip" {
instance = var.state == "init" || var.state == "start" ? module.ec2[0].id : null
vpc = true
tags = local.tags
}
#resource "aws_ami_from_instance" "snapshot" {
# count = var.state == "snapshot" || var.state == "stop" ? 1 : 0
# name = var.namespace
# source_instance_id = module.ec2[0].id
# tags = local.tags
#}