Repository navigation
Expand file tree
/
Copy pathmain.tf
More file actions
83 lines (67 loc) · 1.62 KB
/
Copy pathmain.tf
File metadata and controls
83 lines (67 loc) · 1.62 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
provider "aws" {
region = var.aws_region
}
module "network" {
source = "./modules/networks"
vpc_cidr = "10.0.0.0/16"
public_subnet_cidr = "10.0.1.0/24"
vpc_name = "terraform-vpc"
subnet_name = "public-subnet"
}
resource "aws_security_group" "ssh_tls" {
name = "dev_sg"
description = "dev_security_group"
vpc_id = module.network.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}
resource "aws_instance" "lmachina" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
count = 1
subnet_id = module.network.subnet_id
vpc_security_group_ids = [aws_security_group.ssh_tls.id]
key_name = var.key_name
tags = {
Name = "ubuntu-22-terraform-${count.index + 1}"
}
}
resource "local_file" "inventory" {
filename = "${path.module}/ansible/inventory/hosts.ini"
content = <<EOF
[all:vars]
ansible_connection=ssh
[webservers]
${join("\n",[for instance in aws_instance.lmachina: "${instance.public_ip} ansible_ssh_private_key_file=${var.ssh_key_path}"])}
EOF
}