Extract packer-backed droplet logic to a shared module

This commit is contained in:
Jeremy Dormitzer 2021-01-07 11:28:25 -05:00
parent 44553a3e1d
commit 4cce60af67
7 changed files with 127 additions and 77 deletions

View File

@ -4,43 +4,12 @@ provider "digitalocean" {
spaces_secret_key = var.spaces_secret_key spaces_secret_key = var.spaces_secret_key
} }
data "terraform_remote_state" "do_ssh_keys" { module "packer_droplet" {
backend = "s3" source = "../../../terraform-modules/packer_droplet"
config = {
skip_credentials_validation = true
skip_metadata_api_check = true
access_key = var.spaces_access_id
secret_key = var.spaces_secret_key
region = "us-east-1"
endpoint = "nyc3.digitaloceanspaces.com"
bucket = "jdormit-tf-state"
key = "mgmt/do-ssh-keys.tfstate"
}
}
data "local_file" "packer_manifest" {
filename = "${path.module}/../packer/packer-manifest.json"
}
locals {
packer_manifest = jsondecode(data.local_file.packer_manifest.content)
packer_builds = local.packer_manifest.builds
packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id
packer_image = split(":", local.packer_image_str)[1]
}
resource "digitalocean_droplet" "syncthing" {
name = "syncthing" name = "syncthing"
image = local.packer_image do_token = var.do_token
region = "nyc1" spaces_access_id = var.spaces_access_id
size = "s-1vcpu-1gb" spaces_secret_key = var.spaces_secret_key
backups = true
volume_ids = [digitalocean_volume.syncthing_volume.id]
ssh_keys = [
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id,
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id
]
} }
resource "digitalocean_volume" "syncthing_volume" { resource "digitalocean_volume" "syncthing_volume" {
@ -55,3 +24,8 @@ resource "digitalocean_volume" "syncthing_volume" {
prevent_destroy = true prevent_destroy = true
} }
} }
resource "digitalocean_volume_attachment" "syncthing" {
droplet_id = module.packer_droplet.droplet_id
volume_id = digitalocean_volume.syncthing_volume.id
}

View File

@ -1,9 +1,9 @@
output "ip_address" { output "ip_address" {
value = digitalocean_droplet.syncthing.ipv4_address value = module.packer_droplet.droplet_ip_address
} }
output "urn" { output "urn" {
value = digitalocean_droplet.syncthing.urn value = module.packer_droplet.droplet_urn
} }
output "volume_urn" { output "volume_urn" {

View File

@ -4,40 +4,10 @@ provider "digitalocean" {
spaces_secret_key = var.spaces_secret_key spaces_secret_key = var.spaces_secret_key
} }
data "terraform_remote_state" "do_ssh_keys" { module "packer_droplet" {
backend = "s3" source = "../../../terraform-modules/packer_droplet"
config = {
skip_credentials_validation = true
skip_metadata_api_check = true
access_key = var.spaces_access_id
secret_key = var.spaces_secret_key
region = "us-east-1"
endpoint = "nyc3.digitaloceanspaces.com"
bucket = "jdormit-tf-state"
key = "mgmt/do-ssh-keys.tfstate"
}
}
data "local_file" "packer_manifest" {
filename = "${path.module}/../packer/packer-manifest.json"
}
locals {
packer_manifest = jsondecode(data.local_file.packer_manifest.content)
packer_builds = local.packer_manifest.builds
packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id
packer_image = split(":", local.packer_image_str)[1]
}
resource "digitalocean_droplet" "wallabag" {
name = "wallabag" name = "wallabag"
image = local.packer_image do_token = var.do_token
region = "nyc1" spaces_access_id = var.spaces_access_id
size = "s-1vcpu-1gb" spaces_secret_key = var.spaces_secret_key
backups = true
ssh_keys = [
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id,
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id
]
} }

View File

@ -1,7 +1,7 @@
output "ip_address" { output "ip_address" {
value = digitalocean_droplet.wallabag.ipv4_address value = module.packer_droplet.droplet_ip_address
} }
output "urn" { output "urn" {
value = digitalocean_droplet.wallabag.urn value = module.packer_droplet.droplet_urn
} }

View File

@ -0,0 +1,55 @@
variable "do_token" {
type = string
}
variable "spaces_access_id" {
type = string
}
variable "spaces_secret_key" {
type = string
}
variable "name" {
description = "Droplet name"
type = string
}
variable "packer_manifest" {
description = "Path to the packer manifest JSON file"
type = string
default = "../packer/packer-manifest.json"
}
variable "region" {
description = "Droplet region"
type = string
default = "nyc1"
}
variable "droplet_size" {
description = "Droplet size descriptor"
type = string
default = "s-1vcpu-1gb"
}
variable "backups" {
description = "Whether to enable droplet backups"
type = bool
default = true
}
output "droplet_id" {
description = "Droplet ID"
value = digitalocean_droplet.droplet.id
}
output "droplet_ip_address" {
description = "Droplet IPv4 address"
value = digitalocean_droplet.droplet.ipv4_address
}
output "droplet_urn" {
description = "Droplet URN"
value = digitalocean_droplet.droplet.urn
}

View File

@ -0,0 +1,43 @@
provider "digitalocean" {
token = var.do_token
spaces_access_id = var.spaces_access_id
spaces_secret_key = var.spaces_secret_key
}
data "terraform_remote_state" "do_ssh_keys" {
backend = "s3"
config = {
skip_credentials_validation = true
skip_metadata_api_check = true
access_key = var.spaces_access_id
secret_key = var.spaces_secret_key
region = "us-east-1"
endpoint = "nyc3.digitaloceanspaces.com"
bucket = "jdormit-tf-state"
key = "mgmt/do-ssh-keys.tfstate"
}
}
data "local_file" "packer_manifest" {
filename = "${path.root}/${var.packer_manifest}"
}
locals {
packer_manifest = jsondecode(data.local_file.packer_manifest.content)
packer_builds = local.packer_manifest.builds
packer_image_str = element(local.packer_builds, length(local.packer_builds) - 1).artifact_id
packer_image = split(":", local.packer_image_str)[1]
}
resource "digitalocean_droplet" "droplet" {
name = var.name
image = local.packer_image
region = var.region
size = var.droplet_size
backups = var.backups
ssh_keys = [
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_laptop_ssh_key_id,
data.terraform_remote_state.do_ssh_keys.outputs.jdormit_macbook_lola_ssh_key_id
]
}

View File

@ -0,0 +1,8 @@
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.3.0"
}
}
}