This repository has been archived by the owner on Feb 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.tf
122 lines (103 loc) · 3.15 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
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
variable "aws_region" { default = "eu-west-2" } # London
variable "username" { default = "databoxuser"}
variable "instance_type" {default = "t2.micro" }
variable "volume_size" {default = "40" }
variable "snapshot_id" {
default = ""
description = "Specify a snapshot_id to be used when creating the EBS Volume. Note that this snapshot must be in the same region as the instance, and must be the same size or smaller than the volume as specified in volume_size."
}
variable "public_key_path" {
description = "Enter the path to the SSH Public Key to add to AWS."
default = "~/.ssh/id_rsa.pub"
}
variable "ami_id" {
default = "" # Note this is empty.
description = "Use this specific AMI ID for our EC2 instance. Default is Ubuntu 16.04 LTS in the current region"
}
provider "aws" {
region = "${var.aws_region}"
profile = "gds-data"
}
resource "aws_security_group" "allow_ssh_from_gds" {
name_prefix = "DataBox-SecurityGroup-SSH"
description = "Allow inbound SSH traffic from GDS IPs"
ingress = {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"213.86.153.212/32", "213.86.153.213/32", "213.86.153.214/32",
"213.86.153.235/32", "213.86.153.236/32", "213.86.153.237/32",
"85.133.67.244/32"]
}
}
resource "aws_security_group" "allow_all_outbound" {
name_prefix = "DataBox-SecurityGroup-outbound"
description = "Allow all outbound traffic"
egress = {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "box" {
ami = "${var.ami_id != "" ? var.ami_id : data.aws_ami.ubuntu.id}"
availability_zone = "${var.aws_region}a"
instance_type = "${var.instance_type}"
security_groups = [
"${aws_security_group.allow_ssh_from_gds.name}",
"${aws_security_group.allow_all_outbound.name}"
]
key_name = "${aws_key_pair.auth.key_name}"
provisioner "remote-exec" {
inline = [
"sudo locale-gen en_GB.UTF-8",
"sudo apt-get -y update",
"sudo apt-get -y install python-dev"
]
connection {
type = "ssh"
user = "ubuntu"
agent = true
}
}
tags {
Name = "DataBoxEC2"
}
}
resource "aws_ebs_volume" "volume" {
availability_zone = "${var.aws_region}a"
size = "${var.volume_size}"
snapshot_id = "${var.snapshot_id}"
tags {
Name = "DataBoxVolume"
}
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/xvdh"
volume_id = "${aws_ebs_volume.volume.id}"
instance_id = "${aws_instance.box.id}"
}
resource "aws_key_pair" "auth" {
key_name = "databox-key-${var.username}"
public_key = "${file(var.public_key_path)}"
}
output "ec2_ip" {
value = "${aws_instance.box.public_ip}"
}
output "data_volume_id" {
value = "${aws_ebs_volume.volume.id}"
}