-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
152 lines (115 loc) · 3.14 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
terraform {
cloud {
organization = "nvaughn"
workspaces {
name = "Nashville"
}
}
# Lock in our version to 3.x
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
###########################
# Virtual Private Cloud
###########################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "Class: ${var.class_name}.vpc"
cidr = var.vpc_cidr_block
azs = [var.subnet_zone]
public_subnets = [var.web_subnet]
tags = {
Terraform = "true"
Environment = "dev"
}
}
###########################
# Security groups
###########################
#######
# HTTP
#######
module "web_server_sg" {
source = "terraform-aws-modules/security-group/aws//modules/http-80"
name = "web-server"
description = "Security group for web-server with HTTP ports open within VPC"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
}
#######
# SSH
#######
module "ssh_server_sg" {
source = "terraform-aws-modules/security-group/aws//modules/ssh"
name = "ssh-server"
description = "Security group for ssh-server with SSH ports open within VPC"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
}
# Get latest x86_64 Amazon Linux2 AMI
data "aws_ami" "latest_amazon_linux2" {
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-kernel-*x86_64-gp2"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
#Import existing SSH Key
resource "aws_key_pair" "this" {
key_name = var.key_name
public_key = file(var.public_key)
count = var.create_key_pair ? 0 : 1
}
###########################
# EC2 Instance
###########################
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 3.0"
ami = data.aws_ami.latest_amazon_linux2.id
name = "${var.class_name}-Instance#${count.index}"
instance_type = var.server_type
key_name = var.key_name
vpc_security_group_ids = [module.ssh_server_sg.security_group_id, module.web_server_sg.security_group_id]
subnet_id = module.vpc.public_subnets[0]
user_data = <<EOF
#!/bin/bash
sudo yum -y update && sudo yum -y install httpd
sudo systemctl start httpd && sudo systemctl enable httpd
sudo echo "<h1>Classroom Host Provisioning Complete</h1>" >/var/www/html/index.html
sudo echo "<h2>Provisioned via TerraForm</h2>" >/var/www/html/index.html
sudo yum install mailx -y
sudo echo "Host Complete" | mail -s "Classroom Provisioning Complete" root@localhost
EOF
count = var.instance_count
tags = {
Terraform = "true"
Environment = "PROD"
}
}
#####################
# S3 Bucket
#####################
module "website_s3_bucket" {
source = "github.com/nvaughn/website_s3_bucket"
bucket_name = "cbt-demo-04-11-2022"
tags = {
Terraform = "true"
Environment = "dev"
}
}