forked from michaelkemp/byu-cloud-conference-educate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-ec2.tf
51 lines (45 loc) · 1.19 KB
/
linux-ec2.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
############ AMI ############
data "aws_ami" "amazon-linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
############ EC2 INSTANCES ############
resource "aws_instance" "linux" {
for_each = aws_default_subnet.default
ami = data.aws_ami.amazon-linux.id
instance_type = var.ec2_type
tags = {
Name = "Linux"
}
subnet_id = each.value.id
associate_public_ip_address = true
key_name = aws_key_pair.generated_key.key_name
vpc_security_group_ids = [aws_security_group.SSH-MYSQL.id]
user_data = <<-EOF
#!/bin/bash
yum update -y && yum upgrade -y
EOF
}
############ OUTPUT CONNECTION INFO ############
locals {
ec2logins = [
for instance in aws_instance.linux:
"ssh -i ${aws_key_pair.generated_key.key_name}.pem ec2-user@${instance.public_ip}"
]
}
output "ec2-info" {
value = <<-EOF
# Change key security to log into Instances
chmod 400 ${aws_key_pair.generated_key.key_name}.pem
# EC2 Login Strings
${join("\n",local.ec2logins)}
EOF
}