Last active
November 27, 2018 00:51
-
-
Save m4ldonado/b3e08e51f62d6e582957988ec70dac74 to your computer and use it in GitHub Desktop.
terraform main.tf example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main.tf file based on the exercises in the up and running book. This file is for me to grep through | |
# In reality this would be split up into different files! | |
data "aws_availability_zones" "available" {} | |
variable "server_port" { | |
description = "The port the server will use for HTTP requests" | |
#If you delete the default line it'll prompt on creation | |
# either way you can run with terraform plan -var server_port="8080" | |
# for passwords and such - you can ommit the default and get terraform to use it by | |
# setting TF_VAR_variable_name | |
default = 8080 | |
} | |
variable "map_example" { | |
description = "An example of a map in terraform" | |
type = "map" | |
default { | |
key1 = "foo" | |
key2 = "bar" | |
key3 = "baz" | |
} | |
} | |
provider "aws" { | |
region = "us-east-1" | |
} | |
/* | |
resource "aws_instance" "example" { | |
ami = "ami-e24b7d9d" | |
instance_type = "t2.nano" | |
vpc_security_group_ids = ["${aws_security_group.instance.id}"] | |
# You could have this as a file and read it in like this: | |
# user_data = "${file("user-data.sh")}" | |
# however the variables wouldn't work as below | |
# check out data "template_file" "user_data" {} later on in this file | |
# to see how to do templates | |
user_data = <<-EOF | |
#!/bin/bash | |
echo "Hello, World" > index.html | |
nohup busybox httpd -f -p "${var.server_port}" & | |
EOF | |
tags { | |
Name = "terraform.example" | |
} | |
} | |
output "public_ip" { | |
value = "${aws_instance.example.public_ip}" | |
} | |
*/ | |
resource "aws_security_group" "instance" { | |
name = "terraform-example-instance" | |
ingress = { | |
from_port = "${var.server_port}" | |
to_port = "${var.server_port}" | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
} | |
data "template_file" "user_data" { | |
# use like: user_data = "${data.template_file.user_data.rendered}" | |
template = "${file("user-data.sh")}" | |
vars { | |
server_port = "${var.server_port}" | |
db_address = "${aws_db_instance.example.address}" | |
db_port = "${aws_db_instance.example.port}" | |
} | |
} | |
/* user-data.sh would look like this: | |
#! /bin/bash | |
cat > index.html <<EOF | |
<h1>Hello, World</h1> | |
<p>DB address: ${db_address}</p> | |
<p>DB port: ${db_port}</p> | |
<p>Hostname: $HOSTNAME</p> | |
EOF | |
nohup busybox httpd -f -p "${server_port}" & | |
*/ | |
resource "aws_launch_configuration" "example" { | |
image_id = "ami-e24b7d9d" | |
instance_type = "t2.nano" | |
security_groups = ["${aws_security_group.instance.id}"] | |
user_data = "${data.template_file.user_data.rendered}" | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
#let's check on how the template_file is rendered | |
output "data_file_rendered" { | |
value = "${data.template_file.user_data.rendered}" | |
} | |
resource "aws_autoscaling_group" "example" { | |
launch_configuration = "${aws_launch_configuration.example.id}" | |
availability_zones = ["${data.aws_availability_zones.available.names}"] | |
load_balancers = ["${aws_elb.example.name}"] | |
health_check_type = "ELB" | |
min_size = 2 | |
max_size = 10 | |
tag { | |
key = "Name" | |
value = "terraform-asg-example" | |
propagate_at_launch = true | |
} | |
} | |
resource "aws_elb" "example" { | |
name = "terraform-asg-example" | |
availability_zones = ["${data.aws_availability_zones.available.names}"] | |
security_groups = ["${aws_security_group.elb.id}"] | |
listener { | |
lb_port = 80 | |
lb_protocol = "http" | |
instance_port = "${var.server_port}" | |
instance_protocol = "http" | |
} | |
health_check { | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
timeout = 3 | |
interval = 30 | |
target = "HTTP:${var.server_port}/" | |
} | |
} | |
resource "aws_security_group" "elb" { | |
name = "terraform-example-elb" | |
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"] | |
} | |
} | |
output "elb_dns_name" { | |
value = "${aws_elb.example.dns_name}" | |
} | |
##mysql config | |
variable "db_password" { | |
description = "The password for the database" | |
} | |
/* | |
provider "aws" { | |
region = "us-east=1" | |
} | |
*/ | |
resource "aws_db_instance" "example" { | |
engine = "mysql" | |
allocated_storage = 10 | |
instance_class = "db.t2.micro" | |
name = "example_database" | |
username = "admin" | |
password = "${var.db_password}" | |
} | |
output "database address" { | |
value = "${aws_db_instance.example.address}" | |
} | |
output "database port" { | |
value = "${aws_db_instance.example.port}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment