forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
83 lines (66 loc) · 2.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY AN EC2 INSTANCE THAT RUNS A SIMPLE "HELLO, WORLD" WEB SERVER
# See test/terraform_http_example.go for how to write automated tests for this code.
# ---------------------------------------------------------------------------------------------------------------------
provider "aws" {
region = "${var.aws_region}"
}
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY THE EC2 INSTANCE
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_instance" "example" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
user_data = "${data.template_file.user_data.rendered}"
vpc_security_group_ids = ["${aws_security_group.example.id}"]
tags {
Name = "${var.instance_name}"
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE A SECURITY GROUP TO CONTROL WHAT REQUESTS CAN GO IN AND OUT OF THE EC2 INSTANCE
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_security_group" "example" {
name = "${var.instance_name}"
ingress {
from_port = "${var.instance_port}"
to_port = "${var.instance_port}"
protocol = "tcp"
# To keep this example simple, we allow incoming HTTP requests from any IP. In real-world usage, you may want to
# lock this down to just the IPs of trusted servers (e.g., of a load balancer).
cidr_blocks = ["0.0.0.0/0"]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE THE USER DATA SCRIPT THAT WILL RUN DURING BOOT ON THE EC2 INSTANCE
# ---------------------------------------------------------------------------------------------------------------------
data "template_file" "user_data" {
template = "${file("${path.module}/user-data/user-data.sh")}"
vars {
instance_text = "${var.instance_text}"
instance_port = "${var.instance_port}"
}
}
# ---------------------------------------------------------------------------------------------------------------------
# LOOK UP THE LATEST UBUNTU AMI
# ---------------------------------------------------------------------------------------------------------------------
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "image-type"
values = ["machine"]
}
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
}