forked from azeezsalu/terraform-tutorial-reference-files
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2-reference.tf
101 lines (78 loc) · 1.83 KB
/
ec2-reference.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
# configured aws provider with proper credentials
provider "aws" {
region =
profile =
}
# create default vpc if one does not exit
resource "aws_default_vpc" "default_vpc" {
tags = {
Name =
}
}
# 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 =
tags = {
Name =
}
}
# 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 =
}
}
# 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 and install website
resource "aws_instance" "ec2_instance" {
ami =
instance_type =
subnet_id =
vpc_security_group_ids =
key_name =
user_data =
tags = {
Name =
}
}
# print the ec2's public ipv4 address
output "public_ipv4_address" {
value =
}