-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathec2 reference.tf
More file actions
144 lines (110 loc) · 2.73 KB
/
ec2 reference.tf
File metadata and controls
144 lines (110 loc) · 2.73 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# configured aws provider with proper credentials
provider "aws" {
region = "us-east-1"
profile = "terraform-user"
}
# create default vpc if one does not exit
resource "aws_default_vpc" "default_vpc" {
tags = {
Name = "default vpc"
}
}
# use data source to get all avalablility zones in region
data "aws_availability_zones" "available_zones" {}
# create default subnet if one does not exit
resource "aws_default_subnet" "default_az1" {
availability_zone = data.aws_availability_zones.available_zones.names[0]
tags = {
Name = "default subnet"
}
}
# create security group for the ec2 instance
resource "aws_security_group" "ec2_security_group" {
name = "ec2 security group"
description = "allow access on ports 80 and 22"
vpc_id =
ingress {
description = "http access"
from_port =
to_port =
protocol =
cidr_blocks =
}
ingress {
description = "ssh access"
from_port =
to_port =
protocol =
cidr_blocks =
}
egress {
from_port =
to_port =
protocol =
cidr_blocks =
}
tags = {
Name = "docker server sg"
}
}
# use data source to get a registered amazon linux 2 ami
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
# launch the ec2 instance
resource "aws_instance" "ec2_instance" {
ami =
instance_type =
subnet_id =
vpc_security_group_ids =
key_name =
tags = {
Name = "docker server"
}
}
# an empty resource block
resource "null_resource" "name" {
# ssh into the ec2 instance
connection {
type =
user =
private_key = file()
host =
}
# copy the password file for your docker hub account
# from your computer to the ec2 instance
provisioner "file" {
source =
destination =
}
# copy the dockerfile from your computer to the ec2 instance
provisioner "file" {
source =
destination =
}
# copy the build_docker_image.sh from your computer to the ec2 instance
provisioner "file" {
source =
destination =
}
# set permissions and run the build_docker_image.sh file
provisioner "remote-exec" {
inline = [
]
}
# wait for ec2 to be created
depends_on = []
}
# print the url of the container
output "container_url" {
value = join("", ["http://", aws_instance.ec2_instance.public_dns])
}