-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-instance.tf
81 lines (66 loc) · 1.84 KB
/
ec2-instance.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
provider "aws" {
region = "us-east-1"
access_key = "<YOUR ACCESS KEY HERE>"
secret_key = "<YOUR SECRET KEY HERE>"
}
resource "aws_key_pair" "key" {
key_name = "private_key"
public_key = file(var.public_key)
}
resource "aws_instance" "testvm" {
ami = "ami-0885b1f6bd170450c"
instance_type = "t2.micro"
vpc_security_group_ids = [ aws_security_group.websg.id ]
tags = {
Name = Test-VM"
}
}
resource "aws_security_group" "testsg" {
name = test-sg01"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP1"
from_port = 8080
to_port = 8080
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"]
}
}
resource "aws_eip" "tf_key" {
vpc = true
instance = aws_instance.testvm.id
}
provisioner "remote-exec" {
inline = ["sudo apt update", "sudo apt install python3 -y", "sudo apt install ansible -y", "echo Done!"]
connection {
type = "ssh"
user = "ubuntu"
host = "${self.ipv4_address}"
private_key = "${file(var.private_key)}"
}
}
provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.ipv4_address},' --private-key ${var.private_key} nginix-install.yml"
}
provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ubuntu -i '${self.ipv4_address},' --private-key ${var.private_key} docker-install.yml"
}