├── provisioners_example_01 ├── variables.tf ├── output.tf ├── README.md └── main.tf ├── provisioners_example_02 ├── variables.tf ├── output.tf ├── README.md └── main.tf ├── provisioners_example_03 ├── variables.tf ├── output.tf ├── README.md └── main.tf ├── plans_example_03 └── main.tf ├── plans_example_04 └── main.tf ├── providers_example_01 └── main.tf ├── project_layout_example_01 ├── main.tf ├── other │ └── bucket.tf └── s3.tf ├── outputs_example_01 └── main.tf ├── templatefile_example_01 ├── example.tpl └── main.tf ├── variables_example_03 ├── terraform.tfvars └── main.tf ├── templatefile_example_02 ├── backends.tpl └── main.tf ├── plans_example_01 ├── plan ├── myplan └── main.tf ├── .gitignore ├── variables_example_04 ├── terraform.tfvars └── main.tf ├── workspaces_example_01 └── main.tf ├── first_terraform_project └── main.tf ├── modules_example_03 ├── cross-talk │ ├── variables.tf │ └── main.tf ├── cross-talk-3-way │ ├── variables.tf │ └── main.tf └── main.tf ├── state_example_03 ├── main.tf └── state.tf ├── modules_example_02 ├── sqs-with-backoff │ ├── output.tf │ ├── variables.tf │ └── main.tf └── main.tf ├── providers_example_02 └── main.tf ├── variables_example_08 └── main.tf ├── file_example_01 ├── main.tf └── policy.iam ├── state_example_02a └── main.tf ├── plans_example_02 └── main.tf ├── advanced_resources_example_04 └── main.tf ├── state_example_02b └── main.tf ├── state_example_01 └── main.tf ├── variables_example_11 └── main.tf ├── variables_example_01 └── main.tf ├── advanced_resources_example_02 └── main.tf ├── local_example_02 └── main.tf ├── variables_example_06 └── main.tf ├── variables_example_09 └── main.tf ├── resources_example_01 └── main.tf ├── local_example_01 └── main.tf ├── modules_example_01 ├── sqs-with-backoff │ ├── variables.tf │ ├── output.tf │ └── main.tf └── main.tf ├── providers_example_03 └── main.tf ├── variables_example_02 └── main.tf ├── advanced_resources_example_03 └── main.tf ├── variables_example_05 └── main.tf ├── modules_example_04 └── main.tf ├── modules_example_05 └── main.tf ├── variables_example_07 └── main.tf ├── advanced_resources_example_01 └── main.tf ├── outputs_example_02 └── main.tf ├── outputs_example_03 └── main.tf ├── data_source_example_01 └── main.tf ├── resources_example_02 └── main.tf ├── variables_example_10 └── main.tf ├── resources_example_03 └── main.tf ├── LICENSE └── README.md /provisioners_example_01/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_ip" {} -------------------------------------------------------------------------------- /provisioners_example_02/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_ip" {} -------------------------------------------------------------------------------- /provisioners_example_03/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_ip" {} -------------------------------------------------------------------------------- /plans_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | -------------------------------------------------------------------------------- /plans_example_04/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | -------------------------------------------------------------------------------- /providers_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } -------------------------------------------------------------------------------- /project_layout_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | -------------------------------------------------------------------------------- /outputs_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | output "message" { 3 | value = "Hello World" 4 | } 5 | 6 | 7 | -------------------------------------------------------------------------------- /templatefile_example_01/example.tpl: -------------------------------------------------------------------------------- 1 | 2 | hello there ${name} 3 | there are ${number} things to say -------------------------------------------------------------------------------- /variables_example_03/terraform.tfvars: -------------------------------------------------------------------------------- 1 | bucket_name = "kevholditch" 2 | bucket_suffix = "from_file" -------------------------------------------------------------------------------- /templatefile_example_02/backends.tpl: -------------------------------------------------------------------------------- 1 | %{ for addr in ip_addrs ~} 2 | backend ${addr}:${port} 3 | %{ endfor ~} -------------------------------------------------------------------------------- /provisioners_example_01/output.tf: -------------------------------------------------------------------------------- 1 | output "command" { 2 | value = "curl http://${aws_instance.nginx.public_ip}" 3 | } -------------------------------------------------------------------------------- /provisioners_example_02/output.tf: -------------------------------------------------------------------------------- 1 | output "command" { 2 | value = "curl http://${aws_instance.nginx.public_ip}" 3 | } -------------------------------------------------------------------------------- /provisioners_example_03/output.tf: -------------------------------------------------------------------------------- 1 | output "command" { 2 | value = "curl http://${aws_instance.nginx.public_ip}" 3 | } -------------------------------------------------------------------------------- /plans_example_01/plan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevholditch/terraform-beginner-to-master-examples/HEAD/plans_example_01/plan -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/ 3 | .terraform/ 4 | *.tfstate 5 | *.backup 6 | nginx_key 7 | nginx_key.pub 8 | .terraform.lock.hcl -------------------------------------------------------------------------------- /plans_example_01/myplan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevholditch/terraform-beginner-to-master-examples/HEAD/plans_example_01/myplan -------------------------------------------------------------------------------- /project_layout_example_01/other/bucket.tf: -------------------------------------------------------------------------------- 1 | 2 | resource "aws_s3_bucket" "other" { 3 | bucket = "kevholditch-other-bucket" 4 | } 5 | -------------------------------------------------------------------------------- /project_layout_example_01/s3.tf: -------------------------------------------------------------------------------- 1 | 2 | resource "aws_s3_bucket" "first_bucket" { 3 | bucket = "kevholditch-first-bucket" 4 | } 5 | -------------------------------------------------------------------------------- /plans_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_sqs_queue" "test_queue" { 7 | name = "test_queue" 8 | } 9 | -------------------------------------------------------------------------------- /variables_example_04/terraform.tfvars: -------------------------------------------------------------------------------- 1 | instance_map = { 2 | dev = "t3.small" 3 | test = "t3.medium" 4 | prod = "t3.large" 5 | } 6 | 7 | environment_type = "dev" -------------------------------------------------------------------------------- /templatefile_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | output "rendered_template" { 3 | value = templatefile("./backends.tpl", { port = 8080, ip_addrs = ["10.0.0.1", "10.0.0.2"] }) 4 | } -------------------------------------------------------------------------------- /workspaces_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | resource "aws_sqs_queue" "queue" { 8 | name = "${terraform.workspace}-queue" 9 | } -------------------------------------------------------------------------------- /first_terraform_project/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_s3_bucket" "first_bucket" { 7 | bucket = "kevholditch-first-bucket" 8 | } -------------------------------------------------------------------------------- /modules_example_03/cross-talk/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | variable security_group_1 {} 3 | variable security_group_2 {} 4 | variable port { 5 | type = number 6 | } 7 | variable "protocol" {} 8 | -------------------------------------------------------------------------------- /state_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_sqs_queue" "my_queue" { 7 | name = "my_queue" 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /state_example_03/state.tf: -------------------------------------------------------------------------------- 1 | 2 | terraform { 3 | backend "s3" { 4 | bucket = "kevholditch-terraform-state" 5 | key = "myproject.state" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /variables_example_04/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "instance_map" {} 3 | variable "environment_type" {} 4 | 5 | output "selected_instance" { 6 | value = var.instance_map[var.environment_type] 7 | } -------------------------------------------------------------------------------- /modules_example_02/sqs-with-backoff/output.tf: -------------------------------------------------------------------------------- 1 | 2 | output "queue" { 3 | value = aws_sqs_queue.sqs 4 | } 5 | 6 | output "dead_letter_queue" { 7 | value = aws_sqs_queue.sqs_dead_letter 8 | } 9 | -------------------------------------------------------------------------------- /templatefile_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | locals { 3 | rendered = templatefile("./example.tpl", { name = "kevin", number = 7}) 4 | } 5 | 6 | output "rendered_template" { 7 | value = local.rendered 8 | } -------------------------------------------------------------------------------- /providers_example_02/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "eu-west-1" 3 | } 4 | 5 | terraform { 6 | required_providers { 7 | aws = { 8 | version = "~> 3.46" 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /variables_example_08/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_tup" { 3 | type = tuple([number, string, bool]) 4 | default = [4, "hello", false] 5 | } 6 | 7 | output "tup" { 8 | value = var.my_tup 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /file_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_iam_policy" "my_bucket_policy" { 7 | name = "list-buckets-policy" 8 | 9 | policy = file("./policy.iam") 10 | } -------------------------------------------------------------------------------- /modules_example_03/cross-talk-3-way/variables.tf: -------------------------------------------------------------------------------- 1 | 2 | variable security_group_1 {} 3 | variable security_group_2 {} 4 | variable security_group_3 {} 5 | variable port { 6 | type = number 7 | } 8 | variable "protocol" {} 9 | -------------------------------------------------------------------------------- /state_example_02a/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "my_vpc" { 7 | cidr_block = "10.1.0.0/16" 8 | 9 | tags = { 10 | Name = "vpc" 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /plans_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_sqs_queue" "test_queue" { 7 | name = "test_queue" 8 | visibility_timeout_seconds = 45 9 | } 10 | -------------------------------------------------------------------------------- /advanced_resources_example_04/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "eu-west-1" 3 | } 4 | 5 | resource "aws_sqs_queue" "queue" { 6 | name = "queue" 7 | 8 | lifecycle { 9 | prevent_destroy = true 10 | } 11 | } -------------------------------------------------------------------------------- /state_example_02b/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "main" { 7 | cidr_block = "10.1.0.0/16" 8 | 9 | tags = { 10 | Name = "vpc" 11 | } 12 | } 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /state_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "example" { 7 | cidr_block = "10.0.0.0/16" 8 | 9 | tags = { 10 | Name = "example" 11 | } 12 | 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /variables_example_11/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "any_example" { 3 | type = any 4 | default = { 5 | field1 = "foo" 6 | field2 = "bar" 7 | } 8 | } 9 | 10 | output "any_example" { 11 | value = var.any_example 12 | } 13 | 14 | -------------------------------------------------------------------------------- /file_example_01/policy.iam: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Action": [ 6 | "s3:ListBucket" 7 | ], 8 | "Effect": "Allow", 9 | "Resource": "*" 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /variables_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | variable "bucket_name" { 8 | description = "the name of the bucket you wish to create" 9 | } 10 | 11 | resource "aws_s3_bucket" "bucket" { 12 | bucket = "${var.bucket_name}" 13 | } -------------------------------------------------------------------------------- /advanced_resources_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | resource "aws_sqs_queue" "queues" { 8 | count = 4 9 | name = "queue-${count.index}" 10 | } 11 | 12 | output "queue-0-name" { 13 | value = aws_sqs_queue.queues[0].name 14 | } 15 | -------------------------------------------------------------------------------- /local_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "my_vpc" { 7 | cidr_block = "10.0.0.0/16" 8 | } 9 | 10 | locals { 11 | vpc_arn = aws_vpc.my_vpc.arn 12 | } 13 | 14 | output "vpc_arn" { 15 | value = local.vpc_arn 16 | } 17 | 18 | -------------------------------------------------------------------------------- /variables_example_06/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "a" { 3 | type = list(string) 4 | default = ["foo", "bar", "baz"] 5 | } 6 | 7 | output "a" { 8 | value = var.a 9 | } 10 | 11 | output "b" { 12 | value = element(var.a, 1) 13 | } 14 | 15 | output "c" { 16 | value = length(var.a) 17 | } 18 | -------------------------------------------------------------------------------- /variables_example_09/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_map" { 3 | type = map(number) 4 | default = { 5 | "alpha" = 2 6 | "bravo" = 3 7 | } 8 | } 9 | 10 | output "map" { 11 | value = var.my_map 12 | } 13 | 14 | output "alpha_value" { 15 | value = var.my_map["alpha"] 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /resources_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | 7 | 8 | resource "aws_s3_bucket" "first_bucket" { 9 | bucket = "kevholditch-first-bucket333" 10 | acl = "private" 11 | 12 | versioning { 13 | enabled = true 14 | mfa_delete = false 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /provisioners_example_01/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## Commands to run 5 | 6 | 1. Generate keypair `ssh-keygen` 7 | File name: `nginx_key` 8 | No passphrase 9 | 10 | 1. You should now have two files in this directory `nginx_key` and `nginx_key.pub`. This keypair will be used with the EC2 instance that we create -------------------------------------------------------------------------------- /provisioners_example_02/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## Commands to run 5 | 6 | 1. Generate keypair `ssh-keygen` 7 | File name: `nginx_key` 8 | No passphrase 9 | 10 | 1. You should now have two files in this directory `nginx_key` and `nginx_key.pub`. This keypair will be used with the EC2 instance that we create -------------------------------------------------------------------------------- /provisioners_example_03/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## Commands to run 5 | 6 | 1. Generate keypair `ssh-keygen` 7 | File name: `nginx_key` 8 | No passphrase 9 | 10 | 1. You should now have two files in this directory `nginx_key` and `nginx_key.pub`. This keypair will be used with the EC2 instance that we create -------------------------------------------------------------------------------- /local_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | locals { 7 | first_part = "hello" 8 | second_part = "${local.first_part}-there" 9 | bucket_name = "${local.second_part}-how-are-you-today" 10 | } 11 | 12 | resource "aws_s3_bucket" "bucket" { 13 | bucket = local.bucket_name 14 | } 15 | -------------------------------------------------------------------------------- /modules_example_01/sqs-with-backoff/variables.tf: -------------------------------------------------------------------------------- 1 | variable "queue_name" { 2 | description = "Name of queue" 3 | } 4 | 5 | variable "max_receive_count"{ 6 | description = "The maximum number of times that a message can be received by consumers" 7 | default = 5 8 | } 9 | 10 | variable "visibility_timeout" { 11 | default = 30 12 | } 13 | -------------------------------------------------------------------------------- /modules_example_02/sqs-with-backoff/variables.tf: -------------------------------------------------------------------------------- 1 | variable "queue_name" { 2 | description = "Name of queue" 3 | } 4 | 5 | variable "max_receive_count"{ 6 | description = "The maximum number of times that a message can be received by consumers" 7 | default = 5 8 | } 9 | 10 | variable "visibility_timeout" { 11 | default = 30 12 | } 13 | -------------------------------------------------------------------------------- /providers_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | provider "aws" { 7 | region = "eu-west-2" 8 | alias = "london" 9 | } 10 | 11 | resource "aws_vpc" "ireland_vpc" { 12 | cidr_block = "10.0.0.0/16" 13 | } 14 | 15 | resource "aws_vpc" "london_vpc" { 16 | cidr_block = "10.1.0.0/16" 17 | provider = aws.london 18 | } -------------------------------------------------------------------------------- /variables_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | variable "bucket_name" { 8 | description = "the name of the bucket you wish to create" 9 | } 10 | 11 | variable "bucket_suffix" { 12 | default = "-abcd" 13 | } 14 | 15 | resource "aws_s3_bucket" "bucket" { 16 | bucket = "${var.bucket_name}${var.bucket_suffix}" 17 | } -------------------------------------------------------------------------------- /variables_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | variable "bucket_name" { 8 | description = "the name of the bucket you wish to create" 9 | } 10 | 11 | variable "bucket_suffix" { 12 | default = "-abcd" 13 | } 14 | 15 | resource "aws_s3_bucket" "bucket" { 16 | bucket = "${var.bucket_name}${var.bucket_suffix}" 17 | } -------------------------------------------------------------------------------- /advanced_resources_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | locals { 7 | fruit = toset(["apple", "orange", "banana"]) 8 | } 9 | 10 | resource "aws_sqs_queue" "queues" { 11 | for_each = local.fruit 12 | name = "queue-${each.key}" 13 | } 14 | 15 | output "queue-apple-name" { 16 | value = aws_sqs_queue.queues["apple"].name 17 | } -------------------------------------------------------------------------------- /modules_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | module "work_queue" { 7 | source = "./sqs-with-backoff" 8 | queue_name = "work-queue" 9 | } 10 | 11 | output "work_queue" { 12 | value = module.work_queue.queue 13 | } 14 | 15 | output "work_queue_dead_letter_queue" { 16 | value = module.work_queue.dead_letter_queue 17 | } 18 | 19 | -------------------------------------------------------------------------------- /modules_example_01/sqs-with-backoff/output.tf: -------------------------------------------------------------------------------- 1 | 2 | output "queue_arn" { 3 | value = aws_sqs_queue.sqs.arn 4 | } 5 | 6 | output "queue_name" { 7 | value = aws_sqs_queue.sqs.name 8 | } 9 | 10 | output "dead_letter_queue_arn" { 11 | value = aws_sqs_queue.sqs_dead_letter.arn 12 | } 13 | 14 | output "dead_letter_queue_name" { 15 | value = aws_sqs_queue.sqs_dead_letter.name 16 | } 17 | 18 | -------------------------------------------------------------------------------- /modules_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | module "work_queue" { 7 | source = "./sqs-with-backoff" 8 | queue_name = "work-queue" 9 | } 10 | 11 | output "work_queue_name" { 12 | value = module.work_queue.queue_name 13 | } 14 | 15 | output "work_queue_dead_letter_name" { 16 | value = module.work_queue.dead_letter_queue_name 17 | } 18 | 19 | -------------------------------------------------------------------------------- /variables_example_05/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "a" { 3 | type = string 4 | default = "foo" 5 | } 6 | 7 | variable "b" { 8 | type = bool 9 | default = true 10 | } 11 | 12 | variable "c" { 13 | type = number 14 | default = 123 15 | } 16 | 17 | output "a" { 18 | value = var.a 19 | } 20 | 21 | output "b" { 22 | value = var.b 23 | } 24 | 25 | output "c" { 26 | value = var.c 27 | } 28 | -------------------------------------------------------------------------------- /modules_example_04/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | module "work_queue" { 7 | source = "github.com/kevholditch/sqs-with-backoff" 8 | queue_name = "work-queue" 9 | } 10 | 11 | output "work_queue" { 12 | value = module.work_queue.queue 13 | } 14 | 15 | output "work_queue_dead_letter_queue" { 16 | value = module.work_queue.dead_letter_queue 17 | } 18 | 19 | -------------------------------------------------------------------------------- /modules_example_05/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | module "work_queue" { 7 | source = "github.com/kevholditch/sqs-with-backoff?ref=0.0.1" 8 | queue_name = "work-queue" 9 | } 10 | 11 | output "work_queue" { 12 | value = module.work_queue.queue 13 | } 14 | 15 | output "work_queue_dead_letter_queue" { 16 | value = module.work_queue.dead_letter_queue 17 | } 18 | 19 | -------------------------------------------------------------------------------- /variables_example_07/main.tf: -------------------------------------------------------------------------------- 1 | 2 | variable "my_set" { 3 | type = set(number) 4 | default = [7, 2, 2] 5 | } 6 | 7 | variable "my_list" { 8 | type = list(string) 9 | default = ["foo", "bar", "foo"] 10 | } 11 | 12 | output "set" { 13 | value = var.my_set 14 | } 15 | 16 | output "list" { 17 | value = var.my_list 18 | } 19 | 20 | output "list_as_set" { 21 | value = toset(var.my_list) 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /advanced_resources_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | 3 | provider "aws" { 4 | region = "eu-west-1" 5 | } 6 | 7 | resource "aws_sqs_queue" "queue0" { 8 | name = "queue-0" 9 | } 10 | 11 | resource "aws_sqs_queue" "queue1" { 12 | name = "queue-1" 13 | } 14 | 15 | resource "aws_sqs_queue" "queue2" { 16 | name = "queue-2" 17 | } 18 | 19 | resource "aws_sqs_queue" "queue3" { 20 | name = "queue-3" 21 | } 22 | 23 | output "queue-0-name" { 24 | value = aws_sqs_queue.queue0.name 25 | } 26 | -------------------------------------------------------------------------------- /outputs_example_02/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_s3_bucket" "first_bucket" { 7 | bucket = "kevholditch-bucket-outputs" 8 | } 9 | 10 | output "bucket_name" { 11 | value = aws_s3_bucket.first_bucket.id 12 | } 13 | 14 | output "bucket_arn" { 15 | value = aws_s3_bucket.first_bucket.arn 16 | } 17 | 18 | output "bucket_information" { 19 | value = "bucket name: ${aws_s3_bucket.first_bucket.id}, bucket arn: ${aws_s3_bucket.first_bucket.arn}" 20 | } 21 | -------------------------------------------------------------------------------- /outputs_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_s3_bucket" "first_bucket" { 7 | bucket = "kevholditch-bucket-outputs" 8 | } 9 | 10 | output "bucket_name" { 11 | value = aws_s3_bucket.first_bucket.id 12 | } 13 | 14 | output "bucket_arn" { 15 | value = aws_s3_bucket.first_bucket.arn 16 | } 17 | 18 | output "bucket_information" { 19 | value = "bucket name: ${aws_s3_bucket.first_bucket.id}, bucket arn: ${aws_s3_bucket.first_bucket.arn}" 20 | } 21 | 22 | output "all" { 23 | value = aws_s3_bucket.first_bucket 24 | } 25 | -------------------------------------------------------------------------------- /data_source_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | data "aws_s3_bucket" "bucket" { 7 | bucket = "kevholditch-already-exists" 8 | } 9 | 10 | resource "aws_iam_policy" "my_bucket_policy" { 11 | name = "my-bucket-policy" 12 | 13 | policy = < /usr/share/nginx/html/index.html 91 | systemctl start nginx 92 | EOF 93 | 94 | } 95 | 96 | -------------------------------------------------------------------------------- /provisioners_example_03/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "vpc" { 7 | cidr_block = "10.0.0.0/16" 8 | } 9 | 10 | resource "aws_internet_gateway" "main" { 11 | vpc_id = aws_vpc.vpc.id 12 | } 13 | 14 | resource "aws_subnet" "public" { 15 | vpc_id = aws_vpc.vpc.id 16 | cidr_block = aws_vpc.vpc.cidr_block 17 | map_public_ip_on_launch = true 18 | availability_zone = "eu-west-1a" 19 | } 20 | 21 | resource "aws_route_table" "public" { 22 | vpc_id = aws_vpc.vpc.id 23 | 24 | route { 25 | cidr_block = "0.0.0.0/0" 26 | gateway_id = aws_internet_gateway.main.id 27 | } 28 | } 29 | 30 | resource "aws_route_table_association" "gateway_route" { 31 | subnet_id = aws_subnet.public.id 32 | route_table_id = aws_route_table.public.id 33 | } 34 | 35 | resource "aws_security_group" "rules" { 36 | name = "example" 37 | vpc_id = aws_vpc.vpc.id 38 | 39 | ingress { 40 | from_port = 22 41 | to_port = 22 42 | protocol = "tcp" 43 | cidr_blocks = ["${var.my_ip}/32"] 44 | } 45 | 46 | ingress { 47 | from_port = 80 48 | to_port = 80 49 | protocol = "tcp" 50 | cidr_blocks = ["0.0.0.0/0"] 51 | } 52 | 53 | egress { 54 | from_port = 0 55 | to_port = 0 56 | protocol = "-1" 57 | cidr_blocks = ["0.0.0.0/0"] 58 | } 59 | } 60 | 61 | resource "aws_key_pair" "keypair" { 62 | key_name = "key" 63 | public_key = file("nginx_key.pub") 64 | } 65 | 66 | data "aws_ami" "ami" { 67 | most_recent = true 68 | owners = ["amazon"] 69 | 70 | filter { 71 | name = "name" 72 | values = ["amzn2-ami-hvm-2.0.*-x86_64-gp2"] 73 | } 74 | } 75 | 76 | resource "aws_instance" "nginx" { 77 | ami = data.aws_ami.ami.image_id 78 | instance_type = "t2.micro" 79 | subnet_id = aws_subnet.public.id 80 | vpc_security_group_ids = [aws_security_group.rules.id] 81 | key_name = aws_key_pair.keypair.key_name 82 | } 83 | 84 | resource "null_resource" "setup" { 85 | provisioner "local-exec" { 86 | command = < /usr/share/nginx/html/index.html; sudo systemctl start nginx;' 88 | CMD 89 | } 90 | } -------------------------------------------------------------------------------- /provisioners_example_01/main.tf: -------------------------------------------------------------------------------- 1 | 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | resource "aws_vpc" "vpc" { 7 | cidr_block = "10.0.0.0/16" 8 | } 9 | 10 | resource "aws_internet_gateway" "main" { 11 | vpc_id = aws_vpc.vpc.id 12 | } 13 | 14 | resource "aws_subnet" "public" { 15 | vpc_id = aws_vpc.vpc.id 16 | cidr_block = aws_vpc.vpc.cidr_block 17 | map_public_ip_on_launch = true 18 | availability_zone = "eu-west-1a" 19 | } 20 | 21 | resource "aws_route_table" "public" { 22 | vpc_id = aws_vpc.vpc.id 23 | 24 | route { 25 | cidr_block = "0.0.0.0/0" 26 | gateway_id = aws_internet_gateway.main.id 27 | } 28 | } 29 | 30 | resource "aws_route_table_association" "gateway_route" { 31 | subnet_id = aws_subnet.public.id 32 | route_table_id = aws_route_table.public.id 33 | } 34 | 35 | resource "aws_security_group" "rules" { 36 | name = "example" 37 | vpc_id = aws_vpc.vpc.id 38 | 39 | ingress { 40 | from_port = 22 41 | to_port = 22 42 | protocol = "tcp" 43 | cidr_blocks = ["${var.my_ip}/32"] 44 | } 45 | 46 | ingress { 47 | from_port = 80 48 | to_port = 80 49 | protocol = "tcp" 50 | cidr_blocks = ["0.0.0.0/0"] 51 | } 52 | 53 | egress { 54 | from_port = 0 55 | to_port = 0 56 | protocol = "-1" 57 | cidr_blocks = ["0.0.0.0/0"] 58 | } 59 | } 60 | 61 | resource "aws_key_pair" "keypair" { 62 | key_name = "key" 63 | public_key = file("nginx_key.pub") 64 | } 65 | 66 | data "aws_ami" "ami" { 67 | most_recent = true 68 | owners = ["amazon"] 69 | 70 | filter { 71 | name = "name" 72 | values = ["amzn2-ami-hvm-2.0.*-x86_64-gp2"] 73 | } 74 | } 75 | 76 | resource "aws_instance" "nginx" { 77 | ami = data.aws_ami.ami.image_id 78 | instance_type = "t2.micro" 79 | subnet_id = aws_subnet.public.id 80 | vpc_security_group_ids = [aws_security_group.rules.id] 81 | key_name = aws_key_pair.keypair.key_name 82 | 83 | 84 | provisioner "remote-exec" { 85 | inline = [ 86 | "sudo amazon-linux-extras enable nginx1.12", 87 | "sudo yum -y install nginx", 88 | "sudo chmod 777 /usr/share/nginx/html/index.html", 89 | "echo \"Hello from nginx on AWS\" > /usr/share/nginx/html/index.html", 90 | "sudo systemctl start nginx", 91 | ] 92 | } 93 | 94 | connection { 95 | host = aws_instance.nginx.public_ip 96 | type = "ssh" 97 | user = "ec2-user" 98 | private_key = file("nginx_key") 99 | } 100 | } 101 | 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # terraform-beginner-to-master-examples 2 | Code examples for the book: [Terraform: From Beginner To Master: With Examples in AWS](https://leanpub.com/terraform-from-beginner-to-master) 3 | 4 | These code examples are organised by chapter name and order they appear in the chapter. 5 | 6 | ## Book chapter list 7 | 8 | ### Chapter 3 - Your First Terraform Project 9 | - [first terraform project](./first_terraform_project) 10 | 11 | ### Chapter 4 - Resources 12 | - [resources example 01](./resources_example_01) 13 | - [resources example 02](./resources_example_02) 14 | - [resources example 03](./resources_example_03) 15 | 16 | ### Chapter 5 - Providers 17 | - [providers example 01](./providers_example_01) 18 | - [providers example 02](./providers_example_02) 19 | - [providers example 03](./providers_example_03) 20 | 21 | ### Chapter 6 - Data Sources 22 | - [data source example 01](./data_source_example_01) 23 | 24 | ### Chapter 7 - Outputs 25 | - [outputs example 01](./outputs_example_01) 26 | - [outputs example 02](./outputs_example_02) 27 | - [outputs example 03](./outputs_example_03) 28 | 29 | ### Chapter 8 - Locals 30 | - [locals example 01](./local_example_01) 31 | - [locals example 02](./local_example_02) 32 | 33 | ## Chapter 9 - Templates and Files 34 | - [templatefile example 01](./templatefile_example_01) 35 | - [templatefile example 02](./templatefile_example_02) 36 | 37 | ## Chapter 10 - Variables 38 | - [variables example 01](./variables_example_01) 39 | - [variables example 02](./variables_example_02) 40 | - [variables example 03](./variables_example_03) 41 | - [variables example 04](./variables_example_04) 42 | - [variables example 05](./variables_example_05) 43 | - [variables example 06](./variables_example_06) 44 | - [variables example 07](./variables_example_07) 45 | - [variables example 08](./variables_example_08) 46 | - [variables example 09](./variables_example_09) 47 | - [variables example 10](./variables_example_10) 48 | - [variables example 11](./variables_example_11) 49 | 50 | ## Chapter 11 - Project Layout 51 | - [project layout example 01](./project_layout_example_01) 52 | 53 | ## Chapter 12 - Modules 54 | - [modules example 01](./modules_example_01) 55 | - [modules example 02](./modules_example_02) 56 | - [modules example 03](./modules_example_03) 57 | - [modules example 04](./modules_example_04) 58 | - [modules example 05](./modules_example_05) 59 | 60 | ## Chapter 13 - Plans 61 | - [plans example 01](./plans_example_01) 62 | - [plans example 02](./plans_example_02) 63 | - [plans example 03](./plans_example_03) 64 | - [plans example 04](./plans_example_04) 65 | 66 | ## Chapter 14 - State 67 | - [state example 01](./state_example_01) 68 | - [state example 02a](./state_example_02a) 69 | - [state example 02b](./state_example_02b) 70 | 71 | ## Chapter 15 - Workspaces 72 | - [workspace example 01](./workspaces_example_01) 73 | 74 | ## Chapter 16 - Provisioners 75 | - [provisioners example 01](./provisioners_example_01) 76 | - [provisioners example 02](./provisioners_example_02) 77 | - [provisioners example 03](./provisioners_example_03) 78 | 79 | 80 | --------------------------------------------------------------------------------