├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── 01_05_base ├── main.tf └── terraform.tfvars.backup ├── 02_01_variables ├── main.tf └── terraform.tfvars.backup ├── 02_02-07_variables ├── main.tf └── terraform.tfvars.backup ├── 02_08-09_expressions_functions ├── main.tf └── terraform.tfvars.backup ├── 02_10_count ├── main.tf └── terraform.tfvars.backup ├── 02_11-12_modules ├── main.tf └── terraform.tfvars.backup ├── 03_02-03_remotestate ├── commands.txt ├── main.tf ├── remote_resources │ └── s3_backend.tf └── terraform.tfvars.backup ├── 03_04_complex ├── datasource.tf ├── instance.tf ├── provider.tf ├── security_group.tf ├── terraform.tfvars.backup ├── variables.tf └── vpc.tf ├── 03_05-06_multi_environment ├── commands.txt ├── environments │ ├── 0_development │ │ ├── development.auto.tfvars.backup │ │ └── provider.tf │ ├── 1_qa │ │ ├── provider.tf │ │ └── qa.auto.tfvars.backup │ └── 2_production │ │ ├── production.auto.tfvars.backup │ │ └── provider.tf └── manifests │ ├── datasource.tf │ ├── instance.tf │ ├── provider.tf │ ├── security_group.tf │ ├── variables.tf │ └── vpc.tf ├── 03_07_custommodule ├── main.tf ├── modules │ └── nodejs-instance │ │ ├── LICENSE │ │ ├── README.md │ │ ├── main.tf │ │ ├── outputs.tf │ │ └── variables.tf ├── terraform.tfvars.backup └── variables.tf ├── 04_02_jenkins └── Jenkinsfile ├── 04_03_terraform_cloud ├── datasource.tf ├── instance.tf ├── provider.tf ├── security_group.tf ├── variables.backup ├── variables.tf └── vpc.tf ├── 04_04_terraform_cloud_cli ├── backend.tf ├── datasource.tf ├── instance.tf ├── provider.tf ├── security_group.tf ├── terraform.tfvars.backup ├── variables.tf └── vpc.tf ├── 04_05_terraform_cloud_vcs ├── datasource.tf ├── instance.tf ├── provider.tf ├── security_group.tf ├── terraform.tfvars.backup ├── variables.backup ├── variables.tf └── vpc.tf ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE └── README.md /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) deotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.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 -------------------------------------------------------------------------------- /01_05_base/main.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "ssh_key_name" {} 9 | 10 | variable "private_key_path" {} 11 | 12 | variable "region" { 13 | default = "us-east-2" 14 | } 15 | 16 | variable "vpc_cidr" { 17 | default = "172.16.0.0/16" 18 | } 19 | 20 | variable "subnet1_cidr" { 21 | default = "172.16.0.0/24" 22 | } 23 | 24 | # ////////////////////////////// 25 | # PROVIDERS 26 | # ////////////////////////////// 27 | provider "aws" { 28 | access_key = var.aws_access_key 29 | secret_key = var.aws_secret_key 30 | region = var.region 31 | } 32 | 33 | # ////////////////////////////// 34 | # RESOURCES 35 | # ////////////////////////////// 36 | 37 | # VPC 38 | resource "aws_vpc" "vpc1" { 39 | cidr_block = var.vpc_cidr 40 | enable_dns_hostnames = "true" 41 | } 42 | 43 | # SUBNET 44 | resource "aws_subnet" "subnet1" { 45 | cidr_block = var.subnet1_cidr 46 | vpc_id = aws_vpc.vpc1.id 47 | map_public_ip_on_launch = "true" 48 | availability_zone = data.aws_availability_zones.available.names[1] 49 | } 50 | 51 | # INTERNET_GATEWAY 52 | resource "aws_internet_gateway" "gateway1" { 53 | vpc_id = aws_vpc.vpc1.id 54 | } 55 | 56 | # ROUTE_TABLE 57 | resource "aws_route_table" "route_table1" { 58 | vpc_id = aws_vpc.vpc1.id 59 | 60 | route { 61 | cidr_block = "0.0.0.0/0" 62 | gateway_id = aws_internet_gateway.gateway1.id 63 | } 64 | } 65 | 66 | resource "aws_route_table_association" "route-subnet1" { 67 | subnet_id = aws_subnet.subnet1.id 68 | route_table_id = aws_route_table.route_table1.id 69 | } 70 | 71 | # SECURITY_GROUP 72 | resource "aws_security_group" "sg-nodejs-instance" { 73 | name = "nodejs_sg" 74 | vpc_id = aws_vpc.vpc1.id 75 | 76 | ingress { 77 | from_port = 80 78 | to_port = 80 79 | protocol = "tcp" 80 | cidr_blocks = ["0.0.0.0/0"] 81 | } 82 | 83 | ingress { 84 | from_port = 443 85 | to_port = 443 86 | protocol = "tcp" 87 | cidr_blocks = ["0.0.0.0/0"] 88 | } 89 | 90 | ingress { 91 | from_port = 22 92 | to_port = 22 93 | protocol = "tcp" 94 | cidr_blocks = ["0.0.0.0/0"] 95 | } 96 | 97 | egress { 98 | from_port = 0 99 | to_port = 0 100 | protocol = "-1" 101 | cidr_blocks = ["0.0.0.0/0"] 102 | } 103 | } 104 | 105 | # INSTANCE 106 | resource "aws_instance" "nodejs1" { 107 | ami = data.aws_ami.aws-linux.id 108 | instance_type = "t2.micro" 109 | subnet_id = aws_subnet.subnet1.id 110 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 111 | key_name = var.ssh_key_name 112 | 113 | connection { 114 | type = "ssh" 115 | host = self.public_ip 116 | user = "ec2-user" 117 | private_key = file(var.private_key_path) 118 | } 119 | } 120 | 121 | 122 | # ////////////////////////////// 123 | # DATA 124 | # ////////////////////////////// 125 | data "aws_availability_zones" "available" { 126 | state = "available" 127 | } 128 | 129 | data "aws_ami" "aws-linux" { 130 | most_recent = true 131 | owners = ["amazon"] 132 | 133 | filter { 134 | name = "name" 135 | values = ["amzn-ami-hvm*"] 136 | } 137 | 138 | filter { 139 | name = "root-device-type" 140 | values = ["ebs"] 141 | } 142 | 143 | filter { 144 | name = "virtualization-type" 145 | values = ["hvm"] 146 | } 147 | } 148 | 149 | # ////////////////////////////// 150 | # OUTPUT 151 | # ////////////////////////////// 152 | output "instance-dns" { 153 | value = aws_instance.nodejs1.public_dns 154 | } -------------------------------------------------------------------------------- /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 = "" -------------------------------------------------------------------------------- /02_01_variables/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 | variable "vpc_cidr" { 13 | default = "172.16.0.0/16" 14 | } 15 | 16 | variable "subnet1_cidr" { 17 | default = "172.16.0.0/24" 18 | } 19 | 20 | variable "environment_list" { 21 | type = list(string) 22 | default = ["DEV","QA","STAGE","PROD"] 23 | } 24 | 25 | variable "environment_map" { 26 | type = map(string) 27 | default = { 28 | "DEV" = "DEV", 29 | "QA" = "QA", 30 | "STAGE" = "STAGE", 31 | "PROD" = "PROD" 32 | } 33 | } 34 | 35 | variable "environment_instance_type" { 36 | type = map(string) 37 | default = { 38 | "DEV" = "t2.micro", 39 | "QA" = "t2.micro", 40 | "STAGE" = "t2.micro", 41 | "PROD" = "t2.micro" 42 | } 43 | } 44 | 45 | variable "environment_instance_settings" { 46 | type = map(object({instance_type=string, monitoring=bool})) 47 | default = { 48 | "DEV" = { 49 | instance_type = "t2.micro", 50 | monitoring = false 51 | }, 52 | "QA" = { 53 | instance_type = "t2.micro", 54 | monitoring = false 55 | }, 56 | "STAGE" = { 57 | instance_type = "t2.micro", 58 | monitoring = false 59 | }, 60 | "PROD" = { 61 | instance_type = "t2.micro", 62 | monitoring = true 63 | } 64 | } 65 | } 66 | 67 | # ////////////////////////////// 68 | # PROVIDERS 69 | # ////////////////////////////// 70 | provider "aws" { 71 | access_key = var.aws_access_key 72 | secret_key = var.aws_secret_key 73 | region = var.region 74 | } 75 | 76 | # ////////////////////////////// 77 | # RESOURCES 78 | # ////////////////////////////// 79 | 80 | # VPC 81 | resource "aws_vpc" "vpc1" { 82 | cidr_block = var.vpc_cidr 83 | enable_dns_hostnames = "true" 84 | } 85 | 86 | # SUBNET 87 | resource "aws_subnet" "subnet1" { 88 | cidr_block = var.subnet1_cidr 89 | vpc_id = aws_vpc.vpc1.id 90 | map_public_ip_on_launch = "true" 91 | availability_zone = data.aws_availability_zones.available.names[1] 92 | } 93 | 94 | # INTERNET_GATEWAY 95 | resource "aws_internet_gateway" "gateway1" { 96 | vpc_id = aws_vpc.vpc1.id 97 | } 98 | 99 | # ROUTE_TABLE 100 | resource "aws_route_table" "route_table1" { 101 | vpc_id = aws_vpc.vpc1.id 102 | 103 | route { 104 | cidr_block = "0.0.0.0/0" 105 | gateway_id = aws_internet_gateway.gateway1.id 106 | } 107 | } 108 | 109 | resource "aws_route_table_association" "route-subnet1" { 110 | subnet_id = aws_subnet.subnet1.id 111 | route_table_id = aws_route_table.route_table1.id 112 | } 113 | 114 | # SECURITY_GROUP 115 | resource "aws_security_group" "sg-nodejs-instance" { 116 | name = "nodejs_sg" 117 | vpc_id = aws_vpc.vpc1.id 118 | 119 | ingress { 120 | from_port = 80 121 | to_port = 80 122 | protocol = "tcp" 123 | cidr_blocks = ["0.0.0.0/0"] 124 | } 125 | 126 | ingress { 127 | from_port = 22 128 | to_port = 22 129 | protocol = "tcp" 130 | cidr_blocks = ["0.0.0.0/0"] 131 | } 132 | 133 | egress { 134 | from_port = 0 135 | to_port = 0 136 | protocol = "-1" 137 | cidr_blocks = ["0.0.0.0/0"] 138 | } 139 | } 140 | 141 | # INSTANCE 142 | resource "aws_instance" "nodejs1" { 143 | ami = data.aws_ami.aws-linux.id 144 | instance_type = var.environment_instance_type["DEV"] 145 | //instance_type = var.environment_instance_settings["PROD"].instance_type 146 | subnet_id = aws_subnet.subnet1.id 147 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 148 | 149 | monitoring = var.environment_instance_settings["PROD"].monitoring 150 | 151 | tags = {Environment = var.environment_list[0]} 152 | 153 | } 154 | 155 | # ////////////////////////////// 156 | # DATA 157 | # ////////////////////////////// 158 | data "aws_availability_zones" "available" {} 159 | 160 | data "aws_ami" "aws-linux" { 161 | most_recent = true 162 | owners = ["amazon"] 163 | 164 | filter { 165 | name = "name" 166 | values = ["amzn-ami-hvm*"] 167 | } 168 | 169 | filter { 170 | name = "root-device-type" 171 | values = ["ebs"] 172 | } 173 | 174 | filter { 175 | name = "virtualization-type" 176 | values = ["hvm"] 177 | } 178 | } 179 | 180 | # ////////////////////////////// 181 | # OUTPUT 182 | # ////////////////////////////// 183 | output "instance-dns" { 184 | value = aws_instance.nodejs1.public_dns 185 | } 186 | 187 | output "private-dns" { 188 | value = aws_instance.nodejs1.private_dns 189 | } -------------------------------------------------------------------------------- /02_01_variables/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_02-07_variables/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 | variable "vpc_cidr" { 13 | default = "172.16.0.0/16" 14 | } 15 | 16 | variable "subnet1_cidr" { 17 | default = "172.16.0.0/24" 18 | } 19 | 20 | variable "environment_list" { 21 | type = list(string) 22 | default = ["DEV","QA","STAGE","PROD"] 23 | } 24 | 25 | variable "environment_map" { 26 | type = map(string) 27 | default = { 28 | "DEV" = "DEV", 29 | "QA" = "QA", 30 | "STAGE" = "STAGE", 31 | "PROD" = "PROD" 32 | } 33 | } 34 | 35 | variable "environment_instance_type" { 36 | type = map(string) 37 | default = { 38 | "DEV" = "t2.micro", 39 | "QA" = "t2.micro", 40 | "STAGE" = "t2.micro", 41 | "PROD" = "t2.micro" 42 | } 43 | } 44 | 45 | variable "environment_instance_settings" { 46 | type = map(object({instance_type=string, monitoring=bool})) 47 | default = { 48 | "DEV" = { 49 | instance_type = "t2.micro", 50 | monitoring = false 51 | }, 52 | "QA" = { 53 | instance_type = "t2.micro", 54 | monitoring = false 55 | }, 56 | "STAGE" = { 57 | instance_type = "t2.micro", 58 | monitoring = false 59 | }, 60 | "PROD" = { 61 | instance_type = "t2.micro", 62 | monitoring = true 63 | } 64 | } 65 | } 66 | 67 | # ////////////////////////////// 68 | # PROVIDERS 69 | # ////////////////////////////// 70 | provider "aws" { 71 | access_key = var.aws_access_key 72 | secret_key = var.aws_secret_key 73 | region = var.region 74 | } 75 | 76 | # ////////////////////////////// 77 | # RESOURCES 78 | # ////////////////////////////// 79 | 80 | # VPC 81 | resource "aws_vpc" "vpc1" { 82 | cidr_block = var.vpc_cidr 83 | enable_dns_hostnames = "true" 84 | } 85 | 86 | # SUBNET 87 | resource "aws_subnet" "subnet1" { 88 | cidr_block = var.subnet1_cidr 89 | vpc_id = aws_vpc.vpc1.id 90 | map_public_ip_on_launch = "true" 91 | availability_zone = data.aws_availability_zones.available.names[1] 92 | } 93 | 94 | # INTERNET_GATEWAY 95 | resource "aws_internet_gateway" "gateway1" { 96 | vpc_id = aws_vpc.vpc1.id 97 | } 98 | 99 | # ROUTE_TABLE 100 | resource "aws_route_table" "route_table1" { 101 | vpc_id = aws_vpc.vpc1.id 102 | 103 | route { 104 | cidr_block = "0.0.0.0/0" 105 | gateway_id = aws_internet_gateway.gateway1.id 106 | } 107 | } 108 | 109 | resource "aws_route_table_association" "route-subnet1" { 110 | subnet_id = aws_subnet.subnet1.id 111 | route_table_id = aws_route_table.route_table1.id 112 | } 113 | 114 | # SECURITY_GROUP 115 | resource "aws_security_group" "sg-nodejs-instance" { 116 | name = "nodejs_sg" 117 | vpc_id = aws_vpc.vpc1.id 118 | 119 | ingress { 120 | from_port = 80 121 | to_port = 80 122 | protocol = "tcp" 123 | cidr_blocks = ["0.0.0.0/0"] 124 | } 125 | 126 | ingress { 127 | from_port = 22 128 | to_port = 22 129 | protocol = "tcp" 130 | cidr_blocks = ["0.0.0.0/0"] 131 | } 132 | 133 | egress { 134 | from_port = 0 135 | to_port = 0 136 | protocol = "-1" 137 | cidr_blocks = ["0.0.0.0/0"] 138 | } 139 | } 140 | 141 | # INSTANCE 142 | resource "aws_instance" "nodejs1" { 143 | ami = data.aws_ami.aws-linux.id 144 | instance_type = var.environment_instance_type["DEV"] 145 | //instance_type = var.environment_instance_settings["PROD"].instance_type 146 | subnet_id = aws_subnet.subnet1.id 147 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 148 | 149 | monitoring = var.environment_instance_settings["PROD"].monitoring 150 | 151 | tags = {Environment = var.environment_list[0]} 152 | 153 | } 154 | 155 | # ////////////////////////////// 156 | # DATA 157 | # ////////////////////////////// 158 | data "aws_availability_zones" "available" {} 159 | 160 | data "aws_ami" "aws-linux" { 161 | most_recent = true 162 | owners = ["amazon"] 163 | 164 | filter { 165 | name = "name" 166 | values = ["amzn-ami-hvm*"] 167 | } 168 | 169 | filter { 170 | name = "root-device-type" 171 | values = ["ebs"] 172 | } 173 | 174 | filter { 175 | name = "virtualization-type" 176 | values = ["hvm"] 177 | } 178 | } 179 | 180 | # ////////////////////////////// 181 | # OUTPUT 182 | # ////////////////////////////// 183 | output "instance-dns" { 184 | value = aws_instance.nodejs1.public_dns 185 | } -------------------------------------------------------------------------------- /02_02-07_variables/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_08-09_expressions_functions/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 | variable "vpc_cidr" { 13 | default = "172.16.0.0/16" 14 | } 15 | 16 | variable "subnet1_cidr" { 17 | default = "172.16.0.0/24" 18 | } 19 | 20 | variable "environment_list" { 21 | type = list(string) 22 | default = ["DEV","QA","STAGE","PROD"] 23 | } 24 | 25 | variable "environment_map" { 26 | type = map(string) 27 | default = { 28 | "DEV" = "DEV", 29 | "QA" = "QA", 30 | "STAGE" = "STAGE", 31 | "PROD" = "PROD" 32 | } 33 | } 34 | 35 | variable "environment_instance_type" { 36 | type = map(string) 37 | default = { 38 | "DEV" = "t2.micro", 39 | "QA" = "t2.micro", 40 | "STAGE" = "t2.micro", 41 | "PROD" = "t2.micro" 42 | } 43 | } 44 | 45 | variable "environment_instance_settings" { 46 | type = map(object({instance_type=string, monitoring=bool})) 47 | default = { 48 | "DEV" = { 49 | instance_type = "t2.micro", 50 | monitoring = false 51 | }, 52 | "QA" = { 53 | instance_type = "t2.micro", 54 | monitoring = false 55 | }, 56 | "STAGE" = { 57 | instance_type = "t2.micro", 58 | monitoring = false 59 | }, 60 | "PROD" = { 61 | instance_type = "t2.micro", 62 | monitoring = true 63 | } 64 | } 65 | } 66 | 67 | # ////////////////////////////// 68 | # PROVIDERS 69 | # ////////////////////////////// 70 | provider "aws" { 71 | access_key = var.aws_access_key 72 | secret_key = var.aws_secret_key 73 | region = var.region 74 | } 75 | 76 | # ////////////////////////////// 77 | # RESOURCES 78 | # ////////////////////////////// 79 | 80 | # VPC 81 | resource "aws_vpc" "vpc1" { 82 | cidr_block = var.vpc_cidr 83 | enable_dns_hostnames = "true" 84 | } 85 | 86 | # SUBNET 87 | resource "aws_subnet" "subnet1" { 88 | cidr_block = var.subnet1_cidr 89 | vpc_id = aws_vpc.vpc1.id 90 | map_public_ip_on_launch = "true" 91 | availability_zone = data.aws_availability_zones.available.names[1] 92 | } 93 | 94 | # INTERNET_GATEWAY 95 | resource "aws_internet_gateway" "gateway1" { 96 | vpc_id = aws_vpc.vpc1.id 97 | } 98 | 99 | # ROUTE_TABLE 100 | resource "aws_route_table" "route_table1" { 101 | vpc_id = aws_vpc.vpc1.id 102 | 103 | route { 104 | cidr_block = "0.0.0.0/0" 105 | gateway_id = aws_internet_gateway.gateway1.id 106 | } 107 | } 108 | 109 | resource "aws_route_table_association" "route-subnet1" { 110 | subnet_id = aws_subnet.subnet1.id 111 | route_table_id = aws_route_table.route_table1.id 112 | } 113 | 114 | # SECURITY_GROUP 115 | resource "aws_security_group" "sg-nodejs-instance" { 116 | name = "nodejs_sg" 117 | vpc_id = aws_vpc.vpc1.id 118 | 119 | ingress { 120 | from_port = 80 121 | to_port = 80 122 | protocol = "tcp" 123 | cidr_blocks = ["0.0.0.0/0"] 124 | } 125 | 126 | ingress { 127 | from_port = 22 128 | to_port = 22 129 | protocol = "tcp" 130 | cidr_blocks = ["0.0.0.0/0"] 131 | } 132 | 133 | egress { 134 | from_port = 0 135 | to_port = 0 136 | protocol = "-1" 137 | cidr_blocks = ["0.0.0.0/0"] 138 | } 139 | } 140 | 141 | # INSTANCE 142 | resource "aws_instance" "nodejs1" { 143 | ami = data.aws_ami.aws-linux.id 144 | instance_type = var.environment_instance_type["DEV"] 145 | //instance_type = var.environment_instance_settings["PROD"].instance_type 146 | subnet_id = aws_subnet.subnet1.id 147 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 148 | 149 | monitoring = var.environment_instance_settings["PROD"].monitoring 150 | 151 | tags = {Environment = var.environment_list[0]} 152 | 153 | } 154 | 155 | # ////////////////////////////// 156 | # DATA 157 | # ////////////////////////////// 158 | data "aws_availability_zones" "available" {} 159 | 160 | data "aws_ami" "aws-linux" { 161 | most_recent = true 162 | owners = ["amazon"] 163 | 164 | filter { 165 | name = "name" 166 | values = ["amzn-ami-hvm*"] 167 | } 168 | 169 | filter { 170 | name = "root-device-type" 171 | values = ["ebs"] 172 | } 173 | 174 | filter { 175 | name = "virtualization-type" 176 | values = ["hvm"] 177 | } 178 | } 179 | 180 | # ////////////////////////////// 181 | # OUTPUT 182 | # ////////////////////////////// 183 | output "instance-dns" { 184 | value = aws_instance.nodejs1.public_dns 185 | } -------------------------------------------------------------------------------- /02_08-09_expressions_functions/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /02_10_count/main.tf: -------------------------------------------------------------------------------- 1 | # ////////////////////////////// 2 | # VARIABLES 3 | # ////////////////////////////// 4 | variable "aws_access_key" {} 5 | 6 | variable "aws_secret_key" {} 7 | 8 | variable "iam_accounts" { 9 | type = set(string) 10 | } 11 | 12 | variable "region" { 13 | default = "us-east-2" 14 | } 15 | 16 | variable "vpc_cidr" { 17 | default = "172.16.0.0/16" 18 | } 19 | 20 | variable "subnet1_cidr" { 21 | default = "172.16.0.0/24" 22 | } 23 | 24 | variable "environment_list" { 25 | type = list(string) 26 | default = ["DEV","QA","STAGE","PROD"] 27 | } 28 | 29 | variable "environment_map" { 30 | type = map(string) 31 | default = { 32 | "DEV" = "DEV", 33 | "QA" = "QA", 34 | "STAGE" = "STAGE", 35 | "PROD" = "PROD" 36 | } 37 | } 38 | 39 | variable "environment_instance_type" { 40 | type = map(string) 41 | default = { 42 | "DEV" = "t2.micro", 43 | "QA" = "t2.micro", 44 | "STAGE" = "t2.micro", 45 | "PROD" = "t2.micro" 46 | } 47 | } 48 | 49 | variable "environment_instance_settings" { 50 | type = map(object({instance_type=string, monitoring=bool})) 51 | default = { 52 | "DEV" = { 53 | instance_type = "t2.micro", 54 | monitoring = false 55 | }, 56 | "QA" = { 57 | instance_type = "t2.micro", 58 | monitoring = false 59 | }, 60 | "STAGE" = { 61 | instance_type = "t2.micro", 62 | monitoring = false 63 | }, 64 | "PROD" = { 65 | instance_type = "t2.micro", 66 | monitoring = true 67 | } 68 | } 69 | } 70 | 71 | # ////////////////////////////// 72 | # PROVIDERS 73 | # ////////////////////////////// 74 | provider "aws" { 75 | access_key = var.aws_access_key 76 | secret_key = var.aws_secret_key 77 | region = var.region 78 | } 79 | 80 | # ////////////////////////////// 81 | # RESOURCES 82 | # ////////////////////////////// 83 | 84 | # VPC 85 | resource "aws_vpc" "vpc1" { 86 | cidr_block = var.vpc_cidr 87 | enable_dns_hostnames = "true" 88 | } 89 | 90 | # SUBNET 91 | resource "aws_subnet" "subnet1" { 92 | cidr_block = var.subnet1_cidr 93 | vpc_id = aws_vpc.vpc1.id 94 | map_public_ip_on_launch = "true" 95 | availability_zone = data.aws_availability_zones.available.names[1] 96 | } 97 | 98 | # INTERNET_GATEWAY 99 | resource "aws_internet_gateway" "gateway1" { 100 | vpc_id = aws_vpc.vpc1.id 101 | } 102 | 103 | # ROUTE_TABLE 104 | resource "aws_route_table" "route_table1" { 105 | vpc_id = aws_vpc.vpc1.id 106 | 107 | route { 108 | cidr_block = "0.0.0.0/0" 109 | gateway_id = aws_internet_gateway.gateway1.id 110 | } 111 | } 112 | 113 | resource "aws_route_table_association" "route-subnet1" { 114 | subnet_id = aws_subnet.subnet1.id 115 | route_table_id = aws_route_table.route_table1.id 116 | } 117 | 118 | # SECURITY_GROUP 119 | resource "aws_security_group" "sg-nodejs-instance" { 120 | name = "nodejs_sg" 121 | vpc_id = aws_vpc.vpc1.id 122 | 123 | ingress { 124 | from_port = 80 125 | to_port = 80 126 | protocol = "tcp" 127 | cidr_blocks = ["0.0.0.0/0"] 128 | } 129 | 130 | ingress { 131 | from_port = 22 132 | to_port = 22 133 | protocol = "tcp" 134 | cidr_blocks = ["0.0.0.0/0"] 135 | } 136 | 137 | egress { 138 | from_port = 0 139 | to_port = 0 140 | protocol = "-1" 141 | cidr_blocks = ["0.0.0.0/0"] 142 | } 143 | } 144 | 145 | # INSTANCE 146 | resource "aws_instance" "nodejs1" { 147 | //count = 4 148 | 149 | ami = data.aws_ami.aws-linux.id 150 | instance_type = var.environment_instance_settings["PROD"].instance_type 151 | subnet_id = aws_subnet.subnet1.id 152 | vpc_security_group_ids = [aws_security_group.sg-nodejs-instance.id] 153 | 154 | monitoring = var.environment_instance_settings["PROD"].monitoring 155 | 156 | tags = {Environment = var.environment_list[0]} 157 | } 158 | 159 | 160 | # ////////////////////////////// 161 | # DATA 162 | # ////////////////////////////// 163 | data "aws_availability_zones" "available" {} 164 | 165 | data "aws_ami" "aws-linux" { 166 | most_recent = true 167 | owners = ["amazon"] 168 | 169 | filter { 170 | name = "name" 171 | values = ["amzn-ami-hvm*"] 172 | } 173 | 174 | filter { 175 | name = "root-device-type" 176 | values = ["ebs"] 177 | } 178 | 179 | filter { 180 | name = "virtualization-type" 181 | values = ["hvm"] 182 | } 183 | } 184 | 185 | # ////////////////////////////// 186 | # OUTPUT 187 | # ////////////////////////////// 188 | output "instance-dns" { 189 | value = aws_instance.nodejs1.public_dns 190 | } -------------------------------------------------------------------------------- /02_10_count/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" 4 | 5 | iam_accounts = ["Bob","Sally","Mary","Joe"] -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /02_11-12_modules/terraform.tfvars.backup: -------------------------------------------------------------------------------- 1 | aws_access_key = "" 2 | 3 | aws_secret_key = "" -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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_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 = <