├── 2024 ├── 1_Example.md ├── 2_Example.md ├── TerraformCode_Explanation.md ├── 3_Example.md ├── Terraform_Interview_Questions.md ├── Terrafom_Project_1.md ├── Dependency_in_Terraform.md ├── What is Terraform.md └── What is Outputs_tf.md ├── terraform-aws-examples-master └── My_Terraform_AWS_Examples │ ├── AWS_Examples │ ├── 1 │ │ ├── main.tf │ │ └── README.md │ ├── 2 │ │ ├── main.tf │ │ └── README.md │ ├── 3 │ │ ├── main.tf │ │ └── README.md │ ├── 4 │ │ ├── outputs.tf │ │ ├── vars.tf │ │ ├── main.tf │ │ └── README.md │ ├── 5 │ │ ├── outputs.tf │ │ ├── vars.tf │ │ └── main.tf │ ├── 6 │ │ ├── outputs.tf │ │ ├── vars.tf │ │ ├── main.tf │ │ └── README.md │ ├── 7 │ │ ├── main.tf │ │ ├── backend.tf │ │ └── README.md │ ├── 8 │ │ ├── global │ │ │ └── s3 │ │ │ │ ├── outputs.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ ├── stage │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── user-data.sh │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── README.md │ │ │ │ └── main.tf │ │ └── README.md │ ├── 9 │ │ ├── global │ │ │ └── s3 │ │ │ │ ├── outputs.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ ├── prod │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ ├── stage │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ ├── modules │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── user-data.sh │ │ │ │ ├── outputs.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── README.md │ │ │ │ └── main.tf │ │ └── README.md │ ├── 10 │ │ └── live │ │ │ ├── global │ │ │ └── s3 │ │ │ │ ├── outputs.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ │ ├── prod │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ │ └── stage │ │ │ ├── data-stores │ │ │ └── mysql │ │ │ │ ├── vars.tf │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ │ └── services │ │ │ └── webserver-cluster │ │ │ ├── outputs.tf │ │ │ ├── backend.tf │ │ │ ├── vars.tf │ │ │ ├── main.tf │ │ │ └── README.md │ ├── 11 │ │ ├── outputs.tf │ │ ├── vars.tf │ │ ├── main.tf │ │ └── README.md │ ├── 12 │ │ ├── live │ │ │ ├── global │ │ │ │ └── s3 │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ ├── prod │ │ │ │ ├── data-stores │ │ │ │ │ └── mysql │ │ │ │ │ │ ├── vars.tf │ │ │ │ │ │ ├── outputs.tf │ │ │ │ │ │ ├── backend.tf │ │ │ │ │ │ ├── main.tf │ │ │ │ │ │ └── README.md │ │ │ │ └── services │ │ │ │ │ └── webserver-cluster │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── stage │ │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ └── modules │ │ │ └── services │ │ │ └── webserver-cluster │ │ │ ├── user-data-new.sh │ │ │ ├── user-data.sh │ │ │ ├── outputs.tf │ │ │ ├── README.md │ │ │ └── vars.tf │ ├── 13 │ │ ├── outputs.tf │ │ ├── vars.tf │ │ ├── main.tf │ │ └── README.md │ ├── 14 │ │ ├── live │ │ │ ├── global │ │ │ │ └── s3 │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ ├── prod │ │ │ │ ├── data-stores │ │ │ │ │ └── mysql │ │ │ │ │ │ ├── vars.tf │ │ │ │ │ │ ├── outputs.tf │ │ │ │ │ │ ├── backend.tf │ │ │ │ │ │ ├── main.tf │ │ │ │ │ │ └── README.md │ │ │ │ └── services │ │ │ │ │ └── webserver-cluster │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ └── stage │ │ │ │ ├── data-stores │ │ │ │ └── mysql │ │ │ │ │ ├── vars.tf │ │ │ │ │ ├── outputs.tf │ │ │ │ │ ├── backend.tf │ │ │ │ │ ├── main.tf │ │ │ │ │ └── README.md │ │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── outputs.tf │ │ │ │ ├── backend.tf │ │ │ │ ├── vars.tf │ │ │ │ ├── main.tf │ │ │ │ └── README.md │ │ ├── modules │ │ │ └── services │ │ │ │ └── webserver-cluster │ │ │ │ ├── user-data.sh │ │ │ │ ├── outputs.tf │ │ │ │ ├── vars.tf │ │ │ │ └── README.md │ │ └── README.md │ └── labs │ │ └── AWS_Example_Labs │ ├── TerraformBlocks.md │ ├── TerraformStateFile.md │ └── TerraformVariables.md └── 2024Terraform_Introduction.md /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/7/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "ap-south-1" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/11/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: ARN 2 | output "neo_arn" { 3 | value = ["${aws_iam_user.example.*.arn}"] 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/13/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: ARN 2 | output "neo_arn" { 3 | value = ["${aws_iam_user.example.*.arn}"] 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/5/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${aws_elb.example.dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/4/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: Public IP address 2 | output "public_ip" { 3 | value = "${aws_instance.example.public_ip}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/6/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/global/s3/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/global/s3/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/global/s3/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/global/s3/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/global/s3/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: S3 bucket 2 | output "s3_bucket_arn" { 3 | value = "${aws_s3_bucket.terraform_state.arn}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/data-stores/mysql/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB password 2 | variable "db_password" { 3 | description = "The password for the database" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${aws_elb.example.dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/modules/services/webserver-cluster/user-data-new.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Hello, World, v2" > index.html 4 | nohup busybox httpd -f -p "${server_port}" & 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/4/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/5/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${module.webserver_cluster.elb_dns_name}" 4 | } 5 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/6/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/global/s3/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/global/s3/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/11/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: user names 2 | variable "user_names" { 3 | description = "Create IAM users with these names" 4 | type = "list" 5 | default = ["neo", "trinity", "morpheus"] 6 | } 7 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/global/s3/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/global/s3/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/global/s3/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: S3 bucket name 2 | variable "bucket_name" { 3 | description = "The name of the S3 bucket. Must be globally unique." 4 | default = "terraform-state-my-bucket" 5 | } 6 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/user-data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cat > index.html <Hello, World 4 |

DB address: ${db_address}

5 |

DB port: ${db_port}

6 | EOF 7 | nohup busybox httpd -f -p "${server_port}" & 8 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/1/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "ap-south-1" 4 | } 5 | 6 | # Create an EC2 instance 7 | resource "aws_instance" "mywebserver" { 8 | ami = "ami-xxx" 9 | instance_type = "t2.micro" 10 | } 11 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/modules/services/webserver-cluster/user-data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat > index.html <Hello, World 5 |

DB address: ${db_address}

6 |

DB port: ${db_port}

7 | EOF 8 | 9 | nohup busybox httpd -f -p "${server_port}" & 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/modules/services/webserver-cluster/user-data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat > index.html <Hello, World 5 |

DB address: ${db_address}

6 |

DB port: ${db_port}

7 | EOF 8 | 9 | nohup busybox httpd -f -p "${server_port}" & 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/modules/services/webserver-cluster/user-data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat > index.html <${server_text} 5 |

DB address: ${db_address}

6 |

DB port: ${db_port}

7 | EOF 8 | 9 | nohup busybox httpd -f -p "${server_port}" & 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/7/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "terraform-state/terraform.tfstate" 6 | region = "ap-south-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/data-stores/mysql/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DB instance address 2 | output "address" { 3 | value = "${aws_db_instance.example.address}" 4 | } 5 | 6 | # Output variable: DB instance port 7 | output "port" { 8 | value = "${aws_db_instance.example.port}" 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "file-layout/stage/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "module-example/prod/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "module-example/stage/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/2/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create an EC2 instance 7 | resource "aws_instance" "example" { 8 | ami = "ami-785db401" 9 | instance_type = "t2.micro" 10 | 11 | tags { 12 | Name = "terraform-example" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "if-statements-example/prod/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "multi-repo-example/live/prod/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "multi-repo-example/live/stage/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "if-statements-example/stage/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "module-example/prod/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "module-example/stage/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "file-layout-example/stage/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "if-statements-example/prod/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "if-statements-example/stage/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "zero-downtime-deployment-example/live/prod/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/data-stores/mysql/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "zero-downtime-deployment-example/live/stage/data-stores/mysql/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "multi-repo-example/live/stage/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | 2 | # Define Terraform backend using a S3 bucket for storing the Terraform state 3 | terraform { 4 | backend "s3" { 5 | bucket = "terraform-state-my-bucket" 6 | key = "multi-repo-example/live/prod/services/webserver-cluster/terraform.tfstate" 7 | region = "eu-west-1" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "zero-downtime-deployment-example/live/prod/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/6/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/services/webserver-cluster/backend.tf: -------------------------------------------------------------------------------- 1 | # Define Terraform backend using a S3 bucket for storing the Terraform state 2 | terraform { 3 | backend "s3" { 4 | bucket = "terraform-state-my-bucket" 5 | key = "zero-downtime-deployment-example/live/stage/services/webserver-cluster/terraform.tfstate" 6 | region = "eu-west-1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/global/s3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/global/s3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/global/s3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/global/s3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/global/s3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a S3 bucket 7 | resource "aws_s3_bucket" "terraform_state" { 8 | bucket = "${var.bucket_name}" 9 | 10 | versioning { 11 | enabled = true 12 | } 13 | 14 | lifecycle { 15 | prevent_destroy = true 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/13/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: user names 2 | variable "user_names" { 3 | description = "Create IAM users with these names" 4 | type = "list" 5 | default = ["neo", "trinity", "morpheus"] 6 | } 7 | 8 | # Input variable: give neo cloudwatch full access 9 | variable "give_neo_cloudwatch_full_access" { 10 | description = "If true, neo gets full access to CloudWatch" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/modules/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${aws_elb.example.dns_name}" 4 | } 5 | 6 | # Output variable: Auto Scaling Group name 7 | output "asg_name" { 8 | value = "${aws_autoscaling_group.example.name}" 9 | } 10 | 11 | # Output variable: ELB Security Group Id 12 | output "elb_security_group_id" { 13 | value = "${aws_security_group.elb.id}" 14 | } -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/modules/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${aws_elb.example.dns_name}" 4 | } 5 | 6 | # Output variable: Auto Scaling Group name 7 | output "asg_name" { 8 | value = "${aws_autoscaling_group.example.name}" 9 | } 10 | 11 | # Output variable: ELB Security Group Id 12 | output "elb_security_group_id" { 13 | value = "${aws_security_group.elb.id}" 14 | } 15 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/modules/services/webserver-cluster/outputs.tf: -------------------------------------------------------------------------------- 1 | # Output variable: DNS Name of ELB 2 | output "elb_dns_name" { 3 | value = "${aws_elb.example.dns_name}" 4 | } 5 | 6 | # Output variable: Auto Scaling Group name 7 | output "asg_name" { 8 | value = "${aws_autoscaling_group.example.name}" 9 | } 10 | 11 | # Output variable: ELB Security Group Id 12 | output "elb_security_group_id" { 13 | value = "${aws_security_group.elb.id}" 14 | } 15 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_prod" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_stage" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_prod" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_stage" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_prod" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_stage" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_prod" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/data-stores/mysql/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a DB instance 7 | resource "aws_db_instance" "example" { 8 | engine = "mysql" 9 | allocated_storage = 10 10 | instance_class = "db.t2.micro" 11 | name = "example_database_stage" 12 | username = "admin" 13 | password = "${var.db_password}" 14 | skip_final_snapshot = true 15 | } 16 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-my-bucket" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "module-example/prod/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-my-bucket" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "module-example/stage/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-afb" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "if-statements-example/prod/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-my-bucket" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "multi-repo-example/live/prod/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-my-bucket" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "if-statements-example/stage/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-my-bucket" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "multi-repo-example/live/stage/data-stores/mysql/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-afb" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "zero-downtime-deployment-example/live/prod/services/webserver-cluster/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: DB remote state bucket name 2 | variable "db_remote_state_bucket" { 3 | description = "The name of the S3 bucket for the database's remote state" 4 | default = "terraform-state-afb" 5 | } 6 | 7 | # Input variable: DB remote state bucket key 8 | variable "db_remote_state_key" { 9 | description = "The path for database's remote state in S3" 10 | default = "zero-downtime-deployment-example/live/stage/services/webserver-cluster/terraform.tfstate" 11 | } 12 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../../modules/services/webserver-cluster" 9 | 10 | cluster_name = "werservers-prod" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 10 17 | enable_autoscaling = true 18 | enable_new_user_data = false 19 | } 20 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../../modules/services/webserver-cluster" 9 | 10 | ami = "ami-785db401" 11 | server_text = "Hello, World" 12 | 13 | cluster_name = "werservers-prod" 14 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 15 | db_remote_state_key = "${var.db_remote_state_key}" 16 | 17 | instance_type = "t2.micro" 18 | min_size = 2 19 | max_size = 10 20 | enable_autoscaling = true 21 | } 22 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | 7 | # Input variable: DB remote state bucket name 8 | variable "db_remote_state_bucket" { 9 | description = "The name of the S3 bucket used for the database's remote state storage" 10 | default = "terraform-state-my-bucket" 11 | } 12 | 13 | # Input variable: DB remote state bucket key 14 | variable "db_remote_state_key" { 15 | description = "The name of the key in the S3 bucket used for the database's remote state storage" 16 | default = "file-layout-example/stage/data-stores/mysql/terraform.tfstate" 17 | } 18 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../modules/services/webserver-cluster" 9 | 10 | cluster_name = "werservers-stage" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 2 17 | } 18 | 19 | # Create a Security Group Rule 20 | resource "aws_security_group_rule" "allow_testing_inbound" { 21 | type = "ingress" 22 | security_group_id = "${module.webserver_cluster.elb_security_group_id}" 23 | 24 | from_port = 12345 25 | to_port = 12345 26 | protocol = "tcp" 27 | cidr_blocks = ["0.0.0.0/0"] 28 | } 29 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/4/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Create a Security Group for an EC2 instance 7 | resource "aws_security_group" "instance" { 8 | name = "terraform-example-instance" 9 | 10 | ingress { 11 | from_port = "${var.server_port}" 12 | to_port = "${var.server_port}" 13 | protocol = "tcp" 14 | cidr_blocks = ["0.0.0.0/0"] 15 | } 16 | } 17 | 18 | # Create an EC2 instance 19 | resource "aws_instance" "example" { 20 | ami = "ami-785db401" 21 | instance_type = "t2.micro" 22 | vpc_security_group_ids = ["${aws_security_group.instance.id}"] 23 | 24 | user_data = <<-EOF 25 | #!/bin/bash 26 | echo "Hello, World" > index.html 27 | nohup busybox httpd -f -p "${var.server_port}" & 28 | EOF 29 | 30 | tags { 31 | Name = "terraform-example" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "git::git@github.com:alfonsof/terraform-aws-repo-examples.git//modules/services/webserver-cluster?ref=v0.0.2" 9 | 10 | cluster_name = "werservers-stage" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 2 17 | } 18 | 19 | # Create a Security Group Rule 20 | resource "aws_security_group_rule" "allow_testing_inbound" { 21 | type = "ingress" 22 | security_group_id = "${module.webserver_cluster.elb_security_group_id}" 23 | 24 | from_port = 12345 25 | to_port = 12345 26 | protocol = "tcp" 27 | cidr_blocks = ["0.0.0.0/0"] 28 | } 29 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../../modules/services/webserver-cluster" 9 | 10 | cluster_name = "werservers-stage" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 2 17 | enable_autoscaling = false 18 | enable_new_user_data = true 19 | } 20 | 21 | # Create a Security Group Rule 22 | resource "aws_security_group_rule" "allow_testing_inbound" { 23 | type = "ingress" 24 | security_group_id = "${module.webserver_cluster.elb_security_group_id}" 25 | 26 | from_port = 12345 27 | to_port = 12345 28 | protocol = "tcp" 29 | cidr_blocks = ["0.0.0.0/0"] 30 | } 31 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/11/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | 5 | } 6 | 7 | # Create an IAM user 8 | resource "aws_iam_user" "example" { 9 | count = "${length(var.user_names)}" 10 | name = "${element(var.user_names, count.index)}" 11 | 12 | } 13 | 14 | # Data source: IAM policy document 15 | data "aws_iam_policy_document" "ec2_read_only" { 16 | statement { 17 | effect = "Allow" 18 | actions = ["ec2:Describe*"] 19 | resources = ["*"] 20 | } 21 | } 22 | 23 | # Create an IAM policy 24 | resource "aws_iam_policy" "ec2_read_only" { 25 | name = "ec2-read-only" 26 | policy = "${data.aws_iam_policy_document.ec2_read_only.json}" 27 | } 28 | 29 | # Create an IAM user policy attachement 30 | resource "aws_iam_user_policy_attachment" "ec2_access" { 31 | count = "${length(var.user_names)}" 32 | user = "${element(aws_iam_user.example.*.name, count.index)}" 33 | policy_arn = "${aws_iam_policy.ec2_read_only.arn}" 34 | } 35 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/3/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "ap-south-1" 4 | } 5 | 6 | # Create a Security Group for an EC2 instance 7 | resource "aws_security_group" "instance" { 8 | name = "terraform-example-instance" 9 | 10 | ingress { 11 | from_port = 8080 12 | to_port = 8080 13 | protocol = "tcp" 14 | cidr_blocks = ["0.0.0.0/0"] 15 | } 16 | } 17 | 18 | # Create an EC2 instance 19 | resource "aws_instance" "example" { 20 | ami = "ami-785db401" 21 | instance_type = "t2.micro" 22 | vpc_security_group_ids = ["${aws_security_group.instance.id}"] 23 | 24 | user_data = <<-EOF 25 | #!/bin/bash 26 | echo "Hello, World" > index.html 27 | nohup busybox httpd -f -p 8080 & 28 | EOF 29 | 30 | tags { 31 | Name = "terraform-example" 32 | } 33 | } 34 | 35 | # Output variable: Public IP address 36 | output "public_ip" { 37 | value = "${aws_instance.example.public_ip}" 38 | } 39 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../../modules/services/webserver-cluster" 9 | 10 | ami = "ami-785db401" 11 | server_text = "New server text" 12 | 13 | cluster_name = "werservers-stage" 14 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 15 | db_remote_state_key = "${var.db_remote_state_key}" 16 | 17 | instance_type = "t2.micro" 18 | min_size = 2 19 | max_size = 2 20 | enable_autoscaling = false 21 | } 22 | 23 | # Create a Security Group Rule 24 | resource "aws_security_group_rule" "allow_testing_inbound" { 25 | type = "ingress" 26 | security_group_id = "${module.webserver_cluster.elb_security_group_id}" 27 | 28 | from_port = 12345 29 | to_port = 12345 30 | protocol = "tcp" 31 | cidr_blocks = ["0.0.0.0/0"] 32 | } 33 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/modules/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | 7 | # Input variable: Cluster name 8 | variable "cluster_name" { 9 | description = "The name to use for all the cluster resources" 10 | } 11 | 12 | # Input variable: DB remote state bucket name 13 | variable "db_remote_state_bucket" { 14 | description = "The name of the S3 bucket for the database's remote state" 15 | } 16 | 17 | # Input variable: DB remote state bucket key 18 | variable "db_remote_state_key" { 19 | description = "The path for database's remote state in S3" 20 | } 21 | 22 | # Input variable: Instance type 23 | variable "instance_type" { 24 | description = "The type of EC2 Instances to run (e.g. t2.micro)" 25 | } 26 | 27 | # Input variable: Instance min size 28 | variable "min_size" { 29 | description = "The minimum number of EC2 Instances in the ASG" 30 | } 31 | 32 | # Input variable: Instance max size 33 | variable "max_size" { 34 | description = "The maximum number of EC2 Instances in the ASG" 35 | } 36 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/modules/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster module example 2 | 3 | This folder contains a Web Server Cluster module example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file define a cluster of web servers module on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | 16 | ## Using the code 17 | 18 | Terraform modules are not meant to be deployed directly. Instead, you should be using them from other templates. See 19 | [stage/services/webserver-cluster](../../../stage/services-webserver-cluster) and 20 | [prod/services/webserver-cluster](../../../prod/services-webserver-cluster) for examples. -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/modules/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster module example 2 | 3 | This folder contains a Web Server Cluster module example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file define a cluster of web servers module on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | 16 | ## Using the code 17 | 18 | Terraform modules are not meant to be deployed directly. Instead, you should be using them from other templates. See [live/stage/services/webserver-cluster](../../../live/stage/services/webserver-cluster) and 19 | [live/prod/services/webserver-cluster](../../../live/prod/services/webserver-cluster) for examples. -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "../../../modules/services/webserver-cluster" 9 | 10 | cluster_name = "werservers-prod" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 10 17 | } 18 | 19 | # Create an Autoscaling Schedule 20 | resource "aws_autoscaling_schedule" "scale_out_during_business_hours" { 21 | scheduled_action_name = "scale-out-during-business-hours" 22 | min_size = 2 23 | max_size = 10 24 | desired_capacity = 10 25 | recurrence = "0 9 * * *" 26 | 27 | autoscaling_group_name = "${module.webserver_cluster.asg_name}" 28 | } 29 | 30 | # Create an Autoscaling Schedule 31 | resource "aws_autoscaling_schedule" "scale_in_at_night" { 32 | scheduled_action_name = "scale-in-at-night" 33 | min_size = 2 34 | max_size = 10 35 | desired_capacity = 2 36 | recurrence = "0 17 * * *" 37 | 38 | autoscaling_group_name = "${module.webserver_cluster.asg_name}" 39 | } 40 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Use Module 7 | module "webserver_cluster" { 8 | source = "git::git@github.com:alfonsof/terraform-aws-repo-examples.git//modules/services/webserver-cluster?ref=v0.0.1" 9 | 10 | cluster_name = "werservers-prod" 11 | db_remote_state_bucket = "${var.db_remote_state_bucket}" 12 | db_remote_state_key = "${var.db_remote_state_key}" 13 | 14 | instance_type = "t2.micro" 15 | min_size = 2 16 | max_size = 10 17 | } 18 | 19 | # Create an Autoscaling Schedule 20 | resource "aws_autoscaling_schedule" "scale_out_during_business_hours" { 21 | scheduled_action_name = "scale-out-during-business-hours" 22 | min_size = 2 23 | max_size = 10 24 | desired_capacity = 10 25 | recurrence = "0 9 * * *" 26 | 27 | autoscaling_group_name = "${module.webserver_cluster.asg_name}" 28 | } 29 | 30 | # Create an Autoscaling Schedule 31 | resource "aws_autoscaling_schedule" "scale_in_at_night" { 32 | scheduled_action_name = "scale-in-at-night" 33 | min_size = 2 34 | max_size = 10 35 | desired_capacity = 2 36 | recurrence = "0 17 * * *" 37 | 38 | autoscaling_group_name = "${module.webserver_cluster.asg_name}" 39 | } 40 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/modules/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | 7 | # Input variable: Cluster name 8 | variable "cluster_name" { 9 | description = "The name to use for all the cluster resources" 10 | } 11 | 12 | # Input variable: DB remote state bucket name 13 | variable "db_remote_state_bucket" { 14 | description = "The name of the S3 bucket for the database's remote state" 15 | } 16 | 17 | # Input variable: DB remote state bucket key 18 | variable "db_remote_state_key" { 19 | description = "The path for database's remote state in S3" 20 | } 21 | 22 | # Input variable: Instance type 23 | variable "instance_type" { 24 | description = "The type of EC2 Instances to run (e.g. t2.micro)" 25 | } 26 | 27 | # Input variable: Instance min size 28 | variable "min_size" { 29 | description = "The minimum number of EC2 Instances in the ASG" 30 | } 31 | 32 | # Input variable: Instance max size 33 | variable "max_size" { 34 | description = "The maximum number of EC2 Instances in the ASG" 35 | } 36 | 37 | # Input variable: Enable Autoscaling 38 | variable "enable_autoscaling" { 39 | description = "If set to true, enable autoscaling" 40 | 41 | } 42 | 43 | # Input variable: Enable new user data 44 | variable "enable_new_user_data" { 45 | description = "If set to true, use the new User Data script" 46 | } 47 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/modules/services/webserver-cluster/vars.tf: -------------------------------------------------------------------------------- 1 | # Input variable: server port 2 | variable "server_port" { 3 | description = "The port the server will use for HTTP requests" 4 | default = "8080" 5 | } 6 | 7 | # Input variable: Cluster name 8 | variable "cluster_name" { 9 | description = "The name to use for all the cluster resources" 10 | } 11 | 12 | # Input variable: DB remote state bucket name 13 | variable "db_remote_state_bucket" { 14 | description = "The name of the S3 bucket for the database's remote state" 15 | } 16 | 17 | # Input variable: DB remote state bucket key 18 | variable "db_remote_state_key" { 19 | description = "The path for database's remote state in S3" 20 | } 21 | 22 | # Input variable: Instance type 23 | variable "instance_type" { 24 | description = "The type of EC2 Instances to run (e.g. t2.micro)" 25 | } 26 | 27 | # Input variable: Instance min size 28 | variable "min_size" { 29 | description = "The minimum number of EC2 Instances in the ASG" 30 | } 31 | 32 | # Input variable: Instance max size 33 | variable "max_size" { 34 | description = "The maximum number of EC2 Instances in the ASG" 35 | } 36 | 37 | # Input variable: Enable Autoscaling 38 | variable "enable_autoscaling" { 39 | description = "If set to true, enable autoscaling" 40 | } 41 | 42 | # Input variable: AMI 43 | variable "ami" { 44 | description = "The AMI to run in the cluster" 45 | default = "ami-785db401" 46 | } 47 | 48 | # Input variable: Web server text 49 | variable "server_text" { 50 | description = "The text the web server should return" 51 | default = "Hello, World" 52 | } 53 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS 2 | 3 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 4 | 5 | ## Using the code 6 | 7 | * Configure your AWS access keys. 8 | 9 | * Initialize working directory. 10 | 11 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 12 | 13 | ```bash 14 | terraform init 15 | ``` 16 | 17 | * Configure Terraform backend. 18 | 19 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 20 | 21 | ```bash 22 | bucket = "" 23 | ``` 24 | 25 | * Configure database password. 26 | 27 | The database password is managed by the `db_password` input variable. 28 | 29 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 30 | 31 | ```bash 32 | TF_VAR_db_password= 33 | ``` 34 | 35 | * Validate the changes. 36 | 37 | Run command: 38 | 39 | ```bash 40 | terraform plan 41 | ``` 42 | 43 | * Deploy the changes. 44 | 45 | Run command: 46 | 47 | ```bash 48 | terraform apply 49 | ``` 50 | 51 | * Test the deploy. 52 | 53 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 54 | 55 | * Clean up the resources created. 56 | 57 | When you have finished, run command: 58 | 59 | ```bash 60 | terraform destroy 61 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS 2 | 3 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 4 | 5 | 6 | 7 | ## Using the code 8 | 9 | * Configure your AWS access keys. 10 | 11 | * Initialize working directory. 12 | 13 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 14 | 15 | ```bash 16 | terraform init 17 | ``` 18 | 19 | * Configure Terraform backend. 20 | 21 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 22 | 23 | ```bash 24 | bucket = "" 25 | ``` 26 | 27 | * Configure database password. 28 | 29 | The database password is managed by the `db_password` input variable. 30 | 31 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 32 | 33 | ```bash 34 | TF_VAR_db_password= 35 | ``` 36 | 37 | * Validate the changes. 38 | 39 | Run command: 40 | 41 | ```bash 42 | terraform plan 43 | ``` 44 | 45 | * Deploy the changes. 46 | 47 | Run command: 48 | 49 | ```bash 50 | terraform apply 51 | ``` 52 | 53 | * Test the deploy. 54 | 55 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 56 | 57 | * Clean up the resources created. 58 | 59 | When you have finished, run command: 60 | 61 | ```bash 62 | terraform destroy 63 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/modules/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster module example 2 | 3 | This folder contains a Web Server Cluster module example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file define a cluster of web servers module on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | 16 | ## Using the code 17 | 18 | Terraform modules are not meant to be deployed directly. Instead, you should be using them from other templates. See [live/stage/services/webserver-cluster](../../../live/stage/services-webserver-cluster) and [live/prod/services/webserver-cluster](../../../live/prod/services-webserver-cluster) for examples. 19 | 20 | * Input variables than manage AMI and text the User Data script return. 21 | 22 | There are two input variables defined in `vars.tf` file in this Module in order to manage AMI and text the User Data script return: 23 | 24 | ```hcl 25 | variable "ami" { 26 | description = "The AMI to run in the cluster" 27 | default = "ami-785db401" 28 | } 29 | 30 | variable "server_text" { 31 | description = "The text the web server should return" 32 | default = "Hello, World" 33 | } 34 | ``` 35 | 36 | These are used by the templates in [live/stage/services/webserver-cluster](../../../live/stage/services-webserver-cluster) and [live/prod/services/webserver-cluster](../../../live/prod/services-webserver-cluster). -------------------------------------------------------------------------------- /2024/1_Example.md: -------------------------------------------------------------------------------- 1 | Here is the Terraform code split into two parts: one for creating the Terraform provider block and the other for creating the EC2 instance. 2 | 3 | ### Part 1: Create Terraform Provider Block 4 | 5 | This part sets up the provider configuration. 6 | 7 | ```hcl 8 | # terraform-provider.tf 9 | 10 | # Terraform Settings Block 11 | terraform { 12 | required_providers { 13 | aws = { 14 | source = "hashicorp/aws" 15 | #version = "~> 3.21" # Optional but recommended in production 16 | } 17 | } 18 | } 19 | 20 | # Provider Block 21 | provider "aws" { 22 | profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials 23 | region = "ap-south-1" 24 | } 25 | ``` 26 | 27 | ### Part 2: Create EC2 Instance 28 | 29 | This part defines the resource configuration for the EC2 instance. 30 | 31 | ```hcl 32 | # ec2-instance.tf 33 | 34 | # Resource Block 35 | resource "aws_instance" "ec2demo" { 36 | ami = "ami-06f621d90fa29f6d0" # Amazon Linux in us-east-1, update as per your region 37 | instance_type = "t2.micro" 38 | } 39 | ``` 40 | 41 | ### Directory Structure 42 | 43 | To keep your Terraform project organized, you can structure your directory as follows: 44 | 45 | ``` 46 | terraform_project/ 47 | ├── terraform-provider.tf 48 | └── ec2-instance.tf 49 | ``` 50 | 51 | ### How to Use 52 | 53 | 1. **Initialize the Configuration:** 54 | Navigate to the directory containing your `.tf` files and run: 55 | ```sh 56 | terraform init 57 | ``` 58 | 59 | 2. **Plan the Configuration:** 60 | Preview the changes Terraform will make: 61 | ```sh 62 | terraform plan 63 | ``` 64 | 65 | 3. **Apply the Configuration:** 66 | Apply the changes to create the EC2 instance: 67 | ```sh 68 | terraform apply 69 | ``` 70 | 71 | By separating the provider configuration from the resource definition, you maintain a cleaner and more modular codebase, which can be particularly useful in larger projects. 72 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster 2 | 3 | This Terraform file deploys a cluster of web servers in Amazon Web Services (AWS) using EC2 and Auto Scaling, and a load balancer using ELB. 4 | 5 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 6 | 7 | ## Using the code 8 | 9 | * Configure your AWS access keys. 10 | 11 | * Initialize working directory. 12 | 13 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 14 | 15 | ```bash 16 | terraform init 17 | ``` 18 | 19 | * Configure Terraform backend. 20 | 21 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 22 | 23 | ```bash 24 | bucket = "" 25 | ``` 26 | 27 | * Configure the bucket used for the database's remote state storage. 28 | 29 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 30 | 31 | ```hcl 32 | variable "db_remote_state_bucket" { 33 | description = "The name of the S3 bucket used for the database's remote state storage" 34 | default = "" 35 | } 36 | ``` 37 | 38 | * Validate the changes. 39 | 40 | Run command: 41 | 42 | ```bash 43 | terraform plan 44 | ``` 45 | 46 | * Deploy the changes. 47 | 48 | Run command: 49 | 50 | ```bash 51 | terraform apply 52 | ``` 53 | 54 | * Test the cluster of web servers. 55 | 56 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 57 | 58 | ```bash 59 | curl http:/// 60 | ``` 61 | 62 | * Clean up the resources created. 63 | 64 | When you have finished, run command: 65 | 66 | ```bash 67 | terraform destroy 68 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/stage/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example 2 | 3 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 4 | 5 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 6 | 7 | 8 | 9 | ## Using the code 10 | 11 | * Configure your AWS access keys. 12 | 13 | * Initialize working directory. 14 | 15 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 16 | 17 | ```bash 18 | terraform init 19 | ``` 20 | 21 | * Configure Terraform backend. 22 | 23 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 24 | 25 | ```bash 26 | bucket = "" 27 | ``` 28 | 29 | * Configure the bucket used for the database's remote state storage. 30 | 31 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 32 | 33 | ```hcl 34 | variable "db_remote_state_bucket" { 35 | description = "The name of the S3 bucket used for the database's remote state storage" 36 | default = "" 37 | } 38 | ``` 39 | 40 | * Validate the changes. 41 | 42 | Run command: 43 | 44 | ```bash 45 | terraform plan 46 | ``` 47 | 48 | * Deploy the changes. 49 | 50 | Run command: 51 | 52 | ```bash 53 | terraform apply 54 | ``` 55 | 56 | * Test the cluster of web servers. 57 | 58 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 59 | 60 | ```bash 61 | curl http:/// 62 | ``` 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (production environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (production environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (staging environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (production environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (staging environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (production environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services). 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/data-stores/mysql/README.md: -------------------------------------------------------------------------------- 1 | # Terraform MySQL on RDS example (staging environment) 2 | 3 | This folder contains a MySQL on RDS example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a MySQL database using RDS on AWS (Amazon Web Services) 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Configure Terraform backend. 27 | 28 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 29 | 30 | ```bash 31 | bucket = "" 32 | ``` 33 | 34 | * Configure database password. 35 | 36 | The database password is managed by the `db_password` input variable. 37 | 38 | Terraform will parse any environment variables that are prefixed with `TF_VAR`. You must create an environment variable `TF_VAR_db_password`: 39 | 40 | ```bash 41 | TF_VAR_db_password= 42 | ``` 43 | 44 | * Validate the changes. 45 | 46 | Run command: 47 | 48 | ```bash 49 | terraform plan 50 | ``` 51 | 52 | * Deploy the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform apply 58 | ``` 59 | 60 | * Test the deploy. 61 | 62 | You should see a new RDS MySQL database on AWS (Amazon Web Services). 63 | 64 | * Clean up the resources created. 65 | 66 | When you have finished, run command: 67 | 68 | ```bash 69 | terraform destroy 70 | ``` -------------------------------------------------------------------------------- /2024/2_Example.md: -------------------------------------------------------------------------------- 1 | You can split the code into multiple files to organize your configuration better. Here, I'll show you how to create the necessary files to set up the backend for storing the Terraform state file in S3 and to define the provider and the EC2 instance. 2 | 3 | ### Directory Structure 4 | 5 | ``` 6 | terraform_project/ 7 | ├── backend.tf 8 | ├── provider.tf 9 | └── ec2-instance.tf 10 | ``` 11 | 12 | ### File: backend.tf 13 | 14 | This file configures the Terraform backend to use S3 for remote state storage. 15 | 16 | ```hcl 17 | # backend.tf 18 | 19 | terraform { 20 | required_version = ">= 1.4" 21 | required_providers { 22 | aws = { 23 | source = "hashicorp/aws" 24 | version = "~> 4.0" 25 | } 26 | } 27 | # Adding Backend as S3 for Remote State Storage 28 | backend "s3" { 29 | bucket = "kumarnewbucket" 30 | key = "test/terraform.tfstate" 31 | region = "ap-south-1" 32 | } 33 | } 34 | ``` 35 | 36 | ### File: provider.tf 37 | 38 | This file configures the AWS provider. 39 | 40 | ```hcl 41 | # provider.tf 42 | 43 | # Provider Block 44 | provider "aws" { 45 | region = "ap-south-1" 46 | profile = "default" 47 | } 48 | ``` 49 | 50 | ### File: ec2-instance.tf 51 | 52 | This file defines the EC2 instance resource. 53 | 54 | ```hcl 55 | # ec2-instance.tf 56 | 57 | # Resource Block 58 | resource "aws_instance" "ec2demo" { 59 | ami = "ami-06f621d90fa29f6d0" # Amazon Linux in us-east-1, update as per your region 60 | instance_type = "t2.micro" 61 | } 62 | ``` 63 | 64 | ### How to Use 65 | 66 | 1. **Initialize the Configuration:** 67 | Navigate to the directory containing your `.tf` files and run: 68 | ```sh 69 | terraform init 70 | ``` 71 | 72 | The `terraform init` command initializes the backend configuration and downloads the required providers. 73 | 74 | 2. **Plan the Configuration:** 75 | Preview the changes Terraform will make: 76 | ```sh 77 | terraform plan 78 | ``` 79 | 80 | 3. **Apply the Configuration:** 81 | Apply the changes to create the EC2 instance: 82 | ```sh 83 | terraform apply 84 | ``` 85 | 86 | By splitting your configuration into multiple files, you enhance readability and maintainability. The `backend.tf` file sets up remote state storage, the `provider.tf` file configures the AWS provider, and the `ec2-instance.tf` file defines the EC2 instance resource. 87 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/1/README.md: -------------------------------------------------------------------------------- 1 | ## Using the code 2 | 3 | * Configure your AWS access keys. 4 | 5 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 6 | 7 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 8 | 9 | * The default credentials file 10 | 11 | Set credentials in the AWS credentials profile file on your local system, located at: 12 | 13 | `~/.aws/credentials` on Linux, macOS, or Unix 14 | 15 | `C:\Users\USERNAME\.aws\credentials` on Windows 16 | 17 | This file should contain lines in the following format: 18 | 19 | ```bash 20 | [default] 21 | aws_access_key_id = 22 | aws_secret_access_key = 23 | ``` 24 | Substitute your own AWS credentials values for the values `` and ``. 25 | 26 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 27 | 28 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 29 | 30 | To set these variables on Linux, macOS, or Unix, use `export`: 31 | 32 | ```bash 33 | export AWS_ACCESS_KEY_ID= 34 | export AWS_SECRET_ACCESS_KEY= 35 | ``` 36 | 37 | To set these variables on Windows, use `set`: 38 | 39 | ```bash 40 | set AWS_ACCESS_KEY_ID= 41 | set AWS_SECRET_ACCESS_KEY= 42 | ``` 43 | 44 | * Initialize working directory. 45 | 46 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 47 | 48 | ```bash 49 | terraform init 50 | ``` 51 | 52 | * Validate the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform plan 58 | ``` 59 | 60 | * Deploy the changes. 61 | 62 | Run command: 63 | 64 | ```bash 65 | terraform apply 66 | ``` 67 | 68 | * Test the deploy. 69 | 70 | When the `terraform apply` command completes, use the AWS console, you should see the new EC2 instance created. 71 | 72 | * Clean up the resources created. 73 | 74 | When you have finished, run command: 75 | 76 | ```bash 77 | terraform destroy 78 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/global/s3/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Remote State 2 | 3 | This Terraform file deploys the creation of a bucket in AWS (Amazon Web Services) using S3. 4 | 5 | ## Using the code 6 | 7 | * Configure your AWS access keys. 8 | 9 | * Initialize working directory. 10 | 11 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 12 | 13 | ```bash 14 | terraform init 15 | ``` 16 | 17 | * Modify configuration. 18 | 19 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 20 | 21 | You can modify the S3 bucket name in several ways: 22 | 23 | * Loading variables from command line flags. 24 | 25 | Run Terraform commands in this way: 26 | 27 | ```bash 28 | terraform plan -var 'bucket_name=' 29 | ``` 30 | 31 | ```bash 32 | terraform apply -var 'bucket_name=' 33 | ``` 34 | 35 | * Loading variables from a file. 36 | 37 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 38 | 39 | ```bash 40 | bucket_name = "" 41 | ``` 42 | 43 | * Loading variables from environment variables. 44 | 45 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 46 | 47 | ```bash 48 | TF_VAR_bucket_name= 49 | ``` 50 | 51 | * Variable defaults. 52 | 53 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 54 | 55 | ```hcl 56 | variable "bucket_name" { 57 | description = "The name of the S3 bucket. Must be globally unique." 58 | default = "" 59 | } 60 | ``` 61 | 62 | * Validate the changes. 63 | 64 | Run command: 65 | 66 | ```bash 67 | terraform plan 68 | ``` 69 | 70 | * Deploy the changes. 71 | 72 | Run command: 73 | 74 | ```bash 75 | terraform apply 76 | ``` 77 | 78 | * Clean up the resources created. 79 | 80 | When you have finished, run command: 81 | 82 | ```bash 83 | terraform destroy 84 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/2/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Using the code 4 | 5 | * Configure your AWS access keys. 6 | 7 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 8 | 9 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 10 | 11 | * The default credentials file 12 | 13 | Set credentials in the AWS credentials profile file on your local system, located at: 14 | 15 | `~/.aws/credentials` on Linux, macOS, or Unix 16 | 17 | `C:\Users\USERNAME\.aws\credentials` on Windows 18 | 19 | This file should contain lines in the following format: 20 | 21 | ```bash 22 | [default] 23 | aws_access_key_id = 24 | aws_secret_access_key = 25 | ``` 26 | Substitute your own AWS credentials values for the values `` and ``. 27 | 28 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 29 | 30 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 31 | 32 | To set these variables on Linux, macOS, or Unix, use `export`: 33 | 34 | ```bash 35 | export AWS_ACCESS_KEY_ID= 36 | export AWS_SECRET_ACCESS_KEY= 37 | ``` 38 | 39 | To set these variables on Windows, use `set`: 40 | 41 | ```bash 42 | set AWS_ACCESS_KEY_ID= 43 | set AWS_SECRET_ACCESS_KEY= 44 | ``` 45 | 46 | * Initialize working directory. 47 | 48 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 49 | 50 | ```bash 51 | terraform init 52 | ``` 53 | 54 | * Validate the changes. 55 | 56 | Run command: 57 | 58 | ```bash 59 | terraform plan 60 | ``` 61 | 62 | * Deploy the changes. 63 | 64 | Run command: 65 | 66 | ```bash 67 | terraform apply 68 | ``` 69 | 70 | * Test the deploy. 71 | 72 | When the `terraform apply` command completes, use the AWS console, you should see the new new EC2 instance, and all the resources created with the `terraform-example` tag. 73 | 74 | * Clean up the resources created. 75 | 76 | When you have finished, run command: 77 | 78 | ```bash 79 | terraform destroy 80 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Module 2 | 3 | It shows how to develop (not duplicating code) web server clusters in different environments using a module. 4 | 5 | The environments are: 6 | 7 | * Staging (stage) 8 | * Production (prod) 9 | 10 | This is the file layout: 11 | 12 | ```bash 13 | global 14 | └── s3/ 15 | ├── main.tf 16 | └── (etc) 17 | 18 | modules 19 | └── services/ 20 | └── webserver-cluster/ 21 | ├── main.tf 22 | └── (etc) 23 | 24 | stage 25 | ├── services/ 26 | │ └── webserver-cluster/ 27 | │ ├── main.tf 28 | │ └── (etc) 29 | └── data-stores/ 30 | └── mysql/ 31 | ├── main.tf 32 | └── (etc) 33 | 34 | prod 35 | ├── services/ 36 | │ └── webserver-cluster/ 37 | │ ├── main.tf 38 | │ └── (etc) 39 | └── data-stores/ 40 | └── mysql/ 41 | ├── main.tf 42 | └── (etc) 43 | ``` 44 | 45 | 46 | 47 | ## Using the code 48 | 49 | * Configure your AWS access keys. 50 | 51 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 52 | 53 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 54 | 55 | * The default credentials file 56 | 57 | Set credentials in the AWS credentials profile file on your local system, located at: 58 | 59 | `~/.aws/credentials` on Linux, macOS, or Unix 60 | 61 | `C:\Users\USERNAME\.aws\credentials` on Windows 62 | 63 | This file should contain lines in the following format: 64 | 65 | ```bash 66 | [default] 67 | aws_access_key_id = 68 | aws_secret_access_key = 69 | ``` 70 | Substitute your own AWS credentials values for the values `` and ``. 71 | 72 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 73 | 74 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 75 | 76 | To set these variables on Linux, macOS, or Unix, use `export`: 77 | 78 | ```bash 79 | export AWS_ACCESS_KEY_ID= 80 | export AWS_SECRET_ACCESS_KEY= 81 | ``` 82 | 83 | To set these variables on Windows, use `set`: 84 | 85 | ```bash 86 | set AWS_ACCESS_KEY_ID= 87 | set AWS_SECRET_ACCESS_KEY= 88 | ``` 89 | 90 | 91 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/13/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | 5 | } 6 | 7 | # Create an IAM user 8 | resource "aws_iam_user" "example" { 9 | count = "${length(var.user_names)}" 10 | name = "${element(var.user_names, count.index)}" 11 | 12 | } 13 | 14 | # Data source: IAM policy document 15 | data "aws_iam_policy_document" "ec2_read_only" { 16 | statement { 17 | effect = "Allow" 18 | actions = ["ec2:Describe*"] 19 | resources = ["*"] 20 | } 21 | } 22 | 23 | # Create an IAM policy 24 | resource "aws_iam_policy" "ec2_read_only" { 25 | name = "ec2-read-only" 26 | policy = "${data.aws_iam_policy_document.ec2_read_only.json}" 27 | } 28 | 29 | # Create an IAM user policy attachement 30 | resource "aws_iam_user_policy_attachment" "ec2_access" { 31 | count = "${length(var.user_names)}" 32 | user = "${element(aws_iam_user.example.*.name, count.index)}" 33 | policy_arn = "${aws_iam_policy.ec2_read_only.arn}" 34 | } 35 | 36 | # Create an IAM policy 37 | resource "aws_iam_policy" "cloudwatch_read_only" { 38 | name = "cloudwatch-read-only" 39 | policy = "${data.aws_iam_policy_document.cloudwatch_read_only.json}" 40 | } 41 | 42 | # Data source: IAM policy document 43 | data "aws_iam_policy_document" "cloudwatch_read_only" { 44 | statement { 45 | effect = "Allow" 46 | actions = ["cloudwatch:Describe*", "cloudwatch:Get*", "cloudwatch:List*"] 47 | resources = ["*"] 48 | } 49 | } 50 | 51 | # Create an IAM policy 52 | resource "aws_iam_policy" "cloudwatch_full_access" { 53 | name = "cloudwatch-full-access" 54 | policy = "${data.aws_iam_policy_document.cloudwatch_full_access.json}" 55 | } 56 | 57 | # Data source: IAM policy document 58 | data "aws_iam_policy_document" "cloudwatch_full_access" { 59 | statement { 60 | effect = "Allow" 61 | actions = ["cloudwatch:*"] 62 | resources = ["*"] 63 | } 64 | } 65 | 66 | # Create an IAM user policy attachement 67 | resource "aws_iam_user_policy_attachment" "neo_cloud_watch_full_access" { 68 | count = "${var.give_neo_cloudwatch_full_access}" 69 | 70 | user = "${aws_iam_user.example.0.name}" 71 | policy_arn = "${aws_iam_policy.cloudwatch_full_access.arn}" 72 | } 73 | 74 | # Create an IAM user policy attachement 75 | resource "aws_iam_user_policy_attachment" "neo_cloud_watch_read_only" { 76 | count = "${1 - var.give_neo_cloudwatch_full_access}" 77 | 78 | user = "${aws_iam_user.example.0.name}" 79 | policy_arn = "${aws_iam_policy.cloudwatch_read_only.arn}" 80 | } 81 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/3/README.md: -------------------------------------------------------------------------------- 1 | ## Using the code 2 | 3 | * Configure your AWS access keys. 4 | 5 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 6 | 7 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 8 | 9 | * The default credentials file 10 | 11 | Set credentials in the AWS credentials profile file on your local system, located at: 12 | 13 | `~/.aws/credentials` on Linux, macOS, or Unix 14 | 15 | `C:\Users\USERNAME\.aws\credentials` on Windows 16 | 17 | This file should contain lines in the following format: 18 | 19 | ```bash 20 | [default] 21 | aws_access_key_id = 22 | aws_secret_access_key = 23 | ``` 24 | Substitute your own AWS credentials values for the values `` and ``. 25 | 26 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 27 | 28 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 29 | 30 | To set these variables on Linux, macOS, or Unix, use `export`: 31 | 32 | ```bash 33 | export AWS_ACCESS_KEY_ID= 34 | export AWS_SECRET_ACCESS_KEY= 35 | ``` 36 | 37 | To set these variables on Windows, use `set`: 38 | 39 | ```bash 40 | set AWS_ACCESS_KEY_ID= 41 | set AWS_SECRET_ACCESS_KEY= 42 | ``` 43 | 44 | * Initialize working directory. 45 | 46 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 47 | 48 | ```bash 49 | terraform init 50 | ``` 51 | 52 | * Validate the changes. 53 | 54 | Run command: 55 | 56 | ```bash 57 | terraform plan 58 | ``` 59 | 60 | * Deploy the changes. 61 | 62 | Run command: 63 | 64 | ```bash 65 | terraform apply 66 | ``` 67 | 68 | * Test the web server. 69 | 70 | When the `terraform apply` command completes, it will output the public IP address of the web server. 71 | 72 | You can test it in two ways: 73 | 74 | * Running this command: 75 | 76 | ```bash 77 | curl http://:8080/ 78 | ``` 79 | 80 | * Writing in your browser this URL: `http://:8080/` 81 | 82 | You should get a `Hello, World` response message. 83 | 84 | * Clean up the resources created. 85 | 86 | When you have finished, run command: 87 | 88 | ```bash 89 | terraform destroy 90 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/TerraformBlocks.md: -------------------------------------------------------------------------------- 1 | In Terraform, configuration files are composed of blocks that define different elements of your infrastructure. Let's explore three fundamental types of blocks: Terraform Block, Providers Block, and Resource Block, along with examples. 2 | 3 | ### 1. Terraform Block 4 | 5 | The `terraform` block is used to define settings and configurations for the Terraform execution. 6 | It's where you specify the required version of Terraform, configure backend settings, and set various options. 7 | 8 | Example: 9 | 10 | ```hcl 11 | terraform { 12 | required_version = ">= 0.14" 13 | backend "s3" { 14 | bucket = "my-terraform-state-bucket" 15 | key = "path/to/terraform.tfstate" 16 | region = "us-east-1" 17 | } 18 | } 19 | ``` 20 | 21 | In this example: 22 | 23 | - `required_version`: Specifies the minimum version of Terraform required. 24 | - `backend`: Configures the backend where Terraform stores its state. In this case, it's using an S3 bucket. 25 | 26 | ### 2. Providers Block 27 | 28 | The `providers` block is used to configure the providers (e.g., AWS, Azure, Google Cloud) used in your Terraform configuration. 29 | It includes authentication details and other provider-specific settings. 30 | 31 | Example (AWS provider): 32 | 33 | ```hcl 34 | provider "aws" { 35 | region = "us-west-2" 36 | access_key = "your-access-key" 37 | secret_key = "your-secret-key" 38 | } 39 | ``` 40 | 41 | In this example: 42 | 43 | - `provider "aws"`: Specifies that you are configuring the AWS provider. 44 | - `region`: Specifies the AWS region to operate in. 45 | - `access_key` and `secret_key`: Provide AWS credentials for authentication. 46 | Note that it's better to use environment variables or IAM roles for security reasons. 47 | 48 | ### 3. Resource Block 49 | 50 | The `resource` block is used to define and provision infrastructure resources. 51 | Resources can be virtual machines, databases, networks, etc. Each resource block corresponds to a single resource type. 52 | 53 | Example (AWS S3 bucket): 54 | 55 | ```hcl 56 | resource "aws_s3_bucket" "my_bucket" { 57 | bucket = "my-unique-bucket-name" 58 | acl = "private" 59 | } 60 | ``` 61 | 62 | In this example: 63 | 64 | - `resource "aws_s3_bucket"`: Specifies that you are creating an S3 bucket using the AWS provider. 65 | - `my_bucket`: An arbitrary name to reference this resource. 66 | - `bucket`: Specifies the name of the S3 bucket. 67 | - `acl`: Specifies the access control list for the bucket (in this case, set to "private"). 68 | 69 | Terraform uses these blocks to understand the desired state of your infrastructure and then plans and applies changes accordingly. 70 | These blocks help structure your Terraform configuration in a modular and organized way. 71 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/5/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "ap-south-1" 4 | } 5 | 6 | # Data source: query the list of availability zones 7 | data "aws_availability_zones" "all" {} 8 | 9 | # Create a Security Group for an EC2 instance 10 | resource "aws_security_group" "instance" { 11 | name = "terraform-example-instance" 12 | 13 | ingress { 14 | from_port = "${var.server_port}" 15 | to_port = "${var.server_port}" 16 | protocol = "tcp" 17 | cidr_blocks = ["0.0.0.0/0"] 18 | } 19 | 20 | lifecycle { 21 | create_before_destroy = true 22 | } 23 | } 24 | 25 | # Create a Security Group for an ELB 26 | resource "aws_security_group" "elb" { 27 | name = "terraform-example-elb" 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 | egress { 37 | from_port = 0 38 | to_port = 0 39 | protocol = "-1" 40 | cidr_blocks = ["0.0.0.0/0"] 41 | } 42 | } 43 | 44 | # Create a Launch Configuration 45 | resource "aws_launch_configuration" "example" { 46 | image_id = "ami-785db401" 47 | instance_type = "t2.micro" 48 | security_groups = ["${aws_security_group.instance.id}"] 49 | 50 | user_data = <<-EOF 51 | #!/bin/bash 52 | echo "Hello, World" > index.html 53 | nohup busybox httpd -f -p "${var.server_port}" & 54 | EOF 55 | 56 | lifecycle { 57 | create_before_destroy = true 58 | } 59 | } 60 | 61 | # Create an Autoscaling Group 62 | resource "aws_autoscaling_group" "example" { 63 | launch_configuration = "${aws_launch_configuration.example.id}" 64 | availability_zones = ["${data.aws_availability_zones.all.names}"] 65 | 66 | load_balancers = ["${aws_elb.example.name}"] 67 | health_check_type = "ELB" 68 | 69 | min_size = 2 70 | max_size = 10 71 | 72 | tag { 73 | key = "Name" 74 | value = "terraform-asg-example" 75 | propagate_at_launch = true 76 | } 77 | } 78 | 79 | # Create an ELB 80 | resource "aws_elb" "example" { 81 | name = "terraform-asg-example" 82 | availability_zones = ["${data.aws_availability_zones.all.names}"] 83 | security_groups = ["${aws_security_group.elb.id}"] 84 | 85 | listener { 86 | lb_port = 80 87 | lb_protocol = "http" 88 | instance_port = "${var.server_port}" 89 | instance_protocol = "http" 90 | } 91 | 92 | health_check { 93 | healthy_threshold = 2 94 | unhealthy_threshold = 2 95 | timeout = 3 96 | interval = 30 97 | target = "HTTP:${var.server_port}/" 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/prod/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (production environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the templates in this folder. 16 | * This uses a Terraform module. See [modules/services/webserver-cluster](../../../modules/services/webserver-cluster). 17 | 18 | ## Using the code 19 | 20 | * Configure your AWS access keys. 21 | 22 | * Initialize working directory. 23 | 24 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 25 | 26 | ```bash 27 | terraform init 28 | ``` 29 | 30 | * Configure Terraform backend. 31 | 32 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 33 | 34 | ```bash 35 | bucket = "" 36 | ``` 37 | 38 | * Configure the bucket used for the database's remote state storage. 39 | 40 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 41 | 42 | ```hcl 43 | variable "db_remote_state_bucket" { 44 | description = "The name of the S3 bucket used for the database's remote state storage" 45 | default = "" 46 | } 47 | ``` 48 | 49 | * Validate the changes. 50 | 51 | Run command: 52 | 53 | ```bash 54 | terraform plan 55 | ``` 56 | 57 | * Deploy the changes. 58 | 59 | Run command: 60 | 61 | ```bash 62 | terraform apply 63 | ``` 64 | 65 | * Test the cluster of web servers. 66 | 67 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 68 | 69 | ```bash 70 | curl http:/// 71 | ``` 72 | 73 | * Clean up the resources created. 74 | 75 | When you have finished, run command: 76 | 77 | ```bash 78 | terraform destroy 79 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/stage/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (staging environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module. See [modules/services/webserver-cluster](../../../../modules/services/webserver-cluster). 18 | 19 | ## Using the code 20 | 21 | * Configure your AWS access keys. 22 | 23 | * Initialize working directory. 24 | 25 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 26 | 27 | ```bash 28 | terraform init 29 | ``` 30 | 31 | * Configure Terraform backend. 32 | 33 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 34 | 35 | ```bash 36 | bucket = "" 37 | ``` 38 | 39 | * Configure the bucket used for the database's remote state storage. 40 | 41 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 42 | 43 | ```hcl 44 | variable "db_remote_state_bucket" { 45 | description = "The name of the S3 bucket used for the database's remote state storage" 46 | default = "" 47 | } 48 | ``` 49 | 50 | * Validate the changes. 51 | 52 | Run command: 53 | 54 | ```bash 55 | terraform plan 56 | ``` 57 | 58 | * Deploy the changes. 59 | 60 | Run command: 61 | 62 | ```bash 63 | terraform apply 64 | ``` 65 | 66 | * Test the cluster of web servers. 67 | 68 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 69 | 70 | ```bash 71 | curl http:/// 72 | ``` 73 | 74 | * Clean up the resources created. 75 | 76 | When you have finished, run command: 77 | 78 | ```bash 79 | terraform destroy 80 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/prod/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (production environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module. See [modules/services/webserver-cluster](../../../../modules/services/webserver-cluster). 18 | 19 | 20 | ## Using the code 21 | 22 | * Configure your AWS access keys. 23 | 24 | * Initialize working directory. 25 | 26 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 27 | 28 | ```bash 29 | terraform init 30 | ``` 31 | 32 | * Configure Terraform backend. 33 | 34 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 35 | 36 | ```bash 37 | bucket = "" 38 | ``` 39 | 40 | * Configure the bucket used for the database's remote state storage. 41 | 42 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 43 | 44 | ```hcl 45 | variable "db_remote_state_bucket" { 46 | description = "The name of the S3 bucket used for the database's remote state storage" 47 | default = "" 48 | } 49 | ``` 50 | 51 | * Validate the changes. 52 | 53 | Run command: 54 | 55 | ```bash 56 | terraform plan 57 | ``` 58 | 59 | * Deploy the changes. 60 | 61 | Run command: 62 | 63 | ```bash 64 | terraform apply 65 | ``` 66 | 67 | * Test the cluster of web servers. 68 | 69 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 70 | 71 | ```bash 72 | curl http:/// 73 | ``` 74 | 75 | * Clean up the resources created. 76 | 77 | When you have finished, run command: 78 | 79 | ```bash 80 | terraform destroy 81 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/prod/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (production environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module in another repo. See 18 | [https://github.com/alfonsof/terraform-aws-repo-examples](https://github.com/alfonsof/terraform-aws-repo-examples). 19 | 20 | ## Using the code 21 | 22 | * Configure your AWS access keys. 23 | 24 | * Initialize working directory. 25 | 26 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 27 | 28 | ```bash 29 | terraform init 30 | ``` 31 | 32 | * Configure Terraform backend. 33 | 34 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 35 | 36 | ```bash 37 | bucket = "" 38 | ``` 39 | 40 | * Configure the bucket used for the database's remote state storage. 41 | 42 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 43 | 44 | ```hcl 45 | variable "db_remote_state_bucket" { 46 | description = "The name of the S3 bucket used for the database's remote state storage" 47 | default = "" 48 | } 49 | ``` 50 | 51 | * Validate the changes. 52 | 53 | Run command: 54 | 55 | ```bash 56 | terraform plan 57 | ``` 58 | 59 | * Deploy the changes. 60 | 61 | Run command: 62 | 63 | ```bash 64 | terraform apply 65 | ``` 66 | 67 | * Test the cluster of web servers. 68 | 69 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 70 | 71 | ```bash 72 | curl http:/// 73 | ``` 74 | 75 | * Clean up the resources created. 76 | 77 | When you have finished, run command: 78 | 79 | ```bash 80 | terraform destroy 81 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/stage/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (staging environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module in another repo. See 18 | [https://github.com/alfonsof/terraform-aws-repo-examples](https://github.com/alfonsof/terraform-aws-repo-examples). 19 | 20 | ## Using the code 21 | 22 | * Configure your AWS access keys. 23 | 24 | * Initialize working directory. 25 | 26 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 27 | 28 | ```bash 29 | terraform init 30 | ``` 31 | 32 | * Configure Terraform backend. 33 | 34 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 35 | 36 | ```bash 37 | bucket = "" 38 | ``` 39 | 40 | * Configure the bucket used for the database's remote state storage. 41 | 42 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 43 | 44 | ```hcl 45 | variable "db_remote_state_bucket" { 46 | description = "The name of the S3 bucket used for the database's remote state storage" 47 | default = "" 48 | } 49 | ``` 50 | 51 | * Validate the changes. 52 | 53 | Run command: 54 | 55 | ```bash 56 | terraform plan 57 | ``` 58 | 59 | * Deploy the changes. 60 | 61 | Run command: 62 | 63 | ```bash 64 | terraform apply 65 | ``` 66 | 67 | * Test the cluster of web servers. 68 | 69 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 70 | 71 | ```bash 72 | curl http:/// 73 | ``` 74 | 75 | * Clean up the resources created. 76 | 77 | When you have finished, run command: 78 | 79 | ```bash 80 | terraform destroy 81 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/TerraformStateFile.md: -------------------------------------------------------------------------------- 1 | The Terraform state file is a crucial component that tracks the state of your infrastructure managed by Terraform. It contains information about the resources that Terraform manages, their current configuration, and metadata. Storing and managing the Terraform state properly is essential for collaborative and consistent infrastructure management. 2 | 3 | ## Terraform State File: 4 | 5 | - **Purpose**: The state file is used to map the real-world resources to the Terraform configuration, enabling Terraform to understand the current state of your infrastructure. 6 | 7 | - **Contents**: It includes details like resource IDs, IP addresses, DNS names, and other important information. 8 | 9 | - **Location**: By default, Terraform stores the state locally in a file named `terraform.tfstate`. However, in a collaborative or production environment, it's recommended to use a remote backend for storing the state. 10 | 11 | ## Maintaining High Availability with AWS: 12 | 13 | ### 1. **AWS S3 for Remote Backend:** 14 | 15 | By using an AWS S3 bucket as a remote backend, you can achieve high availability and durability for your Terraform state file. This involves configuring your Terraform backend to use S3. 16 | 17 | Example Terraform configuration: 18 | 19 | ```hcl 20 | terraform { 21 | backend "s3" { 22 | bucket = "your-terraform-state-bucket" 23 | key = "path/to/terraform.tfstate" 24 | region = "us-east-1" 25 | encrypt = true 26 | dynamodb_table = "your-lock-table" 27 | } 28 | } 29 | ``` 30 | 31 | - **`bucket`**: The name of the S3 bucket where the state file will be stored. 32 | - **`key`**: The path within the bucket to store the state file. 33 | - **`region`**: The AWS region where the S3 bucket is located. 34 | - **`encrypt`**: Enables server-side encryption for the state file. 35 | - **`dynamodb_table`**: (Optional) Specifies the DynamoDB table name for state locking to prevent concurrent modifications. 36 | 37 | ### 2. **Using DynamoDB for State Locking:** 38 | 39 | State locking ensures that only one user or process can modify the Terraform state at a time, preventing conflicts. DynamoDB can be used for state locking. 40 | 41 | Example DynamoDB configuration: 42 | 43 | ```hcl 44 | terraform { 45 | backend "s3" { 46 | bucket = "your-terraform-state-bucket" 47 | key = "path/to/terraform.tfstate" 48 | region = "us-east-1" 49 | encrypt = true 50 | dynamodb_table = "your-lock-table" 51 | } 52 | } 53 | ``` 54 | 55 | - **`dynamodb_table`**: Specifies the name of the DynamoDB table to use for state locking. 56 | 57 | By configuring a DynamoDB table for state locking, you ensure that multiple Terraform executions don't interfere with each other, maintaining the consistency and integrity of your infrastructure state. 58 | 59 | Ensure that the AWS credentials used by Terraform have the necessary permissions to read and write to both S3 and DynamoDB. 60 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/global/s3/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Remote State example 2 | 3 | This folder contains an example of a Terraform file [Terraform](https://www.terraform.io/) that create a S3 bucket that can be used for remote state storage. 4 | 5 | This Terraform file deploys the creation a of a bucket on AWS (Amazon Web Services) using S3. 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Modify configuration. 27 | 28 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 29 | 30 | You can modify the S3 bucket name in several ways: 31 | 32 | * Loading variables from command line flags. 33 | 34 | Run Terraform commands in this way: 35 | 36 | ```bash 37 | terraform plan -var 'bucket_name=' 38 | ``` 39 | 40 | ```bash 41 | terraform apply -var 'bucket_name=' 42 | ``` 43 | 44 | * Loading variables from a file. 45 | 46 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 47 | 48 | ```bash 49 | bucket_name = "" 50 | ``` 51 | 52 | * Loading variables from environment variables. 53 | 54 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 55 | 56 | ```bash 57 | TF_VAR_bucket_name= 58 | ``` 59 | 60 | * Variable defaults. 61 | 62 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 63 | 64 | ```hcl 65 | variable "bucket_name" { 66 | description = "The name of the S3 bucket. Must be globally unique." 67 | default = "" 68 | } 69 | ``` 70 | 71 | * Validate the changes. 72 | 73 | Run command: 74 | 75 | ```bash 76 | terraform plan 77 | ``` 78 | 79 | * Deploy the changes. 80 | 81 | Run command: 82 | 83 | ```bash 84 | terraform apply 85 | ``` 86 | 87 | * Clean up the resources created. 88 | 89 | When you have finished, run command: 90 | 91 | ```bash 92 | terraform destroy 93 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/10/live/global/s3/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Remote State example 2 | 3 | This folder contains an example of a Terraform file [Terraform](https://www.terraform.io/) that create a S3 bucket that can be used for remote state storage. 4 | 5 | This Terraform file deploys the creation a of a bucket on AWS (Amazon Web Services) using S3. 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Modify configuration. 27 | 28 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 29 | 30 | You can modify the S3 bucket name in several ways: 31 | 32 | * Loading variables from command line flags. 33 | 34 | Run Terraform commands in this way: 35 | 36 | ```bash 37 | terraform plan -var 'bucket_name=' 38 | ``` 39 | 40 | ```bash 41 | terraform apply -var 'bucket_name=' 42 | ``` 43 | 44 | * Loading variables from a file. 45 | 46 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 47 | 48 | ```bash 49 | bucket_name = "" 50 | ``` 51 | 52 | * Loading variables from environment variables. 53 | 54 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 55 | 56 | ```bash 57 | TF_VAR_bucket_name= 58 | ``` 59 | 60 | * Variable defaults. 61 | 62 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 63 | 64 | ```hcl 65 | variable "bucket_name" { 66 | description = "The name of the S3 bucket. Must be globally unique." 67 | default = "" 68 | } 69 | ``` 70 | 71 | * Validate the changes. 72 | 73 | Run command: 74 | 75 | ```bash 76 | terraform plan 77 | ``` 78 | 79 | * Deploy the changes. 80 | 81 | Run command: 82 | 83 | ```bash 84 | terraform apply 85 | ``` 86 | 87 | * Clean up the resources created. 88 | 89 | When you have finished, run command: 90 | 91 | ```bash 92 | terraform destroy 93 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/12/live/global/s3/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Remote State example 2 | 3 | This folder contains an example of a Terraform file [Terraform](https://www.terraform.io/) that create a S3 bucket that can be used for remote state storage. 4 | 5 | This Terraform file deploys the creation a of a bucket on AWS (Amazon Web Services) using S3. 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Modify configuration. 27 | 28 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 29 | 30 | You can modify the S3 bucket name in several ways: 31 | 32 | * Loading variables from command line flags. 33 | 34 | Run Terraform commands in this way: 35 | 36 | ```bash 37 | terraform plan -var 'bucket_name=' 38 | ``` 39 | 40 | ```bash 41 | terraform apply -var 'bucket_name=' 42 | ``` 43 | 44 | * Loading variables from a file. 45 | 46 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 47 | 48 | ```bash 49 | bucket_name = "" 50 | ``` 51 | 52 | * Loading variables from environment variables. 53 | 54 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 55 | 56 | ```bash 57 | TF_VAR_bucket_name= 58 | ``` 59 | 60 | * Variable defaults. 61 | 62 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 63 | 64 | ```hcl 65 | variable "bucket_name" { 66 | description = "The name of the S3 bucket. Must be globally unique." 67 | default = "" 68 | } 69 | ``` 70 | 71 | * Validate the changes. 72 | 73 | Run command: 74 | 75 | ```bash 76 | terraform plan 77 | ``` 78 | 79 | * Deploy the changes. 80 | 81 | Run command: 82 | 83 | ```bash 84 | terraform apply 85 | ``` 86 | 87 | * Clean up the resources created. 88 | 89 | When you have finished, run command: 90 | 91 | ```bash 92 | terraform destroy 93 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/global/s3/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Remote State example 2 | 3 | This folder contains an example of a Terraform file [Terraform](https://www.terraform.io/) that create a S3 bucket that can be used for remote state storage. 4 | 5 | This Terraform file deploys the creation a of a bucket on AWS (Amazon Web Services) using S3. 6 | 7 | ## Requirements 8 | 9 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 10 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 11 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 12 | * This code was written for Terraform 0.10.x. 13 | 14 | ## Using the code 15 | 16 | * Configure your AWS access keys. 17 | 18 | * Initialize working directory. 19 | 20 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 21 | 22 | ```bash 23 | terraform init 24 | ``` 25 | 26 | * Modify configuration. 27 | 28 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 29 | 30 | You can modify the S3 bucket name in several ways: 31 | 32 | * Loading variables from command line flags. 33 | 34 | Run Terraform commands in this way: 35 | 36 | ```bash 37 | terraform plan -var 'bucket_name=' 38 | ``` 39 | 40 | ```bash 41 | terraform apply -var 'bucket_name=' 42 | ``` 43 | 44 | * Loading variables from a file. 45 | 46 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 47 | 48 | ```bash 49 | bucket_name = "" 50 | ``` 51 | 52 | * Loading variables from environment variables. 53 | 54 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 55 | 56 | ```bash 57 | TF_VAR_bucket_name= 58 | ``` 59 | 60 | * Variable defaults. 61 | 62 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 63 | 64 | ```hcl 65 | variable "bucket_name" { 66 | description = "The name of the S3 bucket. Must be globally unique." 67 | default = "" 68 | } 69 | ``` 70 | 71 | * Validate the changes. 72 | 73 | Run command: 74 | 75 | ```bash 76 | terraform plan 77 | ``` 78 | 79 | * Deploy the changes. 80 | 81 | Run command: 82 | 83 | ```bash 84 | terraform apply 85 | ``` 86 | 87 | * Clean up the resources created. 88 | 89 | When you have finished, run command: 90 | 91 | ```bash 92 | terraform destroy 93 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/11/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Loops example 2 | 3 | This folder contains the loops example of a [Terraform](https://www.terraform.io/) file on AWS (Amazon Web Services). 4 | 5 | It shows how do loops in Terraform. 6 | 7 | This Terraform file create 3 IAM users, create an IAM policy and attach the IAM policy to the IAM users. 8 | 9 | We use the meta-parameter "count" and two interpolation functions: 10 | 11 | * "${element(LIST, INDEX)}" 12 | * "${length(LIST)}" 13 | 14 | ## Requirements 15 | 16 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 17 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 18 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 19 | * This code was written for Terraform 0.10.x. 20 | 21 | ## Using the code 22 | 23 | * Configure your AWS access keys. 24 | 25 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 26 | 27 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 28 | 29 | * The default credentials file 30 | 31 | Set credentials in the AWS credentials profile file on your local system, located at: 32 | 33 | `~/.aws/credentials` on Linux, macOS, or Unix 34 | 35 | `C:\Users\USERNAME\.aws\credentials` on Windows 36 | 37 | This file should contain lines in the following format: 38 | 39 | ```bash 40 | [default] 41 | aws_access_key_id = 42 | aws_secret_access_key = 43 | ``` 44 | Substitute your own AWS credentials values for the values `` and ``. 45 | 46 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 47 | 48 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 49 | 50 | To set these variables on Linux, macOS, or Unix, use `export`: 51 | 52 | ```bash 53 | export AWS_ACCESS_KEY_ID= 54 | export AWS_SECRET_ACCESS_KEY= 55 | ``` 56 | 57 | To set these variables on Windows, use `set`: 58 | 59 | ```bash 60 | set AWS_ACCESS_KEY_ID= 61 | set AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | * Initialize working directory. 65 | 66 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 67 | 68 | ```bash 69 | terraform init 70 | ``` 71 | 72 | * Validate the changes: 73 | 74 | ```bash 75 | terraform plan 76 | ``` 77 | 78 | * Deploy the changes: 79 | 80 | ```bash 81 | terraform apply 82 | ``` 83 | 84 | * Test the deployment. 85 | 86 | You should see 3 IAM users, an IAM policy and the IAM policy attached to the IAM users. 87 | 88 | * Clean up the resources created when you have finished: 89 | 90 | ```bash 91 | terraform destroy 92 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/README.md: -------------------------------------------------------------------------------- 1 | # Terraform File Layout example 2 | 3 | This folder contains a File Layout example of a [Terraform](https://www.terraform.io/) file on AWS (Amazon Web Services). 4 | 5 | This is the file layout: 6 | 7 | ```bash 8 | global 9 | └── s3/ 10 | ├── main.tf 11 | └── (etc) 12 | 13 | stage 14 | ├── services/ 15 | │ └── webserver-cluster/ 16 | │ ├── main.tf 17 | │ └── (etc) 18 | └── data-stores/ 19 | └── mysql/ 20 | ├── main.tf 21 | └── (etc) 22 | ``` 23 | 24 | It uses: 25 | 26 | * Terraform Remote State example: [global/s3](global/s3) 27 | * Terraform MySQL on RDS example: [stage/data-stores/mysql](stage/data-stores/mysql) 28 | * Terraform Web Server Cluster example: [stage/services/webserver-cluster](stage/services/webserver-cluster) 29 | 30 | ## Requirements 31 | 32 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 33 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 34 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 35 | * This code was written for Terraform 0.10.x. 36 | 37 | ## Using the code 38 | 39 | * Configure your AWS access keys. 40 | 41 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 42 | 43 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 44 | 45 | * The default credentials file 46 | 47 | Set credentials in the AWS credentials profile file on your local system, located at: 48 | 49 | `~/.aws/credentials` on Linux, macOS, or Unix 50 | 51 | `C:\Users\USERNAME\.aws\credentials` on Windows 52 | 53 | This file should contain lines in the following format: 54 | 55 | ```bash 56 | [default] 57 | aws_access_key_id = 58 | aws_secret_access_key = 59 | ``` 60 | Substitute your own AWS credentials values for the values `` and ``. 61 | 62 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 63 | 64 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 65 | 66 | To set these variables on Linux, macOS, or Unix, use `export`: 67 | 68 | ```bash 69 | export AWS_ACCESS_KEY_ID= 70 | export AWS_SECRET_ACCESS_KEY= 71 | ``` 72 | 73 | To set these variables on Windows, use `set`: 74 | 75 | ```bash 76 | set AWS_ACCESS_KEY_ID= 77 | set AWS_SECRET_ACCESS_KEY= 78 | ``` 79 | 80 | * Use Terraform Remote State example for creating the remote state bucket. See: [global/s3](global/s3) 81 | 82 | * Use Terraform MySQL on RDS example for creating a MySQL database. See: [stage/data-stores/mysql](stage/data-stores/mysql) 83 | 84 | * Use Terraform Web Server Cluster example for creating a web server cluster. See: [stage/services/webserver-cluster](stage/services/webserver-cluster) 85 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/7/README.md: -------------------------------------------------------------------------------- 1 | # Terraform State 2 | 3 | This Terraform file use the S3 bucket created in the previous example to store the information about what infrastructure has been created. 4 | 5 | This information is stored in the Terraform state file `terraform.tfstate`. This file contains a JSON format that records a mapping from the representation of the resources on AWS to Terrafom resources in the configuration files. 6 | 7 | ## Using the code 8 | 9 | * Configure your AWS access keys. 10 | 11 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 12 | 13 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 14 | 15 | * The default credentials file 16 | 17 | Set credentials in the AWS credentials profile file on your local system, located at: 18 | 19 | `~/.aws/credentials` on Linux, macOS, or Unix 20 | 21 | `C:\Users\USERNAME\.aws\credentials` on Windows 22 | 23 | This file should contain lines in the following format: 24 | 25 | ```bash 26 | [default] 27 | aws_access_key_id = 28 | aws_secret_access_key = 29 | ``` 30 | Substitute your own AWS credentials values for the values `` and ``. 31 | 32 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 33 | 34 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 35 | 36 | To set these variables on Linux, macOS, or Unix, use `export`: 37 | 38 | ```bash 39 | export AWS_ACCESS_KEY_ID= 40 | export AWS_SECRET_ACCESS_KEY= 41 | ``` 42 | 43 | To set these variables on Windows, use `set`: 44 | 45 | ```bash 46 | set AWS_ACCESS_KEY_ID= 47 | set AWS_SECRET_ACCESS_KEY= 48 | ``` 49 | 50 | * Initialize working directory. 51 | 52 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 53 | 54 | ```bash 55 | terraform init 56 | ``` 57 | 58 | * Configure Terraform backend. 59 | 60 | You must modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 61 | 62 | ```bash 63 | bucket = "" 64 | ``` 65 | 66 | * Validate the changes. 67 | 68 | Run command: 69 | 70 | ```bash 71 | terraform plan 72 | ``` 73 | 74 | * Deploy the changes. 75 | 76 | Run command: 77 | 78 | ```bash 79 | terraform apply 80 | ``` 81 | 82 | * Test the deploy. 83 | 84 | When the `terraform apply` command completes, use the AWS console, you should see the `terraform.tfstate` file created in the S3 bucket. 85 | 86 | The `terraform.tfstate` file is where is stored the terraform state. 87 | 88 | * Clean up the resources created. 89 | 90 | When you have finished, run command: 91 | 92 | ```bash 93 | terraform destroy 94 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/8/stage/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Configure the AWS provider 2 | provider "aws" { 3 | region = "eu-west-1" 4 | } 5 | 6 | # Data source: query the list of availability zones 7 | data "aws_availability_zones" "all" {} 8 | 9 | # Data source: DB remote state 10 | data "terraform_remote_state" "db" { 11 | backend = "s3" 12 | 13 | config { 14 | bucket = "${var.db_remote_state_bucket}" 15 | key = "${var.db_remote_state_key}" 16 | region = "eu-west-1" 17 | } 18 | } 19 | 20 | # Data source: Template file 21 | data "template_file" "user_data" { 22 | template = "${file("user-data.sh")}" 23 | 24 | vars { 25 | server_port = "${var.server_port}" 26 | db_address = "${data.terraform_remote_state.db.address}" 27 | db_port = "${data.terraform_remote_state.db.port}" 28 | } 29 | } 30 | 31 | # Create a Security Group for an EC2 instance 32 | resource "aws_security_group" "instance" { 33 | name = "terraform-example-instance" 34 | 35 | ingress { 36 | from_port = "${var.server_port}" 37 | to_port = "${var.server_port}" 38 | protocol = "tcp" 39 | cidr_blocks = ["0.0.0.0/0"] 40 | } 41 | 42 | lifecycle { 43 | create_before_destroy = true 44 | } 45 | } 46 | 47 | # Create a Security Group for an ELB 48 | resource "aws_security_group" "elb" { 49 | name = "terraform-example-elb" 50 | 51 | ingress { 52 | from_port = 80 53 | to_port = 80 54 | protocol = "tcp" 55 | cidr_blocks = ["0.0.0.0/0"] 56 | } 57 | 58 | egress { 59 | from_port = 0 60 | to_port = 0 61 | protocol = "-1" 62 | cidr_blocks = ["0.0.0.0/0"] 63 | } 64 | } 65 | 66 | # Create a Launch Configuration 67 | resource "aws_launch_configuration" "example" { 68 | image_id = "ami-785db401" 69 | instance_type = "t2.micro" 70 | security_groups = ["${aws_security_group.instance.id}"] 71 | user_data = "${data.template_file.user_data.rendered}" 72 | 73 | lifecycle { 74 | create_before_destroy = true 75 | } 76 | } 77 | 78 | # Create an Autoscaling Group 79 | resource "aws_autoscaling_group" "example" { 80 | launch_configuration = "${aws_launch_configuration.example.id}" 81 | availability_zones = ["${data.aws_availability_zones.all.names}"] 82 | 83 | load_balancers = ["${aws_elb.example.name}"] 84 | health_check_type = "ELB" 85 | 86 | min_size = 2 87 | max_size = 10 88 | 89 | tag { 90 | key = "Name" 91 | value = "terraform-asg-example" 92 | propagate_at_launch = true 93 | } 94 | } 95 | 96 | # Create an ELB 97 | resource "aws_elb" "example" { 98 | name = "terraform-asg-example" 99 | availability_zones = ["${data.aws_availability_zones.all.names}"] 100 | security_groups = ["${aws_security_group.elb.id}"] 101 | 102 | listener { 103 | lb_port = 80 104 | lb_protocol = "http" 105 | instance_port = "${var.server_port}" 106 | instance_protocol = "http" 107 | } 108 | 109 | health_check { 110 | healthy_threshold = 2 111 | unhealthy_threshold = 2 112 | timeout = 3 113 | interval = 30 114 | target = "HTTP:${var.server_port}/" 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /2024/TerraformCode_Explanation.md: -------------------------------------------------------------------------------- 1 | 2 | This Terraform code is used to provision an AWS EC2 instance in the "ap-south-1" region (Asia Pacific - Mumbai) using the specified AMI (Amazon Machine Image). Let's break down each section of the code to understand its components and functionality: 3 | 4 | ### Terraform Settings Block 5 | 6 | ```hcl 7 | terraform { 8 | required_providers { 9 | aws = { 10 | source = "hashicorp/aws" 11 | #version = "~> 3.21" # Optional but recommended in production 12 | } 13 | } 14 | } 15 | ``` 16 | 17 | - **terraform {} Block:** This block specifies the required providers and their sources. Providers are plugins that interact with the APIs of various services (in this case, AWS). 18 | - **required_providers:** Declares the providers Terraform needs. Here, it specifies that the AWS provider source is "hashicorp/aws". 19 | - **version:** (Commented out) This line suggests specifying a version constraint for the AWS provider, ensuring compatibility and preventing breaking changes. The `~> 3.21` means any version `>= 3.21.0` and `< 4.0.0`. 20 | 21 | ### Provider Block 22 | 23 | ```hcl 24 | provider "aws" { 25 | profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials 26 | region = "ap-south-1" 27 | } 28 | ``` 29 | 30 | - **provider "aws" {} Block:** Configures the AWS provider with the necessary credentials and settings. 31 | - **profile:** Specifies the AWS credentials profile to use. The "default" profile is used, which must be configured in the `$HOME/.aws/credentials` file on your local machine. 32 | - **region:** Specifies the AWS region where resources will be created. Here, it is set to "ap-south-1" (Mumbai region). 33 | 34 | ### Resource Block 35 | 36 | ```hcl 37 | resource "aws_instance" "ec2demo" { 38 | ami = "ami-06f621d90fa29f6d0" # Amazon Linux in us-east-1, update as per your region 39 | instance_type = "t2.micro" 40 | } 41 | ``` 42 | 43 | - **resource "aws_instance" "ec2demo" {} Block:** Defines an AWS EC2 instance resource named "ec2demo". 44 | - **ami:** Specifies the Amazon Machine Image (AMI) ID to use for the instance. This particular AMI ID corresponds to an Amazon Linux AMI in the "us-east-1" region, so it should be updated to match an appropriate AMI for the "ap-south-1" region. 45 | - **instance_type:** Specifies the type of EC2 instance to launch. "t2.micro" is a low-cost instance type suitable for low-traffic applications and development/testing purposes. 46 | 47 | ### Summary 48 | 49 | - **Terraform Settings Block:** Specifies the required provider for AWS, with an optional version constraint. 50 | - **Provider Block:** Configures the AWS provider using the default profile and sets the region to "ap-south-1". 51 | - **Resource Block:** Defines an EC2 instance resource, specifying the AMI and instance type. 52 | 53 | To successfully provision an EC2 instance with this configuration: 54 | 1. Ensure your AWS credentials are configured in `$HOME/.aws/credentials` under the "default" profile. 55 | 2. Verify that the AMI ID is valid for the specified region ("ap-south-1"). You may need to find a corresponding AMI for the region you are working in. 56 | 57 | You can execute this Terraform script by running the following commands: 58 | 1. `terraform init` - Initializes the configuration and downloads the required provider. 59 | 2. `terraform plan` - Previews the changes Terraform will make. 60 | 3. `terraform apply` - Applies the configuration, creating the specified EC2 instance. 61 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/TerraformVariables.md: -------------------------------------------------------------------------------- 1 | In Terraform, variables allow you to parameterize your configurations, making them more dynamic and reusable. Variables are placeholders for values that can be defined in your Terraform files or provided externally. They make it easy to customize your infrastructure without modifying the underlying Terraform code. 2 | 3 | Here's a brief explanation along with examples that you can run in AWS. 4 | 5 | ## Terraform Variables: 6 | 7 | ### 1. **Variable Definition:** 8 | 9 | In your Terraform configuration file (e.g., `variables.tf`), you define variables: 10 | 11 | ```hcl 12 | variable "instance_type" { 13 | description = "The EC2 instance type" 14 | type = string 15 | default = "t2.micro" 16 | } 17 | 18 | variable "ami" { 19 | description = "The Amazon Machine Image (AMI) ID" 20 | type = string 21 | } 22 | ``` 23 | 24 | In this example: 25 | 26 | - `instance_type`: A variable representing the type of EC2 instance. It has a default value of "t2.micro". 27 | - `ami`: A variable representing the AMI ID for the EC2 instance. It doesn't have a default value, meaning it must be provided when using the Terraform configuration. 28 | 29 | ### 2. **Using Variables in Resource Block:** 30 | 31 | Now, you can use these variables in your resource blocks: 32 | 33 | ```hcl 34 | resource "aws_instance" "example_instance" { 35 | ami = var.ami 36 | instance_type = var.instance_type 37 | 38 | // other configuration settings... 39 | } 40 | ``` 41 | 42 | Here, `var.ami` and `var.instance_type` reference the values assigned to the `ami` and `instance_type` variables. 43 | 44 | ### 3. **Variable Values:** 45 | 46 | You can provide values for variables in multiple ways: 47 | 48 | - **In a variable file (`terraform.tfvars`):** 49 | 50 | ```hcl 51 | instance_type = "t3.micro" 52 | ami = "ami-12345678" 53 | ``` 54 | 55 | Then, you can run Terraform commands like `terraform apply` without specifying these variables, and Terraform will automatically use the values from the `terraform.tfvars` file. 56 | 57 | - **Command-Line Flags:** 58 | 59 | ```bash 60 | terraform apply -var="instance_type=t3.micro" -var="ami=ami-12345678" 61 | ``` 62 | 63 | This allows you to provide variable values directly in the command line. 64 | 65 | - **Environment Variables:** 66 | 67 | Set environment variables with the `TF_VAR_` prefix: 68 | 69 | ```bash 70 | export TF_VAR_instance_type=t3.micro 71 | export TF_VAR_ami=ami-12345678 72 | ``` 73 | 74 | Terraform automatically picks up these environment variables. 75 | 76 | ### 4. **AWS Example:** 77 | 78 | Here's a simple example using AWS resources: 79 | 80 | ```hcl 81 | provider "aws" { 82 | region = "us-west-2" 83 | } 84 | 85 | variable "instance_type" { 86 | description = "The EC2 instance type" 87 | type = string 88 | default = "t2.micro" 89 | } 90 | 91 | variable "ami" { 92 | description = "The Amazon Machine Image (AMI) ID" 93 | type = string 94 | } 95 | 96 | resource "aws_instance" "example_instance" { 97 | ami = var.ami 98 | instance_type = var.instance_type 99 | 100 | tags = { 101 | Name = "ExampleInstance" 102 | } 103 | } 104 | ``` 105 | 106 | This Terraform configuration deploys an EC2 instance in the specified AWS region with the instance type and AMI ID as variables. You can customize the instance type and AMI by providing values through variable files, command-line flags, or environment variables. 107 | -------------------------------------------------------------------------------- /2024/3_Example.md: -------------------------------------------------------------------------------- 1 | Below is the Terraform code to create a VPC with one public subnet and one private subnet using the provided AWS provider block. 2 | 3 | ### Directory Structure 4 | 5 | ``` 6 | terraform_project/ 7 | ├── provider.tf 8 | ├── vpc.tf 9 | └── outputs.tf 10 | ``` 11 | 12 | ### File: provider.tf 13 | 14 | This file contains the AWS provider configuration. 15 | 16 | ```hcl 17 | # provider.tf 18 | 19 | # Provider Block 20 | provider "aws" { 21 | region = "ap-south-1" 22 | profile = "default" 23 | } 24 | ``` 25 | 26 | ### File: vpc.tf 27 | 28 | This file defines the VPC, public subnet, and private subnet. 29 | 30 | ```hcl 31 | # vpc.tf 32 | 33 | resource "aws_vpc" "main" { 34 | cidr_block = "10.0.0.0/16" 35 | tags = { 36 | Name = "main-vpc" 37 | } 38 | } 39 | 40 | resource "aws_subnet" "public" { 41 | vpc_id = aws_vpc.main.id 42 | cidr_block = "10.0.1.0/24" 43 | map_public_ip_on_launch = true 44 | availability_zone = "ap-south-1a" 45 | tags = { 46 | Name = "public-subnet" 47 | } 48 | } 49 | 50 | resource "aws_subnet" "private" { 51 | vpc_id = aws_vpc.main.id 52 | cidr_block = "10.0.2.0/24" 53 | availability_zone = "ap-south-1a" 54 | tags = { 55 | Name = "private-subnet" 56 | } 57 | } 58 | 59 | resource "aws_internet_gateway" "gw" { 60 | vpc_id = aws_vpc.main.id 61 | tags = { 62 | Name = "main-gateway" 63 | } 64 | } 65 | 66 | resource "aws_route_table" "public" { 67 | vpc_id = aws_vpc.main.id 68 | 69 | route { 70 | cidr_block = "0.0.0.0/0" 71 | gateway_id = aws_internet_gateway.gw.id 72 | } 73 | 74 | tags = { 75 | Name = "public-route-table" 76 | } 77 | } 78 | 79 | resource "aws_route_table_association" "public" { 80 | subnet_id = aws_subnet.public.id 81 | route_table_id = aws_route_table.public.id 82 | } 83 | 84 | resource "aws_route_table" "private" { 85 | vpc_id = aws_vpc.main.id 86 | tags = { 87 | Name = "private-route-table" 88 | } 89 | } 90 | 91 | resource "aws_route_table_association" "private" { 92 | subnet_id = aws_subnet.private.id 93 | route_table_id = aws_route_table.private.id 94 | } 95 | ``` 96 | 97 | ### File: outputs.tf 98 | 99 | This file defines the outputs for the created VPC and subnets. 100 | 101 | ```hcl 102 | # outputs.tf 103 | 104 | output "vpc_id" { 105 | description = "The ID of the VPC" 106 | value = aws_vpc.main.id 107 | } 108 | 109 | output "public_subnet_id" { 110 | description = "The ID of the public subnet" 111 | value = aws_subnet.public.id 112 | } 113 | 114 | output "private_subnet_id" { 115 | description = "The ID of the private subnet" 116 | value = aws_subnet.private.id 117 | } 118 | ``` 119 | 120 | ### How to Use 121 | 122 | 1. **Initialize the Configuration:** 123 | Navigate to the directory containing your `.tf` files and run: 124 | ```sh 125 | terraform init 126 | ``` 127 | 128 | 2. **Plan the Configuration:** 129 | Preview the changes Terraform will make: 130 | ```sh 131 | terraform plan 132 | ``` 133 | 134 | 3. **Apply the Configuration:** 135 | Apply the changes to create the VPC and subnets: 136 | ```sh 137 | terraform apply 138 | ``` 139 | 140 | This setup will create a VPC with a public and a private subnet in the "ap-south-1" region. The public subnet is associated with an Internet Gateway, enabling internet access. The private subnet is isolated without direct internet access. The `outputs.tf` file provides the IDs of the created VPC and subnets for easy reference. 141 | -------------------------------------------------------------------------------- /2024/Terraform_Interview_Questions.md: -------------------------------------------------------------------------------- 1 | Here are some Terraform interview questions categorized into concept-based, deep dive, and scenario-based questions suitable for candidates with over 10 years of experience. 2 | 3 | ### Concept-Based Questions 4 | 5 | 1. **What is Terraform, and how does it fit into the Infrastructure as Code (IaC) paradigm?** 6 | 2. **Can you explain the core components of Terraform and their roles (e.g., providers, resources, modules, state files)?** 7 | 3. **What are the main differences between Terraform and other IaC tools like AWS CloudFormation, Ansible, or Chef?** 8 | 4. **How does Terraform manage state, and why is the state file important?** 9 | 5. **What are Terraform modules, and how do they promote reusability and organization in infrastructure code?** 10 | 6. **Explain the Terraform lifecycle and the purpose of each command (`init`, `plan`, `apply`, `destroy`).** 11 | 7. **What are the benefits of using Terraform for cloud infrastructure management?** 12 | 8. **How do Terraform backends work, and why might you use a remote backend?** 13 | 14 | ### Deep Dive Questions 15 | 16 | 1. **How does Terraform handle dependencies between resources, and how does it ensure the correct order of resource creation?** 17 | 2. **Can you explain the concept of Terraform workspaces and their typical use cases?** 18 | 3. **How do you manage secrets and sensitive data in Terraform configurations?** 19 | 4. **Discuss how Terraform's `terraform import` command works and when you would use it.** 20 | 5. **What strategies do you use to handle Terraform state file management, especially in a team environment?** 21 | 6. **Explain the purpose and use of Terraform's provisioners. When should they be used and when should they be avoided?** 22 | 7. **Describe the concept of "remote state" in Terraform. How do you enable and use it?** 23 | 8. **How does Terraform support multi-cloud deployments, and what are the challenges associated with it?** 24 | 25 | ### Scenario-Based Questions 26 | 27 | 1. **Scenario: Your team is responsible for managing a multi-region deployment. How would you structure your Terraform configuration to support this, and what considerations would you have to take into account?** 28 | 2. **Scenario: During an `apply`, you encounter a cyclic dependency error. How would you troubleshoot and resolve this issue?** 29 | 3. **Scenario: A colleague accidentally committed sensitive data to a Terraform configuration file. What steps would you take to mitigate this?** 30 | 4. **Scenario: You need to deploy an application stack across multiple environments (dev, staging, production). How would you structure your Terraform code to handle this, and what best practices would you follow?** 31 | 5. **Scenario: You need to update a resource that is in use (e.g., changing an RDS instance class). How would you approach this to minimize downtime and ensure a smooth transition?** 32 | 6. **Scenario: After running `terraform apply`, some resources failed to create due to a temporary issue. How would you handle reapplying the configuration to ensure consistency without duplicating resources?** 33 | 7. **Scenario: Your organization wants to start using Terraform Cloud or Terraform Enterprise. How would you migrate your existing Terraform projects to leverage these platforms?** 34 | 8. **Scenario: You are tasked with integrating Terraform with a CI/CD pipeline. Describe your approach and the tools you would use to ensure seamless integration and automation.** 35 | 36 | These questions should help you gauge a candidate's understanding of Terraform concepts, their ability to dive deep into technical details, and their problem-solving skills in real-world scenarios. 37 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/prod/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (production environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module. See [modules/services/webserver-cluster](../../../../modules/services/webserver-cluster). 18 | 19 | ## Using the code 20 | 21 | * Configure your AWS access keys. 22 | 23 | * Initialize working directory. 24 | 25 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 26 | 27 | ```bash 28 | terraform init 29 | ``` 30 | 31 | * Configure Terraform backend. 32 | 33 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 34 | 35 | ```bash 36 | bucket = "" 37 | ``` 38 | 39 | * Configure the bucket used for the database's remote state storage. 40 | 41 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 42 | 43 | ```hcl 44 | variable "db_remote_state_bucket" { 45 | description = "The name of the S3 bucket used for the database's remote state storage" 46 | default = "" 47 | } 48 | ``` 49 | 50 | * Input variables than manage AMI and text the User Data script return. 51 | 52 | There are two input variables defined in `vars.tf` file in Module in order to manage AMI and text the User Data script return: 53 | 54 | ```hcl 55 | variable "ami" { 56 | description = "The AMI to run in the cluster" 57 | default = "ami-785db401" 58 | } 59 | 60 | variable "server_text" { 61 | description = "The text the web server should return" 62 | default = "Hello, World" 63 | } 64 | ``` 65 | 66 | This deploy configure both variables: 67 | 68 | ```hcl 69 | ami = "ami-785db401" 70 | server_text = "Hello, World" 71 | ``` 72 | 73 | * Validate the changes. 74 | 75 | Run command: 76 | 77 | ```bash 78 | terraform plan 79 | ``` 80 | 81 | * Deploy the changes. 82 | 83 | Run command: 84 | 85 | ```bash 86 | terraform apply 87 | ``` 88 | 89 | * Test the cluster of web servers. 90 | 91 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 92 | 93 | ```bash 94 | curl http:/// 95 | ``` 96 | 97 | * Clean up the resources created. 98 | 99 | When you have finished, run command: 100 | 101 | ```bash 102 | terraform destroy 103 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/live/stage/services/webserver-cluster/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Web Server Cluster example (staging environment) 2 | 3 | This folder contains a Web Server Cluster example of a Terraform file [Terraform](https://www.terraform.io/). 4 | 5 | This Terraform file deploys a cluster of web servers on AWS (Amazon Web Services) using EC2 and Auto Scaling, and a load balancer using ELB. 6 | 7 | The cluster of web servers returns "Hello, World" for the URL `/`. The load balancer listens on port 80. 8 | 9 | ## Requirements 10 | 11 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 12 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 13 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 14 | * This code was written for Terraform 0.10.x. 15 | * You must deploy the MySQL database in [data-stores/mysql](../../data-stores/mysql) BEFORE deploying the 16 | templates in this folder. 17 | * This uses a Terraform module. See [modules/services/webserver-cluster](../../../../modules/services/webserver-cluster). 18 | 19 | ## Using the code 20 | 21 | * Configure your AWS access keys. 22 | 23 | * Initialize working directory. 24 | 25 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 26 | 27 | ```bash 28 | terraform init 29 | ``` 30 | 31 | * Configure Terraform backend. 32 | 33 | Modify the S3 bucket name, which is defined in the `bucket` attribute in `backend.tf` file. 34 | 35 | ```bash 36 | bucket = "" 37 | ``` 38 | 39 | * Configure the bucket used for the database's remote state storage. 40 | 41 | Modify the S3 bucket name which is defined in the `bucket` attribute in `vars.tf` file. Important! You must deploy the templates in [data-stores/mysql](../../data-stores/mysql) first: 42 | 43 | ```hcl 44 | variable "db_remote_state_bucket" { 45 | description = "The name of the S3 bucket used for the database's remote state storage" 46 | default = "" 47 | } 48 | ``` 49 | 50 | * Input variables than manage AMI and text the User Data script return. 51 | 52 | There are two input variables defined in `vars.tf` file in Module in order to manage AMI and text the User Data script return: 53 | 54 | ```hcl 55 | variable "ami" { 56 | description = "The AMI to run in the cluster" 57 | default = "ami-785db401" 58 | } 59 | 60 | variable "server_text" { 61 | description = "The text the web server should return" 62 | default = "Hello, World" 63 | } 64 | ``` 65 | 66 | This deploy configure both variables: 67 | 68 | ```hcl 69 | ami = "ami-785db401" 70 | server_text = "New server text" 71 | ``` 72 | 73 | * Validate the changes. 74 | 75 | Run command: 76 | 77 | ```bash 78 | terraform plan 79 | ``` 80 | 81 | * Deploy the changes. 82 | 83 | Run command: 84 | 85 | ```bash 86 | terraform apply 87 | ``` 88 | 89 | * Test the cluster of web servers. 90 | 91 | Test the cluster of web servers. When the `apply` command completes, it will output the DNS name of the load balancer. 92 | 93 | ```bash 94 | curl http:/// 95 | ``` 96 | 97 | * Clean up the resources created. 98 | 99 | When you have finished, run command: 100 | 101 | ```bash 102 | terraform destroy 103 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/13/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Simple If-Else-Statement example 2 | 3 | This folder contains the simple If-Else-Statement example of a [Terraform](https://www.terraform.io/) file on AWS (Amazon Web Services). 4 | 5 | It shows how do a simple If-Else-Statement in Terraform. 6 | 7 | This Terraform file create 3 IAM users, create an IAM policy and attach the IAM policy to the IAM users. 8 | 9 | We use the meta-parameter "count" and two interpolation functions: 10 | 11 | * "${element(LIST, INDEX)}" 12 | * "${length(LIST)}" 13 | 14 | We use "give_neo_cloudwatch_full_access" variable in order to give one of the users, neo, access to CloudWatch: 15 | 16 | * "give_neo_cloudwatch_full_access" = 0 -> "cloudwatch_read_only" 17 | * "give_neo_cloudwatch_full_access" = 1 -> "cloudwatch_full_access" 18 | 19 | ## Requirements 20 | 21 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 22 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 23 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 24 | * This code was written for Terraform 0.10.x. 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 33 | 34 | * The default credentials file 35 | 36 | Set credentials in the AWS credentials profile file on your local system, located at: 37 | 38 | `~/.aws/credentials` on Linux, macOS, or Unix 39 | 40 | `C:\Users\USERNAME\.aws\credentials` on Windows 41 | 42 | This file should contain lines in the following format: 43 | 44 | ```bash 45 | [default] 46 | aws_access_key_id = 47 | aws_secret_access_key = 48 | ``` 49 | Substitute your own AWS credentials values for the values `` and ``. 50 | 51 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 52 | 53 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 54 | 55 | To set these variables on Linux, macOS, or Unix, use `export`: 56 | 57 | ```bash 58 | export AWS_ACCESS_KEY_ID= 59 | export AWS_SECRET_ACCESS_KEY= 60 | ``` 61 | 62 | To set these variables on Windows, use `set`: 63 | 64 | ```bash 65 | set AWS_ACCESS_KEY_ID= 66 | set AWS_SECRET_ACCESS_KEY= 67 | ``` 68 | 69 | * Initialize working directory. 70 | 71 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 72 | 73 | ```bash 74 | terraform init 75 | ``` 76 | 77 | * Validate the changes. 78 | 79 | Run command: 80 | 81 | ```bash 82 | terraform plan 83 | ``` 84 | 85 | * Deploy the changes. 86 | 87 | Run command: 88 | 89 | ```bash 90 | terraform apply 91 | ``` 92 | 93 | * Test the deployment. 94 | 95 | You should see 3 IAM users, an IAM policy and the IAM policy attached to the IAM users. 96 | 97 | * Clean up the resources created. 98 | 99 | When you have finished, run command: 100 | 101 | ```bash 102 | terraform destroy 103 | ``` -------------------------------------------------------------------------------- /2024/Terrafom_Project_1.md: -------------------------------------------------------------------------------- 1 | ### Project Description: Highly Available Web Application Deployment on AWS using Terraform 2 | 3 | #### Project Overview 4 | This project demonstrates the use of Terraform, an open-source Infrastructure as Code (IaC) tool, to deploy a highly available and scalable web application on Amazon Web Services (AWS). The deployment includes creating a Virtual Private Cloud (VPC), subnets, security groups, an internet gateway, a route table, an application load balancer, an auto-scaling group of EC2 instances, and a backend MySQL database using Amazon RDS. 5 | 6 | #### Use Case 7 | The primary use case for this project is to showcase the automation and management of cloud infrastructure using Terraform. This example is particularly useful for demonstrating: 8 | 9 | 1. **Infrastructure as Code (IaC)**: Highlighting how to define and provision AWS infrastructure using code, enabling version control, and collaboration. 10 | 2. **Scalability and High Availability**: Demonstrating how to build a scalable and highly available web application that can handle varying loads by automatically adjusting the number of EC2 instances. 11 | 3. **Security and Networking**: Illustrating best practices in setting up secure and isolated environments using VPC, subnets, and security groups. 12 | 4. **Database Integration**: Showcasing the integration of a managed database service (Amazon RDS) with the application layer. 13 | 14 | ### Components and Their Roles 15 | 1. **VPC and Subnets**: Provide a logically isolated network for our resources. Two subnets in different availability zones are created for high availability. 16 | 2. **Internet Gateway and Route Table**: Enable internet access for resources within the VPC. 17 | 3. **Security Groups**: Control inbound and outbound traffic to ensure only authorized access. 18 | 4. **Application Load Balancer**: Distributes incoming traffic across multiple EC2 instances to ensure availability and reliability. 19 | 5. **Auto Scaling Group**: Automatically adjusts the number of EC2 instances based on demand to ensure the application can handle traffic fluctuations. 20 | 6. **Amazon RDS**: A managed relational database service providing a reliable and scalable database backend for the application. 21 | 22 | ### Step-by-Step Implementation 23 | 1. **Define the AWS Provider**: Configure Terraform to use AWS as the cloud provider. 24 | 2. **Set Up Networking**: Create a VPC, subnets, an internet gateway, and route tables to establish the network architecture. 25 | 3. **Configure Security Groups**: Set up security groups to manage access to the web servers and the database. 26 | 4. **Deploy Load Balancer**: Create an application load balancer to distribute traffic across multiple EC2 instances. 27 | 5. **Create Auto Scaling Group**: Define a launch configuration and auto-scaling group to manage the scaling of EC2 instances. 28 | 6. **Provision RDS Database**: Set up a MySQL database using Amazon RDS for the application backend. 29 | 7. **Deploy and Verify**: Use Terraform to deploy the infrastructure and verify that all components are functioning correctly. 30 | 31 | ### Benefits 32 | - **Automation**: Streamlines the process of deploying and managing cloud infrastructure. 33 | - **Consistency**: Ensures that infrastructure is deployed consistently across different environments. 34 | - **Scalability**: Automatically scales resources to meet demand, improving performance and cost-efficiency. 35 | - **High Availability**: Increases application reliability by distributing resources across multiple availability zones. 36 | - **Security**: Implements best practices for network security and access control. 37 | 38 | This project serves as a practical example of how to leverage Terraform for automating complex AWS infrastructure deployments, making it easier to manage and scale applications in the cloud. 39 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/9/modules/services/webserver-cluster/main.tf: -------------------------------------------------------------------------------- 1 | # Data source: query the list of availability zones 2 | data "aws_availability_zones" "all" {} 3 | 4 | # Data source: DB remote state 5 | data "terraform_remote_state" "db" { 6 | backend = "s3" 7 | 8 | config { 9 | bucket = "${var.db_remote_state_bucket}" 10 | key = "${var.db_remote_state_key}" 11 | region = "eu-west-1" 12 | } 13 | } 14 | 15 | # Data source: Template file 16 | data "template_file" "user_data" { 17 | template = "${file("${path.module}/user-data.sh")}" 18 | 19 | vars { 20 | server_port = "${var.server_port}" 21 | db_address = "${data.terraform_remote_state.db.address}" 22 | db_port = "${data.terraform_remote_state.db.port}" 23 | } 24 | } 25 | 26 | # Create a Security Group for an EC2 instance 27 | resource "aws_security_group" "instance" { 28 | name = "${var.cluster_name}-instance" 29 | 30 | lifecycle { 31 | create_before_destroy = true 32 | } 33 | } 34 | 35 | # Create a Security Group Rule 36 | resource "aws_security_group_rule" "allow_server_http_inbound" { 37 | type = "ingress" 38 | security_group_id = "${aws_security_group.instance.id}" 39 | 40 | from_port = "${var.server_port}" 41 | to_port = "${var.server_port}" 42 | protocol = "tcp" 43 | cidr_blocks = ["0.0.0.0/0"] 44 | 45 | } 46 | 47 | # Create a Security Group for an ELB 48 | resource "aws_security_group" "elb" { 49 | name = "${var.cluster_name}-elb" 50 | } 51 | 52 | # Create a Security Group Rule, inbound 53 | resource "aws_security_group_rule" "allow_http_inbound" { 54 | type = "ingress" 55 | security_group_id = "${aws_security_group.elb.id}" 56 | 57 | from_port = 80 58 | to_port = 80 59 | protocol = "tcp" 60 | cidr_blocks = ["0.0.0.0/0"] 61 | } 62 | 63 | # Create a Security Group Rule, outbound 64 | resource "aws_security_group_rule" "allow_all_outbound" { 65 | type = "egress" 66 | security_group_id = "${aws_security_group.elb.id}" 67 | 68 | from_port = 0 69 | to_port = 0 70 | protocol = "-1" 71 | cidr_blocks = ["0.0.0.0/0"] 72 | } 73 | 74 | # Create a Launch Configuration 75 | resource "aws_launch_configuration" "example" { 76 | image_id = "ami-785db401" 77 | instance_type = "${var.instance_type}" 78 | security_groups = ["${aws_security_group.instance.id}"] 79 | user_data = "${data.template_file.user_data.rendered}" 80 | 81 | lifecycle { 82 | create_before_destroy = true 83 | } 84 | } 85 | 86 | # Create an Autoscaling Group 87 | resource "aws_autoscaling_group" "example" { 88 | launch_configuration = "${aws_launch_configuration.example.id}" 89 | availability_zones = ["${data.aws_availability_zones.all.names}"] 90 | load_balancers = ["${aws_elb.example.name}"] 91 | health_check_type = "ELB" 92 | 93 | min_size = "${var.min_size}" 94 | max_size = "${var.max_size}" 95 | 96 | tag { 97 | key = "Name" 98 | value = "${var.cluster_name}" 99 | propagate_at_launch = true 100 | } 101 | } 102 | 103 | # Create an ELB 104 | resource "aws_elb" "example" { 105 | name = "${var.cluster_name}" 106 | availability_zones = ["${data.aws_availability_zones.all.names}"] 107 | security_groups = ["${aws_security_group.elb.id}"] 108 | 109 | listener { 110 | lb_port = 80 111 | lb_protocol = "http" 112 | instance_port = "${var.server_port}" 113 | instance_protocol = "http" 114 | } 115 | 116 | health_check { 117 | healthy_threshold = 2 118 | unhealthy_threshold = 2 119 | timeout = 3 120 | interval = 30 121 | target = "HTTP:${var.server_port}/" 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /2024/Dependency_in_Terraform.md: -------------------------------------------------------------------------------- 1 | In Terraform, resource dependencies are managed automatically based on the expressions and references within your resource definitions. Terraform uses these references to determine the order of resource creation. However, when resources do not have direct references but still need to be created in a specific order, the `depends_on` meta-argument can be used to explicitly declare dependencies. 2 | 3 | ### Implicit Dependency 4 | Terraform can often infer dependencies automatically when one resource references another. For example, if an EC2 instance depends on a security group, Terraform will understand the dependency by examining the references between resources. 5 | 6 | #### Example of Implicit Dependency: 7 | ```hcl 8 | resource "aws_security_group" "example" { 9 | name = "example-sg" 10 | } 11 | 12 | resource "aws_instance" "example" { 13 | ami = "ami-123456" 14 | instance_type = "t2.micro" 15 | 16 | # Implicit dependency: This instance depends on the security group. 17 | vpc_security_group_ids = [aws_security_group.example.id] 18 | } 19 | ``` 20 | 21 | In the above example: 22 | - The EC2 instance (`aws_instance.example`) references the security group (`aws_security_group.example.id`). 23 | - Terraform automatically detects that the security group must be created before the EC2 instance because of this reference. 24 | 25 | ### Explicit Dependency with `depends_on` 26 | Sometimes, resources might not have direct references, but you still want to enforce a specific order. In such cases, you can use the `depends_on` argument to explicitly define dependencies. 27 | 28 | #### Example of Explicit Dependency: 29 | ```hcl 30 | resource "aws_s3_bucket" "example" { 31 | bucket = "my-bucket" 32 | } 33 | 34 | resource "aws_instance" "example" { 35 | ami = "ami-123456" 36 | instance_type = "t2.micro" 37 | 38 | # Explicit dependency: Ensure the S3 bucket is created before the instance. 39 | depends_on = [aws_s3_bucket.example] 40 | } 41 | ``` 42 | 43 | In this case: 44 | - There is no direct reference between the S3 bucket and the EC2 instance, but by using `depends_on`, you are telling Terraform to create the S3 bucket before the EC2 instance. 45 | - This is useful when there's a logical dependency (e.g., maybe the EC2 instance needs to use the bucket after being created), even though Terraform cannot infer it automatically. 46 | 47 | ### How Terraform Resolves Dependencies: 48 | 1. **Graph-Based Planning**: Terraform builds a dependency graph from the configuration. Each resource becomes a node in the graph, and edges between nodes represent the dependencies. 49 | 2. **Execution**: Terraform walks through the graph, ensuring resources are created or modified in the correct order, respecting both implicit and explicit dependencies. 50 | 51 | ### Key Points: 52 | - **Implicit dependencies** are detected when one resource references another through attributes like `id`. 53 | - **Explicit dependencies** can be defined using the `depends_on` meta-argument when there's no direct reference but a logical dependency exists. 54 | 55 | ### Example Use Case: 56 | If you have a web server (`aws_instance`) that relies on an RDS database (`aws_db_instance`), you might not have direct references, but you know the database must be set up before the web server starts. Here, you could use `depends_on` to enforce this order: 57 | 58 | ```hcl 59 | resource "aws_db_instance" "example" { 60 | allocated_storage = 20 61 | engine = "mysql" 62 | instance_class = "db.t2.micro" 63 | name = "mydb" 64 | } 65 | 66 | resource "aws_instance" "web" { 67 | ami = "ami-123456" 68 | instance_type = "t2.micro" 69 | 70 | # Ensure the database is created first 71 | depends_on = [aws_db_instance.example] 72 | } 73 | ``` 74 | 75 | This ensures that Terraform creates the database before the web server, even though there's no direct attribute reference between them. 76 | -------------------------------------------------------------------------------- /2024/What is Terraform.md: -------------------------------------------------------------------------------- 1 | Terraform is an open-source infrastructure as code (IaC) software tool created by HashiCorp. It allows users to define and provision data center infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform automates the setup of infrastructure across a variety of service providers, including popular cloud platforms such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP), as well as many others. 2 | 3 | ### Key Features of Terraform 4 | 5 | 1. **Infrastructure as Code (IaC):** 6 | - Terraform uses HCL to describe infrastructure in a declarative way. This allows you to write code to manage configurations, making it easier to version, share, and reuse configurations. 7 | 8 | 2. **Provider Agnostic:** 9 | - Terraform supports a wide range of cloud providers and services, making it versatile for managing multi-cloud environments and hybrid cloud setups. 10 | 11 | 3. **Plan and Apply:** 12 | - Terraform's `terraform plan` command previews the changes that will be made to the infrastructure, allowing for review and approval before applying changes. 13 | - The `terraform apply` command applies the planned changes to the infrastructure. 14 | 15 | 4. **State Management:** 16 | - Terraform keeps track of the infrastructure state using state files. These files store metadata about the infrastructure and help Terraform to determine what changes need to be applied. 17 | 18 | 5. **Modules:** 19 | - Reusable modules enable users to encapsulate and share pieces of configuration, promoting reuse and reducing duplication of code. 20 | 21 | 6. **Resource Graph:** 22 | - Terraform builds a dependency graph of all resources, enabling parallel execution of non-dependent resources, which speeds up provisioning and ensures the correct order of operations. 23 | 24 | 7. **Provisioners and Backends:** 25 | - Provisioners can be used to execute scripts or commands on a local or remote machine as part of resource creation or destruction. 26 | - Backends allow for remote storage of state files, enabling collaboration and more robust state management. 27 | 28 | ### Example of Terraform Usage 29 | 30 | Here's a simple example of a Terraform configuration file (`main.tf`) for provisioning an AWS EC2 instance: 31 | 32 | ```hcl 33 | provider "aws" { 34 | region = "us-west-2" 35 | } 36 | 37 | resource "aws_instance" "example" { 38 | ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI 39 | instance_type = "t2.micro" 40 | 41 | tags = { 42 | Name = "ExampleInstance" 43 | } 44 | } 45 | ``` 46 | 47 | ### Workflow 48 | 49 | 1. **Write Configuration:** 50 | - Define the desired infrastructure in `.tf` files using HCL. 51 | 52 | 2. **Initialize:** 53 | - Run `terraform init` to initialize the configuration and download necessary providers. 54 | 55 | 3. **Plan:** 56 | - Execute `terraform plan` to see a preview of the changes Terraform will make. 57 | 58 | 4. **Apply:** 59 | - Apply the configuration changes with `terraform apply`. 60 | 61 | 5. **Manage:** 62 | - Continue to use Terraform to update and manage the infrastructure, using `terraform plan` and `terraform apply` as needed. 63 | 64 | ### Benefits of Using Terraform 65 | 66 | - **Consistency:** Ensures consistent configuration across environments. 67 | - **Version Control:** Configurations can be stored in version control systems like Git, allowing for change tracking and collaboration. 68 | - **Scalability:** Manages both small and large-scale infrastructures efficiently. 69 | - **Automated Infrastructure Management:** Reduces manual intervention, minimizing errors and increasing efficiency. 70 | 71 | ### Conclusion 72 | 73 | Terraform is a powerful tool that simplifies the process of managing infrastructure through code. Its provider-agnostic nature and strong community support make it an ideal choice for modern DevOps practices. 74 | -------------------------------------------------------------------------------- /2024Terraform_Introduction.md: -------------------------------------------------------------------------------- 1 | Terraform is an open-source infrastructure as code (IaC) software tool created by HashiCorp. It allows users to define and provision data center infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform automates the setup of infrastructure across a variety of service providers, including popular cloud platforms such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP), as well as many others. 2 | 3 | ### Key Features of Terraform 4 | 5 | 1. **Infrastructure as Code (IaC):** 6 | - Terraform uses HCL to describe infrastructure in a declarative way. This allows you to write code to manage configurations, making it easier to version, share, and reuse configurations. 7 | 8 | 2. **Provider Agnostic:** 9 | - Terraform supports a wide range of cloud providers and services, making it versatile for managing multi-cloud environments and hybrid cloud setups. 10 | 11 | 3. **Plan and Apply:** 12 | - Terraform's `terraform plan` command previews the changes that will be made to the infrastructure, allowing for review and approval before applying changes. 13 | - The `terraform apply` command applies the planned changes to the infrastructure. 14 | 15 | 4. **State Management:** 16 | - Terraform keeps track of the infrastructure state using state files. These files store metadata about the infrastructure and help Terraform to determine what changes need to be applied. 17 | 18 | 5. **Modules:** 19 | - Reusable modules enable users to encapsulate and share pieces of configuration, promoting reuse and reducing duplication of code. 20 | 21 | 6. **Resource Graph:** 22 | - Terraform builds a dependency graph of all resources, enabling parallel execution of non-dependent resources, which speeds up provisioning and ensures the correct order of operations. 23 | 24 | 7. **Provisioners and Backends:** 25 | - Provisioners can be used to execute scripts or commands on a local or remote machine as part of resource creation or destruction. 26 | - Backends allow for remote storage of state files, enabling collaboration and more robust state management. 27 | 28 | ### Example of Terraform Usage 29 | 30 | Here's a simple example of a Terraform configuration file (`main.tf`) for provisioning an AWS EC2 instance: 31 | 32 | ```hcl 33 | provider "aws" { 34 | region = "us-west-2" 35 | } 36 | 37 | resource "aws_instance" "example" { 38 | ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI 39 | instance_type = "t2.micro" 40 | 41 | tags = { 42 | Name = "ExampleInstance" 43 | } 44 | } 45 | ``` 46 | 47 | ### Workflow 48 | 49 | 1. **Write Configuration:** 50 | - Define the desired infrastructure in `.tf` files using HCL. 51 | 52 | 2. **Initialize:** 53 | - Run `terraform init` to initialize the configuration and download necessary providers. 54 | 55 | 3. **Plan:** 56 | - Execute `terraform plan` to see a preview of the changes Terraform will make. 57 | 58 | 4. **Apply:** 59 | - Apply the configuration changes with `terraform apply`. 60 | 61 | 5. **Manage:** 62 | - Continue to use Terraform to update and manage the infrastructure, using `terraform plan` and `terraform apply` as needed. 63 | 64 | ### Benefits of Using Terraform 65 | 66 | - **Consistency:** Ensures consistent configuration across environments. 67 | - **Version Control:** Configurations can be stored in version control systems like Git, allowing for change tracking and collaboration. 68 | - **Scalability:** Manages both small and large-scale infrastructures efficiently. 69 | - **Automated Infrastructure Management:** Reduces manual intervention, minimizing errors and increasing efficiency. 70 | 71 | ### Conclusion 72 | 73 | Terraform is a powerful tool that simplifies the process of managing infrastructure through code. Its provider-agnostic nature and strong community support make it an ideal choice for modern DevOps practices. 74 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/14/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Zero-Downtime Deployment example 2 | 3 | This folder contains a zero-downtime deployment example of a [Terraform](https://www.terraform.io/) installed on your computer file on AWS (Amazon Web Services). 4 | 5 | It shows how to deploy a new AMI across the cluster when you have made changes to your code. In addition, you control the text the User Data script return from its one-liner HTTP server. 6 | 7 | Furthermore, it shows how to develop (not duplicating code) web server clusters in different environments using a module. 8 | 9 | The environments are: 10 | 11 | * Staging (stage) 12 | * Production (prod) 13 | 14 | This is the file layout in this repo: 15 | 16 | ```bash 17 | live 18 | ├── global 19 | │ └── s3/ 20 | │ ├── main.tf 21 | │ └── (etc) 22 | │ 23 | ├── stage 24 | │ ├── services/ 25 | │ │ └── webserver-cluster/ 26 | │ │ ├── main.tf 27 | │ │ └── (etc) 28 | │ └── data-stores/ 29 | │ └── mysql/ 30 | │ ├── main.tf 31 | │ └── (etc) 32 | │ 33 | └── prod 34 | ├── services/ 35 | │ └── webserver-cluster/ 36 | │ ├── main.tf 37 | │ └── (etc) 38 | └── data-stores/ 39 | └── mysql/ 40 | ├── main.tf 41 | └── (etc) 42 | 43 | modules 44 | └── services/ 45 | └── webserver-cluster/ 46 | ├── main.tf 47 | └── (etc) 48 | ``` 49 | 50 | It uses in common for both environments: 51 | 52 | * Terraform Remote State example: [live/global/s3](live/global/s3) 53 | * Terraform Web Server Cluster module example: [modules/services/webserver-cluster](modules/services/webserver-cluster) 54 | 55 | It uses for staging environment: 56 | 57 | * Terraform MySQL on RDS example (staging environment): [live/stage/data-stores/mysql](live/stage/data-stores/mysql) 58 | * Terraform Web Server Cluster example (staging environment): [live/stage/services/webserver-cluster](live/stage/services/webserver-cluster) 59 | 60 | It uses for production environment: 61 | 62 | * Terraform MySQL on RDS example (production environment): [live/prod/data-stores/mysql](live/prod/data-stores/mysql) 63 | * Terraform Web Server Cluster example (production environment): [live/prod/services/webserver-cluster](live/prod/services/webserver-cluster) 64 | 65 | ## Requirements 66 | 67 | * You must have [Terraform](https://www.terraform.io/) installed on your computer. 68 | * You must have an [AWS (Amazon Web Services)](http://aws.amazon.com/) account. 69 | * It uses the Terraform AWS Provider that interacts with the many resources supported by AWS through its APIs. 70 | * This code was written for Terraform 0.10.x. 71 | 72 | ## Using the code 73 | 74 | Configure your AWS access keys. 75 | 76 | Use Terraform Remote State example for creating the remote state bucket. See: [live/global/s3](live/global/s3) 77 | 78 | Use Terraform module example for Web Server Cluster example in the staging environment and Web Server Cluster example in the production environment. See: [modules/services/webserver-cluster](modules/services/webserver-cluster) 79 | 80 | Use Terraform MySQL on RDS example for creating a MySQL database in the staging environment. See: [live/stage/data-stores/mysql](live/stage/data-stores/mysql) 81 | 82 | Use Terraform Web Server Cluster example for creating a web server cluster in the staging environment. See: [live/stage/services/webserver-cluster](live/stage/services/webserver-cluster) 83 | 84 | Use Terraform MySQL on RDS example for creating a MySQL database in the production environment. See: [live/prod/data-stores/mysql](live/prod/data-stores/mysql) 85 | 86 | Use Terraform Web Server Cluster example for creating a web server cluster in the production environment. See: [live/prod/services/webserver-cluster](live/prod/services/webserver-cluster) 87 | -------------------------------------------------------------------------------- /2024/What is Outputs_tf.md: -------------------------------------------------------------------------------- 1 | The `outputs.tf` file in Terraform is used to define outputs, which are a way to extract and display useful information about the resources that Terraform creates. Outputs can be helpful for a variety of reasons: 2 | 3 | 1. **Debugging and Verification:** 4 | - Outputs allow you to verify that resources have been created correctly and with the expected properties. For example, you can check the IDs of resources or the IP addresses assigned to instances. 5 | 6 | 2. **Reference in Other Terraform Configurations:** 7 | - When you use Terraform modules or work with multiple Terraform configurations, outputs can be used to pass information between different configurations. This makes it easier to build complex infrastructure setups by composing smaller, reusable modules. 8 | 9 | 3. **Automation and Integration:** 10 | - Outputs can be used in scripts or CI/CD pipelines to get information about the infrastructure. For example, after deploying an application, you might need the public IP address of a server to run integration tests or update DNS records. 11 | 12 | 4. **Documentation and Reporting:** 13 | - Outputs provide an easy way to document the important parts of your infrastructure. This can be useful for reports or for sharing information with team members who need to know certain details about the deployment. 14 | 15 | Here is an example of the `outputs.tf` file provided earlier, which defines outputs for the VPC and subnets: 16 | 17 | ```hcl 18 | # outputs.tf 19 | 20 | output "vpc_id" { 21 | description = "The ID of the VPC" 22 | value = aws_vpc.main.id 23 | } 24 | 25 | output "public_subnet_id" { 26 | description = "The ID of the public subnet" 27 | value = aws_subnet.public.id 28 | } 29 | 30 | output "private_subnet_id" { 31 | description = "The ID of the private subnet" 32 | value = aws_subnet.private.id 33 | } 34 | ``` 35 | 36 | ### Detailed Explanation of Outputs 37 | 38 | - **output "vpc_id":** 39 | - **description:** Provides a human-readable description of what the output represents. This is useful for documentation purposes. 40 | - **value:** Specifies the actual value to output. In this case, it is the ID of the VPC created by the `aws_vpc.main` resource. 41 | 42 | - **output "public_subnet_id":** 43 | - **description:** Describes the output as the ID of the public subnet. 44 | - **value:** The ID of the public subnet created by the `aws_subnet.public` resource. 45 | 46 | - **output "private_subnet_id":** 47 | - **description:** Describes the output as the ID of the private subnet. 48 | - **value:** The ID of the private subnet created by the `aws_subnet.private` resource. 49 | 50 | ### Using the Outputs 51 | 52 | After running `terraform apply`, you will see the output values in the terminal. For example: 53 | 54 | ```sh 55 | Apply complete! Resources: 4 added, 0 changed, 0 destroyed. 56 | 57 | Outputs: 58 | 59 | private_subnet_id = "subnet-0abcd1234efgh5678" 60 | public_subnet_id = "subnet-0abcd1234ijkl9012" 61 | vpc_id = "vpc-0abcd1234mnop5678" 62 | ``` 63 | 64 | These output values can then be used in various ways, such as: 65 | 66 | - **In Another Terraform Configuration:** 67 | - You can reference these outputs in another Terraform configuration by using the `terraform_remote_state` data source. 68 | 69 | - **In Scripts:** 70 | - You can use these values in shell scripts or other automation tools to perform tasks that depend on the created infrastructure. 71 | 72 | For example, to reference the VPC ID in another Terraform configuration: 73 | 74 | ```hcl 75 | data "terraform_remote_state" "vpc" { 76 | backend = "s3" 77 | config = { 78 | bucket = "kumarnewbucket" 79 | key = "test/terraform.tfstate" 80 | region = "ap-south-1" 81 | } 82 | } 83 | 84 | resource "aws_instance" "example" { 85 | ami = "ami-06f621d90fa29f6d0" 86 | instance_type = "t2.micro" 87 | subnet_id = data.terraform_remote_state.vpc.outputs.public_subnet_id 88 | } 89 | ``` 90 | 91 | This approach helps you to keep your Terraform configurations modular and maintainable. 92 | -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/6/README.md: -------------------------------------------------------------------------------- 1 | # Terraform Create S3 bucket 2 | 3 | This Terraform file deploys the creation of a S3 bucket on AWS (Amazon Web Services). 4 | 5 | ## Using the code 6 | 7 | * Configure your AWS access keys. 8 | 9 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 10 | 11 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 12 | 13 | * The default credentials file 14 | 15 | Set credentials in the AWS credentials profile file on your local system, located at: 16 | 17 | `~/.aws/credentials` on Linux, macOS, or Unix 18 | 19 | `C:\Users\USERNAME\.aws\credentials` on Windows 20 | 21 | This file should contain lines in the following format: 22 | 23 | ```bash 24 | [default] 25 | aws_access_key_id = 26 | aws_secret_access_key = 27 | ``` 28 | Substitute your own AWS credentials values for the values `` and ``. 29 | 30 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 31 | 32 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 33 | 34 | To set these variables on Linux, macOS, or Unix, use `export`: 35 | 36 | ```bash 37 | export AWS_ACCESS_KEY_ID= 38 | export AWS_SECRET_ACCESS_KEY= 39 | ``` 40 | 41 | To set these variables on Windows, use `set`: 42 | 43 | ```bash 44 | set AWS_ACCESS_KEY_ID= 45 | set AWS_SECRET_ACCESS_KEY= 46 | ``` 47 | 48 | * Initialize working directory. 49 | 50 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 51 | 52 | ```bash 53 | terraform init 54 | ``` 55 | 56 | * Configure S3 bucket name. 57 | 58 | You must modify the S3 bucket name, which is defined as an input variable `bucket_name` in `vars.tf` file. 59 | 60 | You can modify the S3 bucket name in several ways: 61 | 62 | * Loading variables from command line flags. 63 | 64 | Run Terraform commands in this way: 65 | 66 | ```bash 67 | terraform plan -var 'bucket_name=' 68 | ``` 69 | 70 | ```bash 71 | terraform apply -var 'bucket_name=' 72 | ``` 73 | 74 | * Loading variables from a file. 75 | 76 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 77 | 78 | ```bash 79 | bucket_name = "" 80 | ``` 81 | 82 | * Loading variables from environment variables. 83 | 84 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_bucket_name`: 85 | 86 | ```bash 87 | TF_VAR_bucket_name= 88 | ``` 89 | 90 | * Variable defaults. 91 | 92 | Change the value of the `default` attribute of `bucket_name` input variable in `vars.tf` file. 93 | 94 | ```hcl 95 | variable "bucket_name" { 96 | description = "The name of the S3 bucket. Must be globally unique." 97 | default = "" 98 | } 99 | ``` 100 | 101 | * Validate the changes. 102 | 103 | Run command: 104 | 105 | ```bash 106 | terraform plan 107 | ``` 108 | 109 | * Deploy the changes. 110 | 111 | Run command: 112 | 113 | ```bash 114 | terraform apply 115 | ``` 116 | 117 | * Test the deploy. 118 | 119 | When the `terraform apply` command completes, use the AWS console, you should see the new S3 bucket created. 120 | 121 | * Clean up the resources created. 122 | 123 | When you have finished, run command: 124 | 125 | ```bash 126 | terraform destroy 127 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/4/README.md: -------------------------------------------------------------------------------- 1 | This Terraform file deploys a single web server on AWS (Amazon Web Services). The web server returns "Hello, World" for the URL `/` listening on port 8080, which is defined as a variable. 2 | 3 | ## Using the code 4 | 5 | * Configure your AWS access keys. 6 | 7 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 8 | 9 | Setting your credentials for use by Terraform can be done in a number of ways, but here are the recommended approaches: 10 | 11 | * The default credentials file 12 | 13 | Set credentials in the AWS credentials profile file on your local system, located at: 14 | 15 | `~/.aws/credentials` on Linux, macOS, or Unix 16 | 17 | `C:\Users\USERNAME\.aws\credentials` on Windows 18 | 19 | This file should contain lines in the following format: 20 | 21 | ```bash 22 | [default] 23 | aws_access_key_id = 24 | aws_secret_access_key = 25 | ``` 26 | Substitute your own AWS credentials values for the values `` and ``. 27 | 28 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` 29 | 30 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 31 | 32 | To set these variables on Linux, macOS, or Unix, use `export`: 33 | 34 | ```bash 35 | export AWS_ACCESS_KEY_ID= 36 | export AWS_SECRET_ACCESS_KEY= 37 | ``` 38 | 39 | To set these variables on Windows, use `set`: 40 | 41 | ```bash 42 | set AWS_ACCESS_KEY_ID= 43 | set AWS_SECRET_ACCESS_KEY= 44 | ``` 45 | 46 | * Initialize working directory. 47 | 48 | The first command that should be run after writing a new Terraform configuration is the `terraform init` command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times. 49 | 50 | ```bash 51 | terraform init 52 | ``` 53 | 54 | * Modify server port configuration. 55 | 56 | The web server is listening on port 8080, which is defined as an input variable `server_port` in `vars.tf` file. 57 | 58 | If you want to modify the server port you will be able to do it in several ways: 59 | 60 | * Loading variables from command line flags. 61 | 62 | Run Terraform commands in this way: 63 | 64 | ```bash 65 | terraform plan -var 'server_port=8080' 66 | ``` 67 | 68 | ```bash 69 | terraform apply -var 'server_port=8080' 70 | ``` 71 | 72 | * Loading variables from a file. 73 | 74 | When Terraform runs it will look for a file called `terraform.tfvars`. You can populate this file with variable values that will be loaded when Terraform runs. An example for the content of the `terraform.tfvars` file: 75 | 76 | ```bash 77 | server_port = "8080" 78 | ``` 79 | 80 | * Loading variables from environment variables. 81 | 82 | Terraform will also parse any environment variables that are prefixed with `TF_VAR`. You can create an environment variable `TF_VAR_server_port`: 83 | 84 | ```bash 85 | TF_VAR_server_port=8080 86 | ``` 87 | 88 | * Variable defaults. 89 | 90 | Change the value of the `default` attribute of `server_port` input variable in `vars.tf` file. 91 | 92 | * Validate the changes. 93 | 94 | Run command: 95 | 96 | ```bash 97 | terraform plan 98 | ``` 99 | 100 | * Deploy the changes. 101 | 102 | Run command: 103 | 104 | ```bash 105 | terraform apply 106 | ``` 107 | 108 | * Test the web server. 109 | 110 | You can test it in two ways: 111 | 112 | * Running this command: 113 | 114 | ```bash 115 | curl http://:8080/ 116 | ``` 117 | 118 | * Writing in your browser this URL: `http://:8080/` 119 | 120 | You should get a `Hello, World` response message. 121 | 122 | * Clean up the resources created. 123 | 124 | When you have finished, run command: 125 | 126 | ```bash 127 | terraform destroy 128 | ``` -------------------------------------------------------------------------------- /terraform-aws-examples-master/My_Terraform_AWS_Examples/AWS_Examples/labs/AWS_Example_Labs: -------------------------------------------------------------------------------- 1 | 2 | Lab#1 : Terraform code to deploy a VPC in AWS: 3 | 4 | ***************** 5 | 6 | >>>>>>>>>>>>>>> 7 | 8 | provider "aws" { 9 | region = "us-east-1" # Specify the desired AWS region 10 | } 11 | 12 | resource "aws_vpc" "my_vpc" { 13 | cidr_block = "10.0.0.0/16" # Specify the desired CIDR block for the VPC 14 | 15 | tags = { 16 | Name = "MyVPC" # Provide a name for the VPC 17 | } 18 | } 19 | 20 | resource "aws_subnet" "my_subnet" { 21 | vpc_id = aws_vpc.my_vpc.id 22 | cidr_block = "10.0.0.0/24" # Specify the desired CIDR block for the subnet 23 | availability_zone = "us-east-1a" # Specify the desired availability zone 24 | 25 | tags = { 26 | Name = "MySubnet" # Provide a name for the subnet 27 | } 28 | } 29 | 30 | resource "aws_internet_gateway" "my_igw" { 31 | vpc_id = aws_vpc.my_vpc.id 32 | 33 | tags = { 34 | Name = "MyIGW" # Provide a name for the internet gateway 35 | } 36 | } 37 | 38 | resource "aws_route_table" "my_route_table" { 39 | vpc_id = aws_vpc.my_vpc.id 40 | 41 | route { 42 | cidr_block = "0.0.0.0/0" 43 | gateway_id = aws_internet_gateway.my_igw.id 44 | } 45 | 46 | tags = { 47 | Name = "MyRouteTable" # Provide a name for the route table 48 | } 49 | } 50 | 51 | resource "aws_route_table_association" "my_route_table_association" { 52 | subnet_id = aws_subnet.my_subnet.id 53 | route_table_id = aws_route_table.my_route_table.id 54 | } 55 | 56 | <<<<<<<<<<<<<<<<<< 57 | 58 | 59 | In this example, we're using the AWS provider and creating the following resources: 60 | 61 | aws_vpc: Creates a VPC with the specified CIDR block. 62 | aws_subnet: Creates a subnet within the VPC with the specified CIDR block and availability zone. 63 | aws_internet_gateway: Creates an internet gateway and attaches it to the VPC. 64 | aws_route_table: Creates a route table for the VPC and adds a route to the internet gateway. 65 | aws_route_table_association: Associates the subnet with the route table. 66 | 67 | Make sure to replace the desired values for region, CIDR blocks, availability zones, and names according to your requirements. 68 | You'll also need to configure your AWS credentials before running this Terraform code. 69 | 70 | **************************** 71 | 72 | Lab2: Terraform code to deploy a VPC, subnet, and an EC2 instance in AWS: 73 | 74 | >>>>>>>>>>>>>>>> 75 | 76 | provider "aws" { 77 | region = "us-east-1" # Specify the desired AWS region 78 | } 79 | 80 | resource "aws_vpc" "my_vpc" { 81 | cidr_block = "10.0.0.0/16" # Specify the desired CIDR block for the VPC 82 | 83 | tags = { 84 | Name = "MyVPC" # Provide a name for the VPC 85 | } 86 | } 87 | 88 | resource "aws_subnet" "my_subnet" { 89 | vpc_id = aws_vpc.my_vpc.id 90 | cidr_block = "10.0.0.0/24" # Specify the desired CIDR block for the subnet 91 | availability_zone = "us-east-1a" # Specify the desired availability zone 92 | 93 | tags = { 94 | Name = "MySubnet" # Provide a name for the subnet 95 | } 96 | } 97 | 98 | resource "aws_instance" "my_ec2_instance" { 99 | ami = "ami-0c94855ba95c71c99" # Specify the desired AMI ID 100 | instance_type = "t2.micro" # Specify the desired instance type 101 | subnet_id = aws_subnet.my_subnet.id 102 | key_name = "my_key_pair" # Specify the name of your key pair 103 | 104 | tags = { 105 | Name = "MyEC2Instance" # Provide a name for the EC2 instance 106 | } 107 | } 108 | 109 | <<<<<<<<<<<<<<< 110 | 111 | In this example, we're using the AWS provider and creating the following resources: 112 | 113 | aws_vpc: Creates a VPC with the specified CIDR block. 114 | aws_subnet: Creates a subnet within the VPC with the specified CIDR block and availability zone. 115 | aws_instance: Launches an EC2 instance in the specified subnet, using the specified AMI, instance type, and key pair. 116 | 117 | Make sure to replace the desired values for region, CIDR blocks, availability zones, AMI ID, instance type, key pair name, 118 | and names according to your requirements. Don't forget to configure your AWS credentials before running this Terraform code. 119 | --------------------------------------------------------------------------------