Skip to content

Commit

Permalink
Initial commit - Application servers
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-norwood committed Apr 16, 2020
0 parents commit 4870dbe
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
#
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
terraform
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Learn Terraform Cloud Run Triggers

Learn what Terraform Cloud Run Triggers are and when to use them.

Follow along with the [Learn guide](https://learn.hashicorp.com/terraform/cloud/run-triggers) at HashiCorp Learn.
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<main>
<p>Hello, world!</p>
</main>
</body>
</html>
74 changes: 74 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
data "terraform_remote_state" "vpc" {
backend = "remote"

config = {
organization = var.tfc_org_name
workspaces = {
name = var.tfc_vpc_workspace_name
}
}
}

provider "aws" {
version = "~> 2.7"
region = data.terraform_remote_state.vpc.outputs.aws_region
}

data "aws_ami" "amazon_linux" {
most_recent = true

filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}

owners = ["amazon"]
}

resource "aws_instance" "app" {
count = var.instances_per_subnet * length(data.terraform_remote_state.vpc.outputs.private_subnet_ids)

ami = aws_ami.amazon_linux.id
instance_type = var.instance_type

subnet_id = data.terraform_remote_state.vpc.outputs.private_subnet_ids[count.index % length(data.terraform_remote_state.vpc.outputs.private_subnet_ids)]

user_data = <<EOF
sudo yum update -y
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
echo "<html><body><div>Hello, world!</div></body></html>" > /var/www/html/index.html
EOF

tags = {
Project = data.terraform_remote_state.vpc.outputs.project_tag
}
}

resource "aws_lb_target_group_attachment" "http" {
count = length(aws_instance.app)

target_group_arn = data.terraform_remote_state.vpc.outputs.lb_target_group_http_arn
target_id = aws_instance.app[count.index].id
port = 80

tags = {
Project = data.terraform_remote_state.vpc.outputs.project_tag
}
}

## HTTPS support requires an SSL certificate, which is out of scope for this
## example.
#
# resource "aws_lb_target_group_attachment" "https" {
# count = length(aws_instance.app)
#
# target_group_arn = data.terraform_remote_state.vpc.outputs.lb_target_group_https_arn
# target_id = aws_instance.app[count.index].id
# port = 443
#
# tags = {
# Project = data.terraform_remote_state.vpc.outputs.project_tag
# }
# }
11 changes: 11 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output web_instance_count {
value = len(aws_instance.app)
}

output public_dns_name {
value = data.terraform_remote_state.vpc.outputs.public_dns_name
}

output aws_region {
value = data.terraform_remote_state.vpc.outputs.aws_region
}
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable tfc_org_name {
description = "Name of the Terraform Cloud Organization"
type = string
default = "RunTriggersExampleOrg"
}

variable tfc_vpc_workspace_name {
description = "Name of the Workspace"
type = string
default = "LearnTerraformRunTriggersVPC"
}

variable instances_per_subnet {
description = "Number of EC2 instances in each private subnet"
type = number
default = 2
}

variable instance_type {
description = "Type of EC2 instance to use"
type = string
default = "t2.micro"
}

0 comments on commit 4870dbe

Please sign in to comment.