-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
60 lines (56 loc) · 1.55 KB
/
main.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
# Terraform module for EC2
// Retrieve prerequisities
data "aws_vpc" "ec2_vpc" {
filter {
name = "tag:Name"
values = ["${var.vpc_name}"]
}
}
data "aws_subnet" "ec2_subnet" {
filter {
name = "tag:Name"
values = ["${var.subnet_name}"]
}
vpc_id = data.aws_vpc.ec2_vpc.id
}
// Embeded Security Group
resource "aws_security_group" "ec2_sg" {
description = "Security Group for EC2 ${var.ec2_security_group_name}"
name = var.ec2_security_group_name
vpc_id = data.aws_vpc.ec2_vpc.id
tags = merge(
var.global_tags,
tomap({ "Environment" : "${var.Environment}",
"Project" : "${var.Project}" })
)
}
// EC2 Instance
resource "aws_instance" "ec2" {
ami = var.ec2_ami
instance_type = var.ec2_instance_type
iam_instance_profile = var.ec2_iam_instance_profile
user_data = <<-EOF
#!/bin/bash
echo ECS_CLUSTER=${var.ecs_cluster_name} >> /etc/ecs/ecs.config
EOF
network_interface {
network_interface_id = aws_network_interface.ec2_nic.id
device_index = 0
}
tags = merge(
var.global_tags,
tomap({ "Environment" : "${var.Environment}",
"Project" : "${var.Project}",
"Name" : "${var.ec2_instance_name}" })
)
}
// EC2 Network interface
resource "aws_network_interface" "ec2_nic" {
subnet_id = data.aws_subnet.ec2_subnet.id
security_groups = [aws_security_group.ec2_sg.id]
tags = merge(
var.global_tags,
tomap({ "Environment" : "${var.Environment}",
"Project" : "${var.Project}" })
)
}