-
Notifications
You must be signed in to change notification settings - Fork 80
/
Nilesh-850470.tf
161 lines (124 loc) · 3.09 KB
/
Nilesh-850470.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
152
153
154
155
156
157
158
159
160
161
##provider.tf
provider "aws"{
access_key=$(var.key_cloud.access_key)
private_key=$(var.key_cloud.access_key)
region=$(var.cloud_region)
}
##compute.tf
resource "aws_instance" "web_server" {
ami = "${lookup(var.webserver_amis,$var.aws_region)}"
instance_type = "t2.micro"
count=2
}
provisioner "file" {
source = "bootstrap.sh"
destination = "/tmp/bootstrap.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap.sh",
"/tmp/bootstrap.sh"
]
}
provisioner "file" {
source = "bootstrap.sh"
destination = "/tmp/bootstrap.sh"
}
resource "aws_vpc" "customvpc" {
cidr_block = "${var.vpc_cidr}"
enable_dns_hostnames = true
tags = {
Name = "customvpc"
Terraform = true
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = "${aws_vpc.customvpc.id}"
tags = {
Name = "igw"
Terraform = true
}
}
resource "aws_subnet" "us-east-1a-public" {
vpc_id = "${aws_vpc.customvpc.id}"
availability_zone = "us-east-1a"
cidr_block = "$"
tags {
Name = "Public Subnet"
Terraform = true
}
}
##variable.tf
variable "access_key" {}
variable "private_key" {}
variable "cloud_region"{
default="us-east-1"
}
##terraform.tfvars
webserver_amis {
"us-east-1" = "ami-6182g767"
"us-west-1" = "ami-3436287j"
"ap-north-1" = "ami-634fdy1"
}
##resources.tf
resource "aws_instance" "web_server" {
…
vpc_security_group_ids =
["${aws_security_group.web_server_sec_group.id}"]
}
Your web server resource should now look like the following:
resource "aws_instance" "web_server" {
ami = "${lookup(var.webserver_amis,
var.aws_region)}"
instance_type = "t2.micro"
Managing Secrets with Hashicorp Vault
Copyright 2017 Christopher Parent 29
subnet_id =
"${aws_default_subnet.learntf_default_subnet.id}"
key_name = "${aws_key_pair.deployerkeypair.
key_name}"
vpc_security_group_ids =
["${aws_security_group.web_server_sec_group.id}"]
depends_on = ["aws_s3_bucket.learntf-bins"]
}
##webservers.tf
resource "aws_security_group" "websg" {
name = "vpc_web"
description = ""
vpc_id = "${aws_vpc.customvpc.id}"
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"]
}
tags = {
Name = "WebSG"
Terraform = true
}
}
resource "aws_instance" "web1" {
ami = "${data.aws_ami.ubuntu.id}"
availability_zone = "us-east-1a"
instance_type = "t2.micro"
key_name = "${var.aws_key_name}"
vpc_security_group_ids = ["${aws_security_group.websg.id}"]
subnet_id = "${aws_subnet.us-east-1a-public.id}"
associate_public_ip_address = true
source_dest_check = false
resource "aws_eip" "web1" {
instance = "${aws_instance.web1.id}"
vpc = true
}
$ terraform init
$ terraform plan
$ terraform apply
$yes
$ terraform import aws_instance.web_server i-
957643he7383498