Skip to content

Commit

Permalink
Merge pull request #6 from buildo/5-update-to-terraform-0.12
Browse files Browse the repository at this point in the history
#5: Update to terraform 0.12
  • Loading branch information
bennesp authored Oct 13, 2020
2 parents 9aa9c86 + ccdde42 commit 1589fca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 61 deletions.
42 changes: 22 additions & 20 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,35 @@ data "aws_ami" "ami" {
}

resource "aws_instance" "instance" {
ami = "${coalesce(var.ami, data.aws_ami.ami.image_id)}"
instance_type = "${var.instance_type}"
key_name = "${var.ssh_key_name}"
security_groups = ["${aws_security_group.sg.name}"]
ami = coalesce(var.ami, data.aws_ami.ami.image_id)
instance_type = var.instance_type
key_name = var.ssh_key_name
security_groups = [aws_security_group.sg.name]
associate_public_ip_address = true
iam_instance_profile = "${var.iam_instance_profile}"
iam_instance_profile = var.iam_instance_profile

tags {
Name = "${var.project_name}"
tags = {
Name = var.project_name
}

root_block_device {
volume_size = "${var.volume_size}"
volume_size = var.volume_size
}

connection {
host = coalesce(self.public_ip, self.private_ip)
type = "ssh"
user = "ubuntu"
private_key = "${file("${var.ssh_private_key}")}"
private_key = file(var.ssh_private_key)
}

provisioner "file" {
content = "${file("crane.yml")}"
content = file("crane.yml")
destination = "~/crane.yml"
}

provisioner "file" {
content = "${var.init_script}"
content = var.init_script
destination = "~/init.sh"
}

Expand Down Expand Up @@ -86,21 +88,21 @@ resource "aws_cloudwatch_metric_alarm" "disk-full" {
namespace = "System/Linux"
period = "60"
statistic = "Average"
threshold = "${var.disk_utilization_alarm_threshold}"
threshold = var.disk_utilization_alarm_threshold
alarm_description = "This metric monitors disk utilization"
alarm_actions = ["${lookup(var.bellosguardo_sns_topic_arn, var.bellosguardo_target)}"]
ok_actions = ["${lookup(var.bellosguardo_sns_topic_arn, var.bellosguardo_target)}"]
alarm_actions = [var.bellosguardo_sns_topic_arn[var.bellosguardo_target]]
ok_actions = [var.bellosguardo_sns_topic_arn[var.bellosguardo_target]]
treat_missing_data = "breaching"

dimensions {
InstanceId = "${aws_instance.instance.id}"
dimensions = {
InstanceId = aws_instance.instance.id
MountPath = "/"
Filesystem = "overlay"
}
}

variable "bellosguardo_sns_topic_arn" {
type = "map"
type = map(string)

default = {
buildo = "arn:aws:sns:eu-west-1:309416224681:bellosguardo"
Expand All @@ -109,9 +111,9 @@ variable "bellosguardo_sns_topic_arn" {
}

resource "aws_route53_record" "dns" {
zone_id = "${var.zone_id}"
name = "${var.host_name}"
zone_id = var.zone_id
name = var.host_name
type = "A"
ttl = "300"
records = ["${aws_instance.instance.public_ip}"]
records = [aws_instance.instance.public_ip]
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ output "ssh_cmd" {
}

output "public_ip" {
value = "${aws_instance.instance.public_ip}"
value = aws_instance.instance.public_ip
}

47 changes: 23 additions & 24 deletions security_group.tf
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
resource "aws_security_group" "sg" {
name = "${var.project_name}"
name = var.project_name
}

resource "aws_security_group_rule" "ssh" {
type = "ingress"
protocol = "tcp"
security_group_id = "${aws_security_group.sg.id}"
from_port = 22
to_port = 22
cidr_blocks = "${var.in_cidr_blocks}"
type = "ingress"
protocol = "tcp"
security_group_id = aws_security_group.sg.id
from_port = 22
to_port = 22
cidr_blocks = var.in_cidr_blocks
}

resource "aws_security_group_rule" "out_all" {
type = "egress"
protocol = -1
security_group_id = "${aws_security_group.sg.id}"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
type = "egress"
protocol = -1
security_group_id = aws_security_group.sg.id
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "custom_ports" {
count = "${length(var.in_open_ports)}"
type = "ingress"
protocol = "tcp"
security_group_id = "${aws_security_group.sg.id}"
from_port = "${2 == length(split("-", element(var.in_open_ports, count.index))) ?
element(split("-", element(var.in_open_ports, count.index)), 0) :
element(var.in_open_ports, count.index) }"
to_port = "${2 == length(split("-", element(var.in_open_ports, count.index))) ?
element(split("-", element(var.in_open_ports, count.index)), 1) :
element(var.in_open_ports, count.index) }"
cidr_blocks = "${var.in_cidr_blocks}"
count = length(var.in_open_ports)
type = "ingress"
protocol = "tcp"
security_group_id = aws_security_group.sg.id
from_port = 2 == length(split("-", element(var.in_open_ports, count.index))) ? element(split("-", element(var.in_open_ports, count.index)), 0) : element(var.in_open_ports, count.index)

to_port = 2 == length(split("-", element(var.in_open_ports, count.index))) ? element(split("-", element(var.in_open_ports, count.index)), 1) : element(var.in_open_ports, count.index)

cidr_blocks = var.in_cidr_blocks
}

32 changes: 16 additions & 16 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
variable project_name {
variable "project_name" {
description = "Project name, used for namespacing things"
}

variable instance_type {
variable "instance_type" {
default = "t2.micro"
}

variable ami {
variable "ami" {
description = "Custom AMI, if empty will use latest Ubuntu"
default = ""
}

variable volume_size {
variable "volume_size" {
description = "Volume size"
default = 8
}

variable ssh_private_key {
variable "ssh_private_key" {
description = "Used to connect to the instance once created"
}

variable ssh_key_name {
variable "ssh_key_name" {
description = "Name of the key-pair on EC2 (aws-ireland, buildo-aws, ...)"
}

variable zone_id {
variable "zone_id" {
description = "Route53 Zone ID"
}

variable host_name {
variable "host_name" {
description = "DNS host name"
}

variable quay_password {
variable "quay_password" {
description = "Quay password"
}

variable init_script {
description = "bash code executed before `crane lift` is called, example: `\"${file(\"init.sh\")}\"`"
variable "init_script" {
description = "bash code executed before `crane lift` is called, example: `\"$${file(\\\"init.sh\\\")}\"`"
default = ""
}

variable in_open_ports {
variable "in_open_ports" {
default = []
}

variable in_cidr_blocks {
variable "in_cidr_blocks" {
default = ["0.0.0.0/0"]
}

variable disk_utilization_alarm_threshold {
variable "disk_utilization_alarm_threshold" {
description = "disk occupation alarm threshold (% of disk utilization)"
default = "80"
}

variable bellosguardo_target {
variable "bellosguardo_target" {
description = "Possible values are 'buildo', 'omnilab'"
}

variable iam_instance_profile {
variable "iam_instance_profile" {
default = ""
}
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}

0 comments on commit 1589fca

Please sign in to comment.