├── 03_07_custommodule ├── modules │ └── nodejs-instance │ │ ├── README.md │ │ ├── outputs.tf │ │ ├── variables.tf │ │ ├── LICENSE │ │ └── main.tf ├── terraform.tfvars.backup ├── main.tf └── variables.tf ├── 02_01_variables ├── terraform.tfvars.backup └── main.tf ├── 02_11-12_modules ├── terraform.tfvars.backup └── main.tf ├── 02_02-07_variables ├── terraform.tfvars.backup └── main.tf ├── 03_02-03_remotestate ├── terraform.tfvars.backup ├── commands.txt ├── main.tf └── remote_resources │ └── s3_backend.tf ├── 02_08-09_expressions_functions ├── terraform.tfvars.backup └── main.tf ├── 03_04_complex ├── terraform.tfvars.backup ├── provider.tf ├── variables.tf ├── datasource.tf ├── vpc.tf ├── instance.tf └── security_group.tf ├── 04_04_terraform_cloud_cli ├── terraform.tfvars.backup ├── backend.tf ├── provider.tf ├── datasource.tf ├── vpc.tf ├── variables.tf ├── instance.tf └── security_group.tf ├── 04_05_terraform_cloud_vcs ├── terraform.tfvars.backup ├── variables.backup ├── provider.tf ├── datasource.tf ├── vpc.tf ├── variables.tf ├── instance.tf └── security_group.tf ├── 02_10_count ├── terraform.tfvars.backup └── main.tf ├── .github ├── CODEOWNERS ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE.md ├── 04_03_terraform_cloud ├── variables.backup ├── provider.tf ├── datasource.tf ├── variables.tf ├── vpc.tf ├── instance.tf └── security_group.tf ├── .gitignore ├── 03_05-06_multi_environment ├── manifests │ ├── provider.tf │ ├── datasource.tf │ ├── vpc.tf │ ├── variables.tf │ ├── instance.tf │ └── security_group.tf ├── environments │ ├── 1_qa │ │ ├── provider.tf │ │ └── qa.auto.tfvars.backup │ ├── 0_development │ │ ├── provider.tf │ │ └── development.auto.tfvars.backup │ └── 2_production │ │ ├── provider.tf │ │ └── production.auto.tfvars.backup └── commands.txt ├── 01_05_base ├── terraform.tfvars.backup └── main.tf ├── CONTRIBUTING.md ├── NOTICE ├── README.md ├── 04_02_jenkins └── Jenkinsfile └── LICENSE /03_07_custommodule/modules/nodejs-instance/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /02_01_variables/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_11-12_modules/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_02-07_variables/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /03_02-03_remotestate/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_08-09_expressions_functions/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /03_04_complex/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" -------------------------------------------------------------------------------- /03_07_custommodule/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" -------------------------------------------------------------------------------- /02_10_count/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | iam_accounts = ["Bob","Sally","Mary","Joe"] -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) deotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /04_03_terraform_cloud/variables.backup: -------------------------------------------------------------------------------- 1 | variable "instance_tags" { 2 | type = map 3 | default = { 4 | "environment" = "dev" 5 | } 6 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/variables.backup: -------------------------------------------------------------------------------- 1 | variable "instance_tags" { 2 | type = map 3 | default = { 4 | "environment" = "dev" 5 | } 6 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.terraform 2 | *.pem 3 | *.tfvars 4 | *.tfplan 5 | *.tfstate 6 | *.tfstate.backup 7 | *.lock.info 8 | .terraform 9 | .DS_Store 10 | commands.txt -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/backend.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | backend "remote" { 3 | organization = "red30" 4 | 5 | workspaces { 6 | name = "cli-workspace" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /03_07_custommodule/modules/nodejs-instance/outputs.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # OUTPUT 3 | # ////////////////////////////// 4 | output "instance_dns" { 5 | value = aws_instance.nodejs.*.public_dns 6 | } -------------------------------------------------------------------------------- /03_04_complex/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/1_qa/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/0_development/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/2_production/provider.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/1_qa/qa.auto.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" 6 | 7 | env_instance_type = "t2.micro" 8 | 9 | env_instance_tags = { 10 | "environment" = "qa" 11 | } 12 | 13 | env_instance_count = 2 -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/0_development/development.auto.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" 6 | 7 | env_instance_type = "t2.micro" 8 | 9 | env_instance_tags = { 10 | "environment" = "development" 11 | } 12 | 13 | env_instance_count = 1 -------------------------------------------------------------------------------- /03_05-06_multi_environment/environments/2_production/production.auto.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | region = "" 6 | 7 | env_instance_type = "t2.micro" 8 | 9 | env_instance_tags = { 10 | "environment" = "production" 11 | } 12 | 13 | env_instance_count = 4 -------------------------------------------------------------------------------- /03_02-03_remotestate/commands.txt: -------------------------------------------------------------------------------- 1 | terraform init \ 2 | -backend-config="bucket=red30-tfstate" \ 3 | -backend-config="key=red30/ecommerceapp/app.state" \ 4 | -backend-config="region=us-east-2" \ 5 | -backend-config="dynamodb_table=red30-tfstatelock" \ 6 | -backend-config="access_key={ACCESS_KEY}" \ 7 | -backend-config="secret_key={SECRET_KEY}" 8 | -------------------------------------------------------------------------------- /01_05_base/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | # MAC/LINUX 2 | # aws ec2 create-key-pair --key-name tf_key --query 'KeyMaterial' --output text > tf_key.pem 3 | ### 4 | # WINDOWS 5 | # aws ec2 create-key-pair --key-name tf_key --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath tf_key.pem 6 | 7 | aws_access_key = "" 8 | 9 | aws_secret_key = "" 10 | 11 | ssh_key_name = "" 12 | 13 | private_key_path = "" -------------------------------------------------------------------------------- /03_04_complex/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | # ////////////////////////////// 13 | # OUTPUT 14 | # ////////////////////////////// 15 | output "instance-ip" { 16 | value = module.ec2_cluster.public_ip 17 | } -------------------------------------------------------------------------------- /03_07_custommodule/main.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # PROVIDERS 3 | # ////////////////////////////// 4 | provider "aws" { 5 | access_key = var.aws_access_key 6 | secret_key = var.aws_secret_key 7 | region = var.region 8 | } 9 | 10 | module "node_instance" { 11 | source = "./modules/nodejs-instance" 12 | instance_count = 2 13 | environment_tags = { 14 | "environment_id" = "development" 15 | } 16 | } -------------------------------------------------------------------------------- /03_07_custommodule/modules/nodejs-instance/variables.tf: -------------------------------------------------------------------------------- 1 | variable "ami_id" { 2 | description = "The ID of the NodeJS AMI to deploy" 3 | default = "ami-6685a403" 4 | } 5 | 6 | variable instance_count { 7 | type = number 8 | default = 1 9 | } 10 | 11 | variable "vpc_cidr" { 12 | default = "172.16.0.0/16" 13 | } 14 | 15 | variable "subnet_cidr" { 16 | default = "172.16.0.0/24" 17 | } 18 | 19 | variable environment_tags { 20 | type = map(string) 21 | } -------------------------------------------------------------------------------- /03_04_complex/datasource.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # DATA 3 | # ////////////////////////////// 4 | data "aws_ami" "aws-linux" { 5 | most_recent = true 6 | owners = ["amazon"] 7 | 8 | filter { 9 | name = "name" 10 | values = ["amzn-ami-hvm*"] 11 | } 12 | 13 | filter { 14 | name = "root-device-type" 15 | values = ["ebs"] 16 | } 17 | 18 | filter { 19 | name = "virtualization-type" 20 | values = ["hvm"] 21 | } 22 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/datasource.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # DATA 3 | # ////////////////////////////// 4 | data "aws_ami" "aws-linux" { 5 | most_recent = true 6 | owners = ["amazon"] 7 | 8 | filter { 9 | name = "name" 10 | values = ["amzn-ami-hvm*"] 11 | } 12 | 13 | filter { 14 | name = "root-device-type" 15 | values = ["ebs"] 16 | } 17 | 18 | filter { 19 | name = "virtualization-type" 20 | values = ["hvm"] 21 | } 22 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/datasource.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # DATA 3 | # ////////////////////////////// 4 | data "aws_ami" "aws-linux" { 5 | most_recent = true 6 | owners = ["amazon"] 7 | 8 | filter { 9 | name = "name" 10 | values = ["amzn-ami-hvm*"] 11 | } 12 | 13 | filter { 14 | name = "root-device-type" 15 | values = ["ebs"] 16 | } 17 | 18 | filter { 19 | name = "virtualization-type" 20 | values = ["hvm"] 21 | } 22 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/datasource.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # DATA 3 | # ////////////////////////////// 4 | data "aws_ami" "aws-linux" { 5 | most_recent = true 6 | owners = ["amazon"] 7 | 8 | filter { 9 | name = "name" 10 | values = ["amzn-ami-hvm*"] 11 | } 12 | 13 | filter { 14 | name = "root-device-type" 15 | values = ["ebs"] 16 | } 17 | 18 | filter { 19 | name = "virtualization-type" 20 | values = ["hvm"] 21 | } 22 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/datasource.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # DATA 3 | # ////////////////////////////// 4 | data "aws_ami" "aws-linux" { 5 | most_recent = true 6 | owners = ["amazon"] 7 | 8 | filter { 9 | name = "name" 10 | values = ["amzn-ami-hvm*"] 11 | } 12 | 13 | filter { 14 | name = "root-device-type" 15 | values = ["ebs"] 16 | } 17 | 18 | filter { 19 | name = "virtualization-type" 20 | values = ["hvm"] 21 | } 22 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | variable "instance_count" { 13 | type = number 14 | default = 1 15 | } 16 | 17 | # ////////////////////////////// 18 | # OUTPUT 19 | # ////////////////////////////// 20 | output "instance-ip" { 21 | value = module.ec2_cluster.public_ip 22 | } -------------------------------------------------------------------------------- /03_04_complex/vpc.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VPC 3 | # ////////////////////////////// 4 | module "vpc" { 5 | source = "terraform-aws-modules/vpc/aws" 6 | name = "frontend-vpc" 7 | cidr = "10.0.0.0/16" 8 | 9 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 10 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 11 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 12 | 13 | enable_nat_gateway = true 14 | single_nat_gateway = true 15 | # one_nat_gateway_per_az = true 16 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/vpc.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VPC 3 | # ////////////////////////////// 4 | module "vpc" { 5 | source = "terraform-aws-modules/vpc/aws" 6 | name = "frontend-vpc" 7 | cidr = "10.0.0.0/16" 8 | 9 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 10 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 11 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 12 | 13 | enable_nat_gateway = true 14 | single_nat_gateway = true 15 | # one_nat_gateway_per_az = true 16 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/vpc.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VPC 3 | # ////////////////////////////// 4 | module "vpc" { 5 | source = "terraform-aws-modules/vpc/aws" 6 | name = "frontend-vpc" 7 | cidr = "10.0.0.0/16" 8 | 9 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 10 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 11 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 12 | 13 | enable_nat_gateway = true 14 | single_nat_gateway = true 15 | # one_nat_gateway_per_az = true 16 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/vpc.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VPC 3 | # ////////////////////////////// 4 | module "vpc" { 5 | source = "terraform-aws-modules/vpc/aws" 6 | name = "frontend-vpc" 7 | cidr = "10.0.0.0/16" 8 | 9 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 10 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 11 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 12 | 13 | enable_nat_gateway = true 14 | single_nat_gateway = true 15 | # one_nat_gateway_per_az = true 16 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/vpc.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VPC 3 | # ////////////////////////////// 4 | module "vpc" { 5 | source = "terraform-aws-modules/vpc/aws" 6 | name = "frontend-vpc" 7 | cidr = "10.0.0.0/16" 8 | 9 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 10 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 11 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 12 | 13 | enable_nat_gateway = true 14 | single_nat_gateway = true 15 | # one_nat_gateway_per_az = true 16 | } -------------------------------------------------------------------------------- /03_07_custommodule/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | variable "vpc_cidr" { 13 | default = "172.16.0.0/16" 14 | } 15 | 16 | variable "subnet_cidr" { 17 | default = "172.16.0.0/24" 18 | } 19 | 20 | # ////////////////////////////// 21 | # OUTPUT 22 | # ////////////////////////////// 23 | output "instance_dns" { 24 | value = module.node_instance.instance_dns 25 | } -------------------------------------------------------------------------------- /03_04_complex/instance.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # EC2 MODULE 3 | # ////////////////////////////// 4 | module "ec2_cluster" { 5 | source = "terraform-aws-modules/ec2-instance/aws" 6 | version = "~> 2.0" 7 | 8 | name = "frontend-linux" 9 | instance_count = 1 10 | 11 | ami = data.aws_ami.aws-linux.id 12 | instance_type = "t2.micro" 13 | 14 | vpc_security_group_ids = [aws_security_group.sg_frontend.id] 15 | subnet_id = module.vpc.public_subnets[1] 16 | 17 | } -------------------------------------------------------------------------------- /03_07_custommodule/modules/nodejs-instance/LICENSE: -------------------------------------------------------------------------------- 1 | Licensed under the Apache License, Version 2.0 (the "License"); 2 | you may not use this file except in compliance with the License. 3 | You may obtain a copy of the License at 4 | 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | variable env_instance_type {} 13 | 14 | 15 | variable env_instance_tags { 16 | type = map(string) 17 | } 18 | 19 | variable env_instance_count { 20 | type = number 21 | } 22 | 23 | # ////////////////////////////// 24 | # OUTPUT 25 | # ////////////////////////////// 26 | output "instance-ip" { 27 | value = module.ec2_cluster.public_ip 28 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | variable "instance_count" { 13 | type = number 14 | default = 1 15 | } 16 | 17 | variable "instance_tags" { 18 | type = map 19 | default = { 20 | "environment" = "dev" 21 | } 22 | } 23 | # ////////////////////////////// 24 | # OUTPUT 25 | # ////////////////////////////// 26 | output "instance-ip" { 27 | value = module.ec2_cluster.public_ip 28 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/instance.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # EC2 MODULE 3 | # ////////////////////////////// 4 | module "ec2_cluster" { 5 | source = "terraform-aws-modules/ec2-instance/aws" 6 | version = "~> 2.0" 7 | 8 | name = "frontend-linux" 9 | instance_count = var.instance_count 10 | 11 | ami = data.aws_ami.aws-linux.id 12 | instance_type = "t2.micro" 13 | 14 | vpc_security_group_ids = [aws_security_group.sg_frontend.id] 15 | subnet_id = module.vpc.public_subnets[1] 16 | 17 | tags = var.instance_tags 18 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/variables.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | variable "instance_count" { 13 | type = number 14 | default = 1 15 | } 16 | 17 | variable "instance_tags" { 18 | type = map 19 | default = { 20 | "environment" = "dev" 21 | } 22 | } 23 | 24 | # ////////////////////////////// 25 | # OUTPUT 26 | # ////////////////////////////// 27 | output "instance-ip" { 28 | value = module.ec2_cluster.public_ip 29 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/instance.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # EC2 MODULE 3 | # ////////////////////////////// 4 | module "ec2_cluster" { 5 | source = "terraform-aws-modules/ec2-instance/aws" 6 | version = "~> 2.0" 7 | 8 | name = "frontend-linux" 9 | instance_count = var.instance_count 10 | 11 | ami = data.aws_ami.aws-linux.id 12 | instance_type = "t2.micro" 13 | 14 | vpc_security_group_ids = [aws_security_group.sg_frontend.id] 15 | subnet_id = module.vpc.public_subnets[1] 16 | 17 | tags = var.instance_tags 18 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/instance.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # EC2 MODULE 3 | # ////////////////////////////// 4 | module "ec2_cluster" { 5 | source = "terraform-aws-modules/ec2-instance/aws" 6 | version = "~> 2.0" 7 | 8 | name = "frontend-linux" 9 | instance_count = var.instance_count 10 | 11 | ami = data.aws_ami.aws-linux.id 12 | instance_type = "t2.micro" 13 | 14 | vpc_security_group_ids = [aws_security_group.sg_frontend.id] 15 | subnet_id = module.vpc.public_subnets[1] 16 | 17 | tags = var.instance_tags 18 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2019 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/instance.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # EC2 MODULE 3 | # ////////////////////////////// 4 | module "ec2_cluster" { 5 | source = "terraform-aws-modules/ec2-instance/aws" 6 | version = "~> 2.0" 7 | 8 | name = "frontend-linux" 9 | instance_count = var.env_instance_count 10 | 11 | ami = data.aws_ami.aws-linux.id 12 | instance_type = var.env_instance_type 13 | 14 | vpc_security_group_ids = [aws_security_group.sg_frontend.id] 15 | subnet_id = module.vpc.public_subnets[1] 16 | 17 | tags = var.env_instance_tags 18 | } -------------------------------------------------------------------------------- /03_04_complex/security_group.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # SECURITY GROUP 3 | # ////////////////////////////// 4 | resource "aws_security_group" "sg_frontend" { 5 | name = "sg_frontend" 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ingress { 9 | from_port = 80 10 | to_port = 80 11 | protocol = "tcp" 12 | cidr_blocks = ["0.0.0.0/0"] 13 | } 14 | 15 | ingress { 16 | from_port = 443 17 | to_port = 443 18 | protocol = "tcp" 19 | cidr_blocks = ["0.0.0.0/0"] 20 | } 21 | 22 | ingress { 23 | from_port = 22 24 | to_port = 22 25 | protocol = "tcp" 26 | cidr_blocks = ["0.0.0.0/0"] 27 | } 28 | 29 | egress { 30 | from_port = 0 31 | to_port = 0 32 | protocol = "-1" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /04_03_terraform_cloud/security_group.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # SECURITY GROUP 3 | # ////////////////////////////// 4 | resource "aws_security_group" "sg_frontend" { 5 | name = "sg_frontend" 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ingress { 9 | from_port = 80 10 | to_port = 80 11 | protocol = "tcp" 12 | cidr_blocks = ["0.0.0.0/0"] 13 | } 14 | 15 | ingress { 16 | from_port = 443 17 | to_port = 443 18 | protocol = "tcp" 19 | cidr_blocks = ["0.0.0.0/0"] 20 | } 21 | 22 | ingress { 23 | from_port = 22 24 | to_port = 22 25 | protocol = "tcp" 26 | cidr_blocks = ["0.0.0.0/0"] 27 | } 28 | 29 | egress { 30 | from_port = 0 31 | to_port = 0 32 | protocol = "-1" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /04_04_terraform_cloud_cli/security_group.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # SECURITY GROUP 3 | # ////////////////////////////// 4 | resource "aws_security_group" "sg_frontend" { 5 | name = "sg_frontend" 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ingress { 9 | from_port = 80 10 | to_port = 80 11 | protocol = "tcp" 12 | cidr_blocks = ["0.0.0.0/0"] 13 | } 14 | 15 | ingress { 16 | from_port = 443 17 | to_port = 443 18 | protocol = "tcp" 19 | cidr_blocks = ["0.0.0.0/0"] 20 | } 21 | 22 | ingress { 23 | from_port = 22 24 | to_port = 22 25 | protocol = "tcp" 26 | cidr_blocks = ["0.0.0.0/0"] 27 | } 28 | 29 | egress { 30 | from_port = 0 31 | to_port = 0 32 | protocol = "-1" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /04_05_terraform_cloud_vcs/security_group.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # SECURITY GROUP 3 | # ////////////////////////////// 4 | resource "aws_security_group" "sg_frontend" { 5 | name = "sg_frontend" 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ingress { 9 | from_port = 80 10 | to_port = 80 11 | protocol = "tcp" 12 | cidr_blocks = ["0.0.0.0/0"] 13 | } 14 | 15 | ingress { 16 | from_port = 443 17 | to_port = 443 18 | protocol = "tcp" 19 | cidr_blocks = ["0.0.0.0/0"] 20 | } 21 | 22 | ingress { 23 | from_port = 22 24 | to_port = 22 25 | protocol = "tcp" 26 | cidr_blocks = ["0.0.0.0/0"] 27 | } 28 | 29 | egress { 30 | from_port = 0 31 | to_port = 0 32 | protocol = "-1" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/manifests/security_group.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # SECURITY GROUP 3 | # ////////////////////////////// 4 | resource "aws_security_group" "sg_frontend" { 5 | name = "sg_frontend" 6 | vpc_id = module.vpc.vpc_id 7 | 8 | ingress { 9 | from_port = 80 10 | to_port = 80 11 | protocol = "tcp" 12 | cidr_blocks = ["0.0.0.0/0"] 13 | } 14 | 15 | ingress { 16 | from_port = 443 17 | to_port = 443 18 | protocol = "tcp" 19 | cidr_blocks = ["0.0.0.0/0"] 20 | } 21 | 22 | ingress { 23 | from_port = 22 24 | to_port = 22 25 | protocol = "tcp" 26 | cidr_blocks = ["0.0.0.0/0"] 27 | } 28 | 29 | egress { 30 | from_port = 0 31 | to_port = 0 32 | protocol = "-1" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /03_02-03_remotestate/main.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # BACKEND 3 | # ////////////////////////////// 4 | terraform { 5 | backend "s3" { 6 | } 7 | } 8 | 9 | # ////////////////////////////// 10 | # VARIABLES 11 | # ////////////////////////////// 12 | variable "aws_access_key" {} 13 | 14 | variable "aws_secret_key" {} 15 | 16 | variable "region" { 17 | default = "us-east-2" 18 | } 19 | 20 | variable "vpc_cidr" { 21 | default = "172.16.0.0/16" 22 | } 23 | 24 | variable "subnet1_cidr" { 25 | default = "172.16.0.0/24" 26 | } 27 | 28 | # ////////////////////////////// 29 | # PROVIDERS 30 | # ////////////////////////////// 31 | provider "aws" { 32 | access_key = var.aws_access_key 33 | secret_key = var.aws_secret_key 34 | region = var.region 35 | } 36 | 37 | 38 | # ////////////////////////////// 39 | # MODULES 40 | # ////////////////////////////// 41 | module "vpc" { 42 | source = "terraform-aws-modules/vpc/aws" 43 | 44 | name = "vpc-module-example" 45 | 46 | cidr = "10.0.0.0/16" 47 | 48 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 49 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 50 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 51 | 52 | enable_nat_gateway = true 53 | single_nat_gateway = true 54 | } -------------------------------------------------------------------------------- /03_05-06_multi_environment/commands.txt: -------------------------------------------------------------------------------- 1 | export AWS_PAGER="" 2 | 3 | --ALL INSTANCES-- 4 | aws ec2 describe-instances \ 5 | --filters Name=tag-key,Values=Name \ 6 | --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,Environment:Tags[?Key==`environment`]|[0].Value}' \ 7 | --output table 8 | 9 | --DEV INSTANCES-- 10 | aws ec2 describe-instances \ 11 | --filters Name=tag:environment,Values=development \ 12 | --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,Environment:Tags[?Key==`environment`]|[0].Value}' \ 13 | --output table 14 | 15 | --QA INSTANCES-- 16 | aws ec2 describe-instances \ 17 | --filters Name=tag:environment,Values=qa \ 18 | --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,Environment:Tags[?Key==`environment`]|[0].Value}' \ 19 | --output table 20 | 21 | --PRODUCTION INSTANCES-- 22 | aws ec2 describe-instances \ 23 | --filters Name=tag:environment,Values=production \ 24 | --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,Environment:Tags[?Key==`environment`]|[0].Value}' \ 25 | --output table 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Terraform 2 | This is the repository for the LinkedIn Learning course Advanced Terraform. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Advanced Terraform][lil-thumbnail-url] 5 | Terraform simplifies and accelerates the configuration of cloud-based environments. DevOps engineers looking to use Terraform in the real world can start by learning how to work with the Terraform CLI and the HashiCorp Configuration Language (HCL). In this course, David Swersky covers these concepts and more, helping you go beyond the basics with this powerful infrastructure as code solution. Using practical use cases, David shows how to manage and automate your infrastructure with Terraform. He steps through how to analyze an existing application running in a client's data center, and design a Terraform configuration that supports the application in AWS. He also goes over advanced concepts, including how to set the values of variables in a Terraform configuration. Plus, he steps through how to develop an application infrastructure with Terraform, create an infrastructure CI/CD pipeline using GitHub and Terraform Cloud, and more. 6 | 7 | ## Instructions 8 | This repository has a folder for each of the videos in the course. The naming convention is [CHAPTER]_[NAME], e.g.: **01_05_base** 9 | 10 | 11 | ## Installing 12 | 1. To use these exercise files, you must have the following installed: 13 | - [list of requirements for course] 14 | 2. Fork this repository to your own account 15 | 3. Clone your fork to your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 16 | 17 | ### Instructor 18 | 19 | **David Swersky** 20 | 21 | _DevOps and Enterprise Architect with 20+ years of IT experience_ 22 | 23 | Check out some of my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/david-swersky). 24 | 25 | [lil-course-url]: https://www.linkedin.com/learning/advanced-terraform 26 | [lil-thumbnail-url]: https://cdn.lynda.com/course/2823489/2823489-1604938909984-16x9.jpg 27 | -------------------------------------------------------------------------------- /03_07_custommodule/modules/nodejs-instance/main.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | resource "aws_vpc" "vpc" { 3 | cidr_block = var.vpc_cidr 4 | enable_dns_hostnames = "true" 5 | } 6 | 7 | # SUBNET 8 | resource "aws_subnet" "subnet" { 9 | cidr_block = var.subnet_cidr 10 | vpc_id = aws_vpc.vpc.id 11 | map_public_ip_on_launch = "true" 12 | availability_zone = data.aws_availability_zones.available.names[1] 13 | } 14 | 15 | # INTERNET_GATEWAY 16 | resource "aws_internet_gateway" "igw" { 17 | vpc_id = aws_vpc.vpc.id 18 | } 19 | 20 | # ROUTE_TABLE 21 | resource "aws_route_table" "route_table" { 22 | vpc_id = aws_vpc.vpc.id 23 | 24 | route { 25 | cidr_block = "0.0.0.0/0" 26 | gateway_id = aws_internet_gateway.igw.id 27 | } 28 | } 29 | 30 | resource "aws_route_table_association" "rta" { 31 | subnet_id = aws_subnet.subnet.id 32 | route_table_id = aws_route_table.route_table.id 33 | } 34 | 35 | # SECURITY_GROUP 36 | resource "aws_security_group" "sg-nodejs-instance" { 37 | name = "nodejs_sg" 38 | vpc_id = aws_vpc.vpc.id 39 | 40 | ingress { 41 | from_port = 80 42 | to_port = 80 43 | protocol = "tcp" 44 | cidr_blocks = ["0.0.0.0/0"] 45 | } 46 | 47 | ingress { 48 | from_port = 443 49 | to_port = 443 50 | protocol = "tcp" 51 | cidr_blocks = ["0.0.0.0/0"] 52 | } 53 | 54 | ingress { 55 | from_port = 22 56 | to_port = 22 57 | protocol = "tcp" 58 | cidr_blocks = ["0.0.0.0/0"] 59 | } 60 | 61 | egress { 62 | from_port = 0 63 | to_port = 0 64 | protocol = "-1" 65 | cidr_blocks = ["0.0.0.0/0"] 66 | } 67 | } 68 | 69 | # INSTANCE 70 | resource "aws_instance" "nodejs" { 71 | count = var.instance_count 72 | 73 | ami = var.ami_id 74 | instance_type = "t2.micro" 75 | subnet_id = aws_subnet.subnet.id 76 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 77 | 78 | tags = var.environment_tags 79 | } 80 | 81 | # ////////////////////////////// 82 | # DATA 83 | # ////////////////////////////// 84 | data "aws_availability_zones" "available" { 85 | state = "available" 86 | } -------------------------------------------------------------------------------- /02_11-12_modules/main.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "region" { 9 | default = "us-east-2" 10 | } 11 | 12 | 13 | # ////////////////////////////// 14 | # PROVIDERS 15 | # ////////////////////////////// 16 | provider "aws" { 17 | access_key = var.aws_access_key 18 | secret_key = var.aws_secret_key 19 | region = var.region 20 | } 21 | 22 | # ////////////////////////////// 23 | # SECURITY GROUP 24 | # ////////////////////////////// 25 | resource "aws_security_group" "sg_frontend" { 26 | name = "sg_frontend" 27 | vpc_id = module.vpc.vpc_id 28 | 29 | ingress { 30 | from_port = 80 31 | to_port = 80 32 | protocol = "tcp" 33 | cidr_blocks = ["0.0.0.0/0"] 34 | } 35 | 36 | ingress { 37 | from_port = 443 38 | to_port = 443 39 | protocol = "tcp" 40 | cidr_blocks = ["0.0.0.0/0"] 41 | } 42 | 43 | ingress { 44 | from_port = 22 45 | to_port = 22 46 | protocol = "tcp" 47 | cidr_blocks = ["0.0.0.0/0"] 48 | } 49 | 50 | egress { 51 | from_port = 0 52 | to_port = 0 53 | protocol = "-1" 54 | cidr_blocks = ["0.0.0.0/0"] 55 | } 56 | 57 | } 58 | 59 | # ////////////////////////////// 60 | # MODULES 61 | # ////////////////////////////// 62 | module "vpc" { 63 | source = "terraform-aws-modules/vpc/aws" 64 | name = "frontend-vpc" 65 | cidr = "10.0.0.0/16" 66 | 67 | azs = ["us-east-2a", "us-east-2b", "us-east-2c"] 68 | private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] 69 | public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] 70 | 71 | enable_nat_gateway = true 72 | single_nat_gateway = true 73 | # one_nat_gateway_per_az = true 74 | } 75 | 76 | 77 | # ////////////////////////////// 78 | # DATA 79 | # ////////////////////////////// 80 | data "aws_ami" "aws-linux" { 81 | most_recent = true 82 | owners = ["amazon"] 83 | 84 | filter { 85 | name = "name" 86 | values = ["amzn-ami-hvm*"] 87 | } 88 | 89 | filter { 90 | name = "root-device-type" 91 | values = ["ebs"] 92 | } 93 | 94 | filter { 95 | name = "virtualization-type" 96 | values = ["hvm"] 97 | } 98 | } -------------------------------------------------------------------------------- /03_02-03_remotestate/remote_resources/s3_backend.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "bucket_name" { 9 | default = "red30-tfstate" 10 | } 11 | 12 | # ////////////////////////////// 13 | # PROVIDER 14 | # ////////////////////////////// 15 | provider "aws" { 16 | access_key = var.aws_access_key 17 | secret_key = var.aws_secret_key 18 | region = "us-east-2" 19 | } 20 | 21 | # ////////////////////////////// 22 | # TERRAFORM USER 23 | # ////////////////////////////// 24 | data "aws_iam_user" "terraform" { 25 | user_name = "terraform" 26 | } 27 | 28 | # ////////////////////////////// 29 | # S3 BUCKET 30 | # ////////////////////////////// 31 | resource "aws_s3_bucket" "red30-tfremotestate" { 32 | bucket = var.bucket_name 33 | force_destroy = true 34 | acl = "private" 35 | 36 | versioning { 37 | enabled = true 38 | } 39 | 40 | # Grant read/write access to the terraform user 41 | policy = <