Skip to content

Commit 34bcb74

Browse files
committed
add terraform
1 parent 09eb014 commit 34bcb74

6 files changed

Lines changed: 148 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ target/
2626
*.log
2727

2828
site
29+
30+
.terraform/
31+
*.tfstate
32+
*.tfplan
33+
plan.out
34+
*.tfvars

docs/terraform.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Terraform
2+
3+
> **Terraform** is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently
4+
5+
Resources
6+
7+
* [Terraform in Action](https://www.manning.com/books/terraform-in-action)
8+
* [Documentation](https://developer.hashicorp.com/terraform)
9+
* [Terraform Registry](https://registry.terraform.io)
10+
11+
Setup
12+
```sh
13+
brew tap hashicorp/tap
14+
brew install hashicorp/tap/terraform
15+
16+
terraform version
17+
```
18+
19+
Terraform's core building blocks:
20+
21+
* `terraform` Terraform's own settings (required version, providers, backend/state config)
22+
* `provider` tells Terraform which cloud/API to talk to and how to authenticate
23+
* `resource` something Terraform creates/updates/deletes e.g. VM, subnet, bucket
24+
* `data` something Terraform reads only (already exists)
25+
* `variable` input values for your config (like function arguments)
26+
* `locals` internal named values/calculations to avoid repeating logic
27+
* `output` values Terraform prints after apply (and exposes to parent modules)
28+
* `module` reusable Terraform package/folder you call from another config
29+
30+
Examples
31+
```sh
32+
# formatting/linting
33+
terraform fmt -check
34+
35+
# initializing a configuration directory downloads and installs the providers defined in the configuration
36+
cd terraform/docker-example
37+
terraform init
38+
39+
# validatiom
40+
terraform validate
41+
# dry run
42+
terraform plan
43+
44+
# with rancher desktop
45+
terraform plan -var="docker_host=unix:///${HOME}/.rd/docker.sock"
46+
# apply with "TF_VAR_<name>" convention
47+
TF_VAR_docker_host="unix:///${HOME}/.rd/docker.sock" terraform apply -auto-approve
48+
49+
# verify state
50+
terraform show
51+
terraform state list
52+
53+
# http://localhost:8080
54+
terraform output -json
55+
56+
# controlled/appoved deployments (binary format)
57+
terraform plan -out=plan.out
58+
terraform show -json plan.out
59+
terraform apply plan.out
60+
```

terraform/docker-example/.terraform.lock.hcl

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/docker-example/locals.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
locals {
2+
image_name = "nginx:latest"
3+
container_name = "my-nginx"
4+
container_port = 80
5+
host_port = 8080
6+
}

terraform/docker-example/main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
terraform {
2+
required_providers {
3+
# https://github.com/kreuzwerker/terraform-provider-docker
4+
# https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs
5+
docker = {
6+
# shorthand for "registry.terraform.io/kreuzwerker/docker"
7+
source = "kreuzwerker/docker"
8+
version = "~> 3.0.1"
9+
}
10+
}
11+
}
12+
13+
provider "docker" {
14+
host = var.docker_host
15+
}
16+
17+
# resource <type> <name> defines components of the infrastructure
18+
# the prefix of the type maps to the name of the provider i.e. "docker_"
19+
# resource <type> and <name> form a unique ID i.e. "docker_image.nginx"
20+
resource "docker_image" "nginx" {
21+
# arguments
22+
name = local.image_name
23+
keep_locally = false
24+
}
25+
26+
resource "docker_container" "my-nginx" {
27+
image = docker_image.nginx.image_id
28+
name = local.container_name
29+
ports {
30+
internal = local.container_port
31+
external = local.host_port
32+
}
33+
}
34+
35+
output "port_message" {
36+
description = "host/container port mapping"
37+
value = "Exposed port: ${local.host_port} -> ${local.container_port}"
38+
}
39+
40+
output "container_info" {
41+
value = {
42+
container_id = docker_container.my-nginx.id
43+
container_name = docker_container.my-nginx.name
44+
image_id = docker_image.nginx.image_id
45+
image_name = docker_image.nginx.image_id
46+
}
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "docker_host" {
2+
description = "Docker daemon socket/endpoint"
3+
type = string
4+
default = "unix:///var/run/docker.sock"
5+
}

0 commit comments

Comments
 (0)