├── .gitignore ├── README.md ├── azure-aws ├── aws.tf └── azure.tf ├── azure-example └── main.tf ├── data-sources └── main.tf ├── dynamic-blocks └── main.tf ├── expressions └── main.tf ├── functions └── main.tf ├── getting-started └── main.tf ├── imports └── main.tf ├── module-examples ├── main.tf └── modules │ ├── main.tf │ ├── outputs.tf │ └── variables.tf ├── outputs └── main.tf ├── provisioners └── main.tf ├── remote-state └── main.tf ├── resource-exports-dependency └── main.tf ├── resource-meta-attributes └── main.tf ├── variables ├── main.tf ├── terraform.tfvars └── variables.tf └── workspaces ├── dev.tfvars ├── main.tf ├── prod.tfvars ├── terraform.tfvars └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | 11 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 12 | # .tfvars files are managed as part of configuration and so should be included in 13 | # version control. 14 | # 15 | # example.tfvars 16 | 17 | # Ignore override files as they are usually used to override resources locally and so 18 | # are not checked in 19 | override.tf 20 | override.tf.json 21 | *_override.tf 22 | *_override.tf.json 23 | 24 | # Include override files you do wish to add to version control using negated pattern 25 | # 26 | # !example_override.tf 27 | 28 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 29 | # example: *tfplan* 30 | 31 | # Ignore CLI configuration files 32 | .terraformrc 33 | terraform.rc 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Terraform examples from YouTube course 2 | 3 | Based on this [YouTube](https://www.youtube.com/playlist?list=PL8HowI-L-3_9bkocmR3JahQ4Y-Pbqs2Nt) series. 4 | 5 | The examples in this repo range from the very basics of creating an ec2 instance in AWS to creating workspaces and modules. 6 | -------------------------------------------------------------------------------- /azure-aws/aws.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 3.74" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | region = "us-west-2" 12 | } 13 | 14 | resource "aws_vpc" "main" { 15 | cidr_block = "10.0.0.0/16" 16 | } 17 | 18 | resource "aws_subnet" "web" { 19 | vpc_id = aws_vpc.main.id 20 | cidr_block = "10.0.1.0/16" 21 | } 22 | -------------------------------------------------------------------------------- /azure-aws/azure.tf: -------------------------------------------------------------------------------- 1 | provider "azurerm" { 2 | version = "~> 2.13" 3 | features {} 4 | } 5 | 6 | resource "azurerm_resource_group" "rg" { 7 | name = "tuts-rg" 8 | location = "westus" 9 | 10 | tags = { 11 | foo = "bar" 12 | } 13 | } 14 | 15 | resource "azurerm_virtual_network" "main" { 16 | name = "tuts-network" 17 | address_space = ["10.0.0.0/16"] 18 | location = azurerm_resource_group.rg.location 19 | resource_group_name = azurerm_resource_group.rg.name 20 | } 21 | 22 | resource "azurerm_subnet" "web" { 23 | name = "tuts-subnet" 24 | resource_group_name = azurerm_resource_group.rg.name 25 | virtual_network_name = azurerm_virtual_network.main.name 26 | address_prefixes = ["10.0.1.0/24"] 27 | } 28 | -------------------------------------------------------------------------------- /azure-example/main.tf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WillBrock/terraform-course-examples/2c826cbacba0c9fd57d68ce2b9c29e67820b14d5/azure-example/main.tf -------------------------------------------------------------------------------- /data-sources/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 3.74" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | region = "us-west-2" 12 | } 13 | 14 | # resource "aws_instance" "web" { 15 | # ami = data.aws_ami.ubuntu.id 16 | # instance_type = "t2.micro" 17 | # } 18 | 19 | data "aws_ami" "ubuntu" { 20 | most_recent = true 21 | owners = ["self"] 22 | 23 | filter { 24 | name = "name" 25 | values = ["tuts-ubuntu"] 26 | } 27 | } 28 | 29 | data "aws_vpc" "foo" { 30 | default = true 31 | } 32 | 33 | data "aws_vpc" "main" { 34 | filter { 35 | name = "tag:Name" 36 | values = ["will-vpc"] 37 | } 38 | } 39 | 40 | output "vpc" { 41 | value = data.aws_vpc.foo 42 | } 43 | -------------------------------------------------------------------------------- /dynamic-blocks/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 3.74" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | region = "us-west-2" 12 | } 13 | 14 | locals { 15 | ingress_rules = [{ 16 | port = 443 17 | description = "Port 443" 18 | }, 19 | { 20 | port = 80 21 | description = "Port 80" 22 | }] 23 | } 24 | 25 | resource "aws_security_group" "main" { 26 | name = "foo" 27 | vpc_id = data.aws_vpc.main.id 28 | 29 | dynamic "ingress" { 30 | for_each = local.ingress_rules 31 | 32 | content { 33 | description = ingress.value.description 34 | from_port = ingress.value.port 35 | to_port = ingress.value.port 36 | protocol = "tcp" 37 | cidr_blocks = ["0.0.0.0/0"] 38 | } 39 | } 40 | /* 41 | ingress { 42 | description = "Foo bar" 43 | from_port = 443 44 | to_port = 443 45 | protocol = "tcp" 46 | cidr_blocks = ["0.0.0.0/0"] 47 | } 48 | */ 49 | 50 | tags = { 51 | Name = "tuts security group" 52 | } 53 | } 54 | 55 | /* 56 | resource "aws_elastic_beanstalk_application" "tftest" { 57 | name = "tf-test-name" 58 | description = "tf-test-desc" 59 | } 60 | 61 | resource "aws_elastic_beanstalk_environment" "tfenvtest" { 62 | name = "tf-test-name" 63 | application = "${aws_elastic_beanstalk_application.tftest.name}" 64 | solution_stack_name = "64bit Amazon Linux 2015.03 v2.0.3 running Go 1.4" 65 | 66 | setting { 67 | namespace = "aws:ec2:vpc" 68 | name = "VPCId" 69 | value = "vpc-xxxxxxxx" 70 | } 71 | 72 | setting { 73 | namespace = "aws:ec2:vpc" 74 | name = "Subnets" 75 | value = "subnet-xxxxxxxx" 76 | } 77 | } 78 | */ 79 | -------------------------------------------------------------------------------- /expressions/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 3.74" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | region = "us-west-2" 12 | } 13 | 14 | data "aws_vpc" "main" { 15 | default = true 16 | } 17 | 18 | locals { 19 | baz = "hello" 20 | } 21 | 22 | variable "testing" { 23 | type = map 24 | default = { 25 | foo = 123 26 | bar = 555 27 | } 28 | } 29 | 30 | /* 31 | resource "aws_instance" "web" { 32 | count = 2 33 | ami = "ami-003634241a8fcdec0" 34 | instance_type = "t2.micro" 35 | 36 | tags = { 37 | Name = "Tuts Test ${count.index}" 38 | } 39 | } 40 | */ 41 | 42 | output "foo" { 43 | # lists out an list/array of instance ids 44 | #value = aws_instance.web[*].id 45 | #value = [for instance in aws_instance.web : instance.public_ip] 46 | #value = [for k, v in var.testing : upper(k)] 47 | value = [for k, v in var.testing : k if k == "foo"] 48 | } 49 | 50 | resource "aws_instance" "tuts" { 51 | ami = "ami-003634241a8fcdec0" 52 | instance_type = "t2.micro" 53 | 54 | tags = { 55 | Name = "Tuts Test ${local.baz}" 56 | foo = local.baz == "aaa" ? "yes" : "no" 57 | bar = <