Skip to content

Commit

Permalink
Merge pull request #8 from liquidweb/examples
Browse files Browse the repository at this point in the history
adding examples
  • Loading branch information
jakdept committed Oct 11, 2023
2 parents 33f90ad + 029571c commit 646294b
Show file tree
Hide file tree
Showing 35 changed files with 1,588 additions and 28 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
devkey
devkey.pub
**/devkey
**/devkey.pub
terraform.tfvars
*.log
.terraform/
**/.terraform/
vendor/
.lwapi.toml
terraform-provider-liquidweb
terraform.tfstate*
*.tfvars
*.tfvars.json
go/
.cache/
dist/
.ash_history
.terraform.lock.hcl
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ resource "liquidweb_cloud_server" "server" {
## Argument Reference

- `LWAPI_USERNAME`
- `LWAPI_PASSWORD``
- `LWAPI_PASSWORD`

These environment variables are used to set your credentials for the provider.
86 changes: 86 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Terraform Examples

This repository contains terraform examples that are slated for inclusion in the terraform repository elsewhere, but not yet ready.

## What is Terraform?

Terraform is a tool targeted at a Infrastructure as Code approach to managing asset inventory.
It offers a declarative language to create configurations describing infrastructure.
Given the configurations, you can rapidly create, remove, and recreate infrastructure.
Since the configuraitons are plaintext, it allows easy versioning of the infrastructure state with Version Control Software.

### Background Terms

That's a loaded paragraph, some terminology:

- Version Control Software (vcs) - like `git`, a tool that lets you track files over time and compare differences
- Of note, most developers put their source code in VCS. This has many benefits.
- Infrastructure as Code (IaC) - managing servers via config files, often which you can commit to a repository
- Declarative Syntax / Language - describing what an system should be
- Asset Inventory - what assets you have. VPS's are an asset, but SSL certificates, LB's, and Block Storage are also assets.
- configuration files end in `.tf` and determine what is needed
- State - the current way a system is, the actual live snapshot of it, not the way it hsould be
- Lockfile - a file tracking what things terraform currently has

### Terraform Basic Commands

The focus of Terraform is create, recreate, and destroying what is needed.
Terraform can be used alone, and assets recreated as your schema changes.
But most of the time, multiple IaC tools are used to better describe a system.

The major background pieces it will create are:

- the lock file resides at `./.terraform.lock.hcl`
- the state file resides at `./terraform.tfstate`
- a backup state file at `./terraform.tfstate.backup`
- providers typically reside in `./terraform.d`

If you have something deployed, you want to save the

The major commands that terraform provides are:

- `init` - download required providers and set up state and lockfile
- `validate` - make sure configs are valid
- `plan` - show changes to modify state to match configs
- `apply` - run `plan`, then prompt to make those changes
- `destroy` show changes to remove everything, prompt, then remove everything
- `show` - display the current assets
- `taint` - mark an asset currently deployed, on next `apply` will be recreated
- `refresh` - update the state of assets (not supported with LiquidWeb's provider)
- `import` - add existing assets into current state (not supported with LiquidWeb's provider)

### Installing and Examples

The [Hashicorp official instructions for installing terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) are well written.
You are likely best off going there.
Do note, you will likely have a better time if you use the package install for your OS.
In other words, on macOS use `homebrew`, on Windows use `choco`, on Linux use your package manager.

The LiquidWeb provider does not require special installation.
If it is used in your configs, it should be automatically installed with `terraform init`.

[Documentation for that provider is published on the provider page](https://registry.terraform.io/providers/liquidweb/liquidweb/latest/docs).

You will need to provide credentials to a LiquidWeb account in order to use the LiquidWeb provider.
These credentials should be in the following environment variables:

```env
LWAPI_USERNAME="username"
LWAPI_PASSWORD="password"
```

There is also an `acme` SSL provider.
If your domain is hosted with LiquidWeb, you can use this to get an SSL.
[The documentation gives a basic example](https://registry.terraform.io/providers/vancluever/acme/latest/docs/guides/dns-providers-liquidweb).
If you wish to get an SSL with the `acme` provider with a DNS server, you must provide the following credentials:

```env
LIQUID_WEB_USERNAME="username"
LIQUID_WEB_PASSWORD="password"
```

For examples, please look at:

- [Basic server example](./basic-example/)
- [Basic wordpress example](./simple-wordpress/)
- [Wordpress Cluster example](./scalable-wordpress/)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resource "random_id" "block" {
}

resource "liquidweb_cloud_block_storage" "testing_block_volume" {
domain = "terraform-block${random_id.block.dec}.us-midwest-2.hostbaitor.com"
domain = "terraform-block${random_id.block.dec}.us-midwest-2.example.com"
size = 10
}

Expand Down
4 changes: 2 additions & 2 deletions examples/dns.tf → examples/basic-example/dns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ resource "random_id" "dns_rec" {
}

resource "liquidweb_network_dns_record" "testing_a_record" {
name = "dns-rec-${random_id.dns_rec.hex}.us-midwest-2.hostbaitor.com"
name = "dns-rec-${random_id.dns_rec.hex}.us-midwest-2.example.com"
type = "A"
rdata = "127.0.0.1"
zone = "hostbaitor.com"
zone = "example.com"
}

output "domain_a_name" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "liquidweb_network_load_balancer" "testing_lb" {
depends_on = [data.liquidweb_network_zone.testing_zone]
name = "lb.0.terraform-testing.api.hostbaitor.com"
name = "lb.0.terraform-testing.api.example.com"

region = data.liquidweb_network_zone.testing_zone.region_id

Expand Down
9 changes: 9 additions & 0 deletions examples/basic-example/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

terraform {
required_providers {
liquidweb = {
source = "local.providers/liquidweb/liquidweb"
version = "~> 1.6.2"
}
}
}
3 changes: 3 additions & 0 deletions examples/basic-example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Basic Example

This most basic Terraform example in this folder does not target a specific use case, instead it just creates resources.
4 changes: 2 additions & 2 deletions examples/server.tf → examples/basic-example/server.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ resource "random_id" "server" {
resource "liquidweb_cloud_server" "testing_servers" {
count = 2

#config_id = "${data.liquidweb_cloud_server_config.api.id}"
#config_id = "${data.liquidweb_storm_server_config.api.id}"
config_id = 1757
zone = 27
#data.liquidweb_network_zone.api.id
template = "UBUNTU_1804_UNMANAGED" // ubuntu 18.04
domain = "terraform-host${random_id.server[count.index].dec}.us-midwest-2.hostbaitor.com"
domain = "terraform-host${random_id.server[count.index].dec}.us-midwest-2.example.com"
public_ssh_key = file("${path.root}/devkey.pub")
password = "1Aaaaaaaaa"
}
Expand Down
File renamed without changes.
16 changes: 0 additions & 16 deletions examples/provider.tf

This file was deleted.

89 changes: 89 additions & 0 deletions examples/scalable-wordpress/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
terraform {
required_providers {
liquidweb = {
source = "liquidweb/liquidweb"
version = ">= 1.7.0"
}
acme = {
source = "vancluever/acme"
version = "2.17.1"
}
}
}

resource "random_id" "server" {
byte_length = 1
# count = 1
}

resource "random_password" "server" {
length = 20
special = false
}

resource "random_password" "wordpress_dbpass" {
length = 20
special = true
}

resource "random_password" "wordpress_salt" {
length = 32
special = true
}

data "liquidweb_network_zone" "zonec" {
name = "Zone C"
region_name = "US Central"
}

data "template_file" "install-wordpress" {
template = file("${path.module}/templates/install-wordpress.sh")
vars = {
user = var.username
}
}

data "template_file" "wp-config" {
template = file("${path.module}/templates/wp-config.php")
vars = {
dbname = var.wordpress_dbname
dbuser = var.wordpress_dbuser
dbpass = random_password.wordpress_dbpass.result
salt = random_password.wordpress_salt.result
dbhost = liquidweb_cloud_server.dbserver.ip
}
}

data "template_file" "site-conf" {
template = file("${path.module}/templates/nginx.conf")
vars = {
domain = var.site_name
user = var.username
}
}

data "template_file" "php-conf" {
template = file("${path.module}/templates/php-fpm.conf")
vars = {
user = var.username
}
}

resource "liquidweb_network_dns_record" "webserver_dns" {
count = 3
name = liquidweb_cloud_server.webserver[count.index].domain
type = "A"
rdata = liquidweb_cloud_server.webserver[count.index].ip
zone = var.top_domain
}

resource "liquidweb_network_dns_record" "wordpress_record" {
name = var.site_name
type = "A"
rdata = liquidweb_network_load_balancer.loadbalancer.vip
zone = var.top_domain
}

output "domain_a_name" {
value = liquidweb_network_dns_record.wordpress_record.name
}
43 changes: 43 additions & 0 deletions examples/scalable-wordpress/dbserver.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
resource "liquidweb_cloud_server" "dbserver" {
#config_id = "${data.liquidweb_storm_server_config.api.id}"
config_id = 1757
zone = data.liquidweb_network_zone.zonec.network_zone_id
#data.liquidweb_network_zone.api.id
template = "ROCKYLINUX_8_UNMANAGED"
domain = "wordpress-db01-p${random_id.server.dec}.us-midwest-2.${var.top_domain}"
public_ssh_key = file("${path.root}/default.pub")
password = random_password.server.result

connection {
type = "ssh"
user = "root"
agent = true
host = self.ip
}

provisioner "remote-exec" {
inline = [
"yum install -y epel-release",
"yum install -y http://rpms.remirepo.net/enterprise/remi-release-8.rpm",
"yum install -y wget curl mysql mysql-common mysql-server"
]
}

provisioner "remote-exec" {
inline = [
"systemctl start mysqld.service",
"systemctl enable mysqld.service",
"firewall-cmd --zone public --permanent --add-port 3306/tcp",
"firewall-cmd --reload"
]
}
}

output "dbserver_hostnames" {
value = liquidweb_cloud_server.dbserver.ip
}

output "dbserver_ips" {
value = liquidweb_cloud_server.dbserver.ip
}

1 change: 1 addition & 0 deletions examples/scalable-wordpress/default.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDWMKoQLgiAhw3ZZrCd7pZfQ/Wqj+ct6jvV/+MzOJ2BLgoUd9ijnOImf/HWUwuWDol8vQP5sf5Q7mMURut2uFm7bLcZMVnZ7wWSUhS8qptdJY/Hixc3S65xP/DCRjKuE1YWbZXVZYPzLt7JERY3jADo7C0XjOzn5jY509RAU77EIm/idKT0Q0S382OjP8avNNfoevZ2h8A5EiRlcypCTsn8VJk6NFIk+NnqxWsxuCHCbt7+91hPvIhPFxW8f0y4TujIZyUc/8OqKqqBb7e3/eBEBfrCcDRVAho3vD7kLt42xdKy0BtpK58Dds3iToPjfho342qflaAoniXpeAkh0nX2PBJXN6t698bfJ+TVr5xChZ3J7FJw/G7D6KI01hdr43ACTILpDb1gpWHgZMTt7/G2PE3T6hPZeZDYVce5HHkn8B1ptZtRr3vbKZLsPLTm5lEK3GayOWxuUpBRvYaqQNOHw7lOlGTlYJGlCfbfuI2bFQxPBLhaA/KlTiPT5MIGganJyD82gIf4Yw4UckpV57AP7KAP64D9++a5fmpVQ4lBWE7DP+GoXUz8yw9YMygBbniEbkM6dXf6RB8G+8GxHohfZ/lFgWUEyrrL9vrFi5eELTCpoPu5COzXwMg61cXKfsGD8/n3Eh8XSMTFeJQJdRmPQCBs8SormvT6j5JhGUOVyw== cardno:9_021_958
26 changes: 26 additions & 0 deletions examples/scalable-wordpress/loadbalancer.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "liquidweb_network_load_balancer" "loadbalancer" {
# depends_on = [data.liquidweb_network_zone.testing_zone]
name = "wordpress-loadbalancer1-p${random_id.server.dec}${var.top_domain}"

region = data.liquidweb_network_zone.zonec.region_id

nodes = liquidweb_cloud_server.webserver[*].ip

service {
src_port = 80
dest_port = 80
}

service {
src_port = 443
dest_port = 443
}

#session_persistence = false
#ssl_termination = false
strategy = "roundrobin"
}

output "lb_vip" {
value = liquidweb_network_load_balancer.loadbalancer.vip
}
Loading

0 comments on commit 646294b

Please sign in to comment.