-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.tf
72 lines (65 loc) · 2.55 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
69
70
71
72
################
# ec2/ec2.tf #
################
#--------------------------------------------------------------------------
# resource for creating ec2 instance
#--------------------------------------------------------------------------
resource "aws_instance" "ec2" {
ami = var.ami_id
instance_type = var.instance_type
vpc_security_group_ids = var.security_group_ids
subnet_id = var.subnet
key_name = length(var.key_name) > 0 ? var.key_name : var.name
user_data = var.user_data
disable_api_termination = var.disable_api_termination
source_dest_check = var.source_dest_check
ebs_optimized = var.ebs_optimized
volume_tags = var.enable_volume_tags ? { "Name" = var.name } : null
ipv6_address_count = var.enable_ipv6 ? var.ipv6_address_count : 0
dynamic "root_block_device" {
for_each = var.root_block_device
content {
volume_size = lookup(root_block_device.value, "volume_size", 8)
volume_type = lookup(root_block_device.value, "volume_type", "standard")
delete_on_termination = lookup(root_block_device.value, "delete_on_termination", true)
encrypted = lookup(root_block_device.value, "encrypted", true)
tags = try(root_block_device.value.tags, null)
}
}
dynamic "ebs_block_device" {
for_each = var.ebs_block_device
content {
delete_on_termination = try(ebs_block_device.value.delete_on_termination, null)
device_name = ebs_block_device.value.device_name
encrypted = try(ebs_block_device.value.encrypted, true)
iops = try(ebs_block_device.value.iops, null)
volume_size = try(ebs_block_device.value.volume_size, null)
volume_type = try(ebs_block_device.value.volume_type, null)
throughput = try(ebs_block_device.value.throughput, null)
tags = try(ebs_block_device.value.tags, null)
}
}
lifecycle {
ignore_changes = [ami]
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
tags = merge(
{
Name = var.name
}, var.tags
)
}
#--------------------------------------------------------------------------
# resource for allocation elastic ip to ec2 instnace
#--------------------------------------------------------------------------
resource "aws_eip" "eip" {
count = var.eip ? 1 : 0
instance = aws_instance.ec2.id
domain = "vpc"
tags = {
Name = var.name
}
}