├── .gitignore
├── LICENSE
├── README.md
├── conditional-expressions
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── count
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── custom-environment-variable
├── main.tf
├── providers.tf
└── variables.tf
├── data-source-life-cycle
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── data-source
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── dynamic-block
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── for-expressions
├── main.tf
├── terraform.tfvars
└── variables.tf
├── for_each_map
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── for_each_toset
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── images
└── terraform.png
├── input-variables-validation
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── lifecycle
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── local
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── localstack
└── docker-compose.yml
├── output
├── main.tf
├── outputs.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── provisioner-file
├── file.txt
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── provisioner-local-exec
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── provisioner-remote-exec
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
├── remote-backend-s3
└── providers.tf
├── resource
├── main.tf
└── providers.tf
├── splat-expressions
├── main.tf
├── terraform.tfvars
└── variables.tf
├── template-strings-for
├── main.tf
├── terraform.tfvars
└── variables.tf
├── template-strings-if-else
├── main.tf
└── providers.tf
└── variables
├── main.tf
├── providers.tf
├── terraform.tfvars
└── variables.tf
/.gitignore:
--------------------------------------------------------------------------------
1 | # Local .terraform directories
2 | **/.terraform/*
3 |
4 | # .tfstate files
5 | *.tfstate
6 | *.tfstate.*
7 |
8 | # Crash log files
9 | crash.log
10 |
11 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most
12 | # .tfvars files are managed as part of configuration and so should be included in
13 | # version control.
14 | #
15 | # example.tfvars
16 |
17 | # Ignore override files as they are usually used to override resources locally and so
18 | # are not checked in
19 | override.tf
20 | override.tf.json
21 | *_override.tf
22 | *_override.tf.json
23 |
24 | # Include override files you do wish to add to version control using negated pattern
25 | #
26 | # !example_override.tf
27 |
28 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
29 | # example: *tfplan*
30 |
31 | # Ignore .terraform.local.hcl
32 | .terraform.lock.hcl
33 |
34 | # Ignore .DS_Store
35 | .DS_Store
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Mahdi Abbasi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Terraform Tutorial
2 |
3 |
4 |
5 |
6 |
7 | I have written some examples for each concept in Terraform:
8 |
9 | - [conditional-expressions](conditional-expressions)
10 | - [count](count)
11 | - [custom-environment-variable](custom-environment-variable)
12 | - [data-source](data-source)
13 | - [data-source-life-cycle](data-source-life-cycle)
14 | - [dynamic-block](dynamic-block)
15 | - [for_each_map](for_each_map)
16 | - [for_each_toset](for_each_toset)
17 | - [for-expressions](for-expressions)
18 | - [input-variables-validation](input-variables-validation)
19 | - [lifecycle](lifecycle)
20 | - [local](local)
21 | - [output](output)
22 | - [provisioner-file](provisioner-file)
23 | - [provisioner-local-exec](provisioner-local-exec)
24 | - [provisioner-remote-exec](provisioner-remote-exec)
25 | - [remote-backend-s3](remote-backend-s3)
26 | - [resource](resource)
27 | - [splat-expressions](splat-expressions)
28 | - [template-strings-for](template-strings-for)
29 | - [template-strings-if-else](template-strings-if-else)
30 | - [variables](variables)
31 |
32 | Also I have added docker-compose for localstack:
33 |
34 | - [localstack](localstack)
35 |
36 | You can find the full documentation of Terraform in the link below:
37 |
38 | [Terraform Documentation](https://developer.hashicorp.com/terraform/docs)
--------------------------------------------------------------------------------
/conditional-expressions/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type != "" ? var.instance_type : "t2.micro"
11 |
12 | tags = {
13 | "Name" = "ec2-${var.environment}"
14 | }
15 | }
--------------------------------------------------------------------------------
/conditional-expressions/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/conditional-expressions/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/conditional-expressions/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | }
5 |
6 | variable "environment" {
7 | type = string
8 | description = "Environment"
9 | }
--------------------------------------------------------------------------------
/count/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | count = 2
10 |
11 | ami = data.aws_ami.busybox.id
12 | instance_type = var.instance_type
13 |
14 | tags = {
15 | "Name" = "ec2-${var.environment}"
16 | "index" = "ec2-${count.index}"
17 | }
18 | }
--------------------------------------------------------------------------------
/count/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/count/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/count/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------
/custom-environment-variable/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | tags = {
13 | "Name" = "ec2-${var.environment}"
14 | }
15 | }
--------------------------------------------------------------------------------
/custom-environment-variable/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/custom-environment-variable/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------
/data-source-life-cycle/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "ami" {
2 | filter {
3 | name = "name"
4 | values = [var.ami_name]
5 | }
6 |
7 | lifecycle {
8 | postcondition {
9 | condition = self.name == "busybox"
10 | error_message = "The AMI name should be busybox"
11 | }
12 | }
13 | }
14 |
15 | resource "aws_instance" "ec2" {
16 | ami = data.aws_ami.ami.id
17 | instance_type = var.instance_type
18 |
19 | tags = {
20 | "Name" = "ec2"
21 | }
22 | }
--------------------------------------------------------------------------------
/data-source-life-cycle/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/data-source-life-cycle/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | ami_name = "busybox"
--------------------------------------------------------------------------------
/data-source-life-cycle/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "ami_name" {
8 | type = string
9 | description = "AMI Name"
10 | default = "busybox"
11 | }
--------------------------------------------------------------------------------
/data-source/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | tags = {
13 | "Name" = "ec2"
14 | }
15 | }
--------------------------------------------------------------------------------
/data-source/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/data-source/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
--------------------------------------------------------------------------------
/data-source/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
--------------------------------------------------------------------------------
/dynamic-block/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_security_group" "my_sg" {
9 | name = "my-sg"
10 |
11 | dynamic "ingress" {
12 | for_each = var.security_groups_list
13 | content {
14 | from_port = ingress.value["port"]
15 | to_port = ingress.value["port"]
16 | protocol = ingress.value["protocol"]
17 | cidr_blocks = ["0.0.0.0/0"]
18 | }
19 | }
20 | }
21 |
22 | resource "aws_instance" "ec2" {
23 | ami = data.aws_ami.busybox.id
24 | instance_type = var.instance_type
25 |
26 | security_groups = [aws_security_group.my_sg.name]
27 |
28 | tags = {
29 | "Name" = "ec2-${var.environment}"
30 | }
31 | }
--------------------------------------------------------------------------------
/dynamic-block/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/dynamic-block/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
3 | security_groups_list = [{
4 | port = 443
5 | protocol = "tcp"
6 | },
7 | {
8 | port = 80
9 | protocol = "tcp"
10 | }]
--------------------------------------------------------------------------------
/dynamic-block/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
11 |
12 | variable "security_groups_list" {
13 | type = list(object({
14 | port = number
15 | protocol = string
16 | }))
17 | }
--------------------------------------------------------------------------------
/for-expressions/main.tf:
--------------------------------------------------------------------------------
1 | output "uppercase_list" {
2 | value = [for item in var.list : upper(item)]
3 | }
4 |
5 | output "uppercase_list_with_key" {
6 | value = [for key, value in var.list : "${key} is ${value}"]
7 | }
8 |
9 | output "uppercase_object" {
10 | value = { for item in var.list : item => upper(item) }
11 | }
12 |
13 | output "uppercase_object_with_condition" {
14 | value = { for item in var.list : item => upper(item) if item != "string2" }
15 | }
--------------------------------------------------------------------------------
/for-expressions/terraform.tfvars:
--------------------------------------------------------------------------------
1 | list = ["string1", "string2", "string3"]
--------------------------------------------------------------------------------
/for-expressions/variables.tf:
--------------------------------------------------------------------------------
1 | variable "list" {
2 | type = list(string)
3 | description = "A sample list of strings"
4 | }
--------------------------------------------------------------------------------
/for_each_map/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | for_each = var.ec2_instances
10 |
11 | ami = data.aws_ami.busybox.id
12 | instance_type = var.instance_type
13 |
14 | tags = {
15 | "Name" = each.key
16 | "Environment" = each.value
17 | }
18 | }
--------------------------------------------------------------------------------
/for_each_map/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/for_each_map/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | ec2_instances = {
3 | "ec2_instance1" = "dev"
4 | "ec2_instance2" = "stage"
5 | "ec2_instance3" = "prod"
6 | }
--------------------------------------------------------------------------------
/for_each_map/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "ec2_instances" {
8 | type = map(string)
9 | description = "EC2 Instances Tags"
10 | }
--------------------------------------------------------------------------------
/for_each_toset/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | for_each = toset(var.ec2_instances)
10 |
11 | ami = data.aws_ami.busybox.id
12 | instance_type = var.instance_type
13 |
14 | tags = {
15 | "Name" = each.key
16 | }
17 | }
--------------------------------------------------------------------------------
/for_each_toset/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/for_each_toset/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | ec2_instances = ["ec2-number1", "ec2-number2"]
--------------------------------------------------------------------------------
/for_each_toset/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "ec2_instances" {
8 | type = list(string)
9 | description = "EC2 Instance Name"
10 | }
--------------------------------------------------------------------------------
/images/terraform.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MahdiAbbasi95/terraform-tutorial/cd0ccc993ae5d6c10d8d1b098641e3652fb03a6b/images/terraform.png
--------------------------------------------------------------------------------
/input-variables-validation/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | tags = {
13 | "Name" = "ec2-${var.environment}"
14 | }
15 | }
--------------------------------------------------------------------------------
/input-variables-validation/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/input-variables-validation/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/input-variables-validation/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 |
6 | validation {
7 | condition = substr(var.instance_type, 0, 2) == "t2"
8 | error_message = "You should choose a t2 tier"
9 | }
10 | }
11 |
12 | variable "environment" {
13 | type = string
14 | description = "Environment"
15 | }
--------------------------------------------------------------------------------
/lifecycle/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | data "aws_ami" "ubuntu" {
9 | filter {
10 | name = "name"
11 | values = ["ubuntu"]
12 | }
13 | }
14 |
15 | resource "aws_instance" "ec2_1" {
16 | ami = data.aws_ami.ubuntu.id
17 | instance_type = var.instance_type
18 |
19 | tags = {
20 | "Name" = "first-ec2-${var.environment}"
21 | }
22 | }
23 |
24 | resource "aws_instance" "ec2" {
25 | ami = data.aws_ami.busybox.id
26 | instance_type = var.instance_type
27 |
28 | tags = {
29 | "Name" = "ec2-${var.environment}"
30 | }
31 |
32 | lifecycle {
33 | # create_before_destroy = true
34 | # prevent_destroy = true
35 | # ignore_changes = [
36 | # tags
37 | # ]
38 | # replace_triggered_by = [
39 | # aws_instance.ec2_1.id
40 | # ]
41 | # precondition {
42 | # condition = data.aws_ami.busybox.id == "ami-000001"
43 | # error_message = "This image is not busybox, For this instance only busybox will be accepted."
44 | # }
45 | }
46 | }
--------------------------------------------------------------------------------
/lifecycle/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/lifecycle/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/lifecycle/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------
/local/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | locals {
9 | written_by = "terraform"
10 | }
11 |
12 | resource "aws_instance" "ec2" {
13 | ami = data.aws_ami.busybox.id
14 | instance_type = var.instance_type
15 |
16 | tags = {
17 | "Name" = "ec2-${var.environment}"
18 | "written_by" = local.written_by
19 | }
20 | }
--------------------------------------------------------------------------------
/local/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/local/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/local/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------
/localstack/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3.8"
2 |
3 | services:
4 | localstack:
5 | container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
6 | image: localstack/localstack
7 | ports:
8 | - "4566:4566" # LocalStack Gateway
9 | - "4510-4559:4510-4559" # external services port range
10 | environment:
11 | - DEBUG=${DEBUG-}
12 | - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
13 | - DOCKER_HOST=unix:///var/run/docker.sock
14 | # - LOCALSTACK_API_KEY=
15 | volumes:
16 | - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
17 | - "/var/run/docker.sock:/var/run/docker.sock"
--------------------------------------------------------------------------------
/output/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | tags = {
13 | "Name" = "ec2"
14 | }
15 | }
--------------------------------------------------------------------------------
/output/outputs.tf:
--------------------------------------------------------------------------------
1 | output "public_ip" {
2 | value = aws_instance.ec2.public_ip
3 | }
4 |
5 | output "private_ip" {
6 | value = aws_instance.ec2.private_ip
7 | }
--------------------------------------------------------------------------------
/output/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/output/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
--------------------------------------------------------------------------------
/output/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
--------------------------------------------------------------------------------
/provisioner-file/file.txt:
--------------------------------------------------------------------------------
1 | File-provisioner
--------------------------------------------------------------------------------
/provisioner-file/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_key_pair" "key_pair" {
9 | key_name = "my-key"
10 | public_key = var.public_key
11 | }
12 |
13 | resource "aws_instance" "ec2" {
14 | ami = data.aws_ami.busybox.id
15 | instance_type = var.instance_type
16 |
17 | key_name = aws_key_pair.key_pair.key_name
18 |
19 | provisioner "file" {
20 | connection {
21 | type = "ssh"
22 | user = var.username
23 | host = self.public_ip
24 | private_key = file(var.private_key)
25 | }
26 |
27 | source = var.source_file
28 | destination = var.destination_file
29 | }
30 |
31 | tags = {
32 | "Name" = "ec2-${var.environment}"
33 | }
34 | }
--------------------------------------------------------------------------------
/provisioner-file/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/provisioner-file/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
3 | source_file = "file.txt"
4 | destination_file = "/root/file.txt"
5 | public_key = ""
6 | private_key = ""
7 | username = "root"
--------------------------------------------------------------------------------
/provisioner-file/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
11 |
12 | variable "source_file" {
13 | type = string
14 | description = "Source file"
15 | }
16 |
17 | variable "destination_file" {
18 | type = string
19 | description = "Destination file"
20 | }
21 |
22 | variable "public_key" {
23 | type = string
24 | description = "Public Key"
25 | }
26 |
27 | variable "private_key" {
28 | type = string
29 | description = "Private key path"
30 | }
31 |
32 | variable "username" {
33 | type = string
34 | description = "Username"
35 | }
--------------------------------------------------------------------------------
/provisioner-local-exec/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | provisioner "local-exec" {
13 | command = "echo ${self.private_ip} > private_ip.txt"
14 | }
15 |
16 | provisioner "local-exec" {
17 | command = "echo Provisioner in Destroy time > destroy_time.txt"
18 | when = destroy
19 | }
20 |
21 | provisioner "local-exec" {
22 | command = "test 'test1' = 'test2'"
23 | on_failure = fail # fail or continue
24 | }
25 |
26 | tags = {
27 | "Name" = "ec2-${var.environment}"
28 | }
29 | }
--------------------------------------------------------------------------------
/provisioner-local-exec/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/provisioner-local-exec/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/provisioner-local-exec/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------
/provisioner-remote-exec/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_key_pair" "key_pair" {
9 | key_name = "my-key"
10 | public_key = var.public_key
11 | }
12 |
13 | resource "aws_instance" "ec2" {
14 | ami = data.aws_ami.busybox.id
15 | instance_type = var.instance_type
16 |
17 | key_name = aws_key_pair.key_pair.key_name
18 |
19 | provisioner "remote-exec" {
20 | connection {
21 | type = "ssh"
22 | user = var.username
23 | host = self.public_ip
24 | private_key = file(var.private_key)
25 | }
26 |
27 | inline = [
28 | "echo remote-exec-provisioner > remote-exec.txt"
29 | ]
30 |
31 | }
32 |
33 | tags = {
34 | "Name" = "ec2-${var.environment}"
35 | }
36 | }
--------------------------------------------------------------------------------
/provisioner-remote-exec/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/provisioner-remote-exec/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
3 | public_key = ""
4 | private_key = ""
5 | username = "root"
--------------------------------------------------------------------------------
/provisioner-remote-exec/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
11 |
12 | variable "public_key" {
13 | type = string
14 | description = "Public Key"
15 | }
16 |
17 | variable "private_key" {
18 | type = string
19 | description = "Private key path"
20 | }
21 |
22 | variable "username" {
23 | type = string
24 | description = "Username"
25 | }
--------------------------------------------------------------------------------
/remote-backend-s3/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 |
9 | backend "s3" {
10 | bucket = "s3-backend"
11 | key = "terraform.tfstate"
12 | region = "us-east-1"
13 | }
14 | }
15 |
16 | provider "aws" {
17 | access_key = "test"
18 | secret_key = "test"
19 | region = "us-east-1"
20 | s3_use_path_style = false
21 | skip_credentials_validation = true
22 | skip_metadata_api_check = true
23 | skip_requesting_account_id = true
24 |
25 | endpoints {
26 | apigateway = "http://localhost:4566"
27 | apigatewayv2 = "http://localhost:4566"
28 | cloudformation = "http://localhost:4566"
29 | cloudwatch = "http://localhost:4566"
30 | dynamodb = "http://localhost:4566"
31 | ec2 = "http://localhost:4566"
32 | es = "http://localhost:4566"
33 | elasticache = "http://localhost:4566"
34 | firehose = "http://localhost:4566"
35 | iam = "http://localhost:4566"
36 | kinesis = "http://localhost:4566"
37 | lambda = "http://localhost:4566"
38 | rds = "http://localhost:4566"
39 | redshift = "http://localhost:4566"
40 | route53 = "http://localhost:4566"
41 | s3 = "http://s3.localhost.localstack.cloud:4566"
42 | secretsmanager = "http://localhost:4566"
43 | ses = "http://localhost:4566"
44 | sns = "http://localhost:4566"
45 | sqs = "http://localhost:4566"
46 | ssm = "http://localhost:4566"
47 | stepfunctions = "http://localhost:4566"
48 | sts = "http://localhost:4566"
49 | }
50 | }
--------------------------------------------------------------------------------
/resource/main.tf:
--------------------------------------------------------------------------------
1 | resource "aws_s3_bucket" "s3_bucket" {
2 | bucket = "my-bucket"
3 |
4 | tags = {
5 | "Name" = "my-bucket"
6 | }
7 | }
--------------------------------------------------------------------------------
/resource/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/splat-expressions/main.tf:
--------------------------------------------------------------------------------
1 | output "list_interface" {
2 | value = var.list[*].interface
3 | }
4 |
5 | output "list_ip" {
6 | value = var.list[*].ip
7 | }
--------------------------------------------------------------------------------
/splat-expressions/terraform.tfvars:
--------------------------------------------------------------------------------
1 | list = [
2 | {
3 | interface = "eth0"
4 | ip = "192.168.0.1"
5 | },
6 | {
7 | interface = "eth1"
8 | ip = "192.168.10.1"
9 |
10 | },
11 | {
12 | interface = "eth0"
13 | ip = "192.168.20.1"
14 | }
15 | ]
--------------------------------------------------------------------------------
/splat-expressions/variables.tf:
--------------------------------------------------------------------------------
1 | variable "list" {
2 | type = list(object({
3 | interface = string
4 | ip = string
5 | }))
6 | description = "A sample list of intefaces with their IP addresses"
7 | }
--------------------------------------------------------------------------------
/template-strings-for/main.tf:
--------------------------------------------------------------------------------
1 | output "string_templates_for" {
2 | value = "%{for ip in var.ip_list}${ip}\n%{endfor}"
3 | }
--------------------------------------------------------------------------------
/template-strings-for/terraform.tfvars:
--------------------------------------------------------------------------------
1 | ip_list = ["192.168.0.1", "192.168.0.2", "192.168.10.1", "192.168.10.2"]
--------------------------------------------------------------------------------
/template-strings-for/variables.tf:
--------------------------------------------------------------------------------
1 | variable "ip_list" {
2 | type = list(string)
3 | description = "List of ips"
4 | }
--------------------------------------------------------------------------------
/template-strings-if-else/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | output "ami" {
9 | value = "%{if data.aws_ami.busybox.name == "busybox"} This is a busybox image %{else} This is not busybox image %{endif}"
10 | }
--------------------------------------------------------------------------------
/template-strings-if-else/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/variables/main.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "busybox" {
2 | filter {
3 | name = "name"
4 | values = ["busybox"]
5 | }
6 | }
7 |
8 | resource "aws_instance" "ec2" {
9 | ami = data.aws_ami.busybox.id
10 | instance_type = var.instance_type
11 |
12 | tags = {
13 | "Name" = "ec2-${var.environment}"
14 | }
15 | }
--------------------------------------------------------------------------------
/variables/providers.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | aws = {
4 | source = "hashicorp/aws"
5 | version = "4.52.0"
6 | }
7 | }
8 | }
9 |
10 | provider "aws" {
11 | access_key = "test"
12 | secret_key = "test"
13 | region = "us-east-1"
14 | s3_use_path_style = false
15 | skip_credentials_validation = true
16 | skip_metadata_api_check = true
17 | skip_requesting_account_id = true
18 |
19 | endpoints {
20 | apigateway = "http://localhost:4566"
21 | apigatewayv2 = "http://localhost:4566"
22 | cloudformation = "http://localhost:4566"
23 | cloudwatch = "http://localhost:4566"
24 | dynamodb = "http://localhost:4566"
25 | ec2 = "http://localhost:4566"
26 | es = "http://localhost:4566"
27 | elasticache = "http://localhost:4566"
28 | firehose = "http://localhost:4566"
29 | iam = "http://localhost:4566"
30 | kinesis = "http://localhost:4566"
31 | lambda = "http://localhost:4566"
32 | rds = "http://localhost:4566"
33 | redshift = "http://localhost:4566"
34 | route53 = "http://localhost:4566"
35 | s3 = "http://s3.localhost.localstack.cloud:4566"
36 | secretsmanager = "http://localhost:4566"
37 | ses = "http://localhost:4566"
38 | sns = "http://localhost:4566"
39 | sqs = "http://localhost:4566"
40 | ssm = "http://localhost:4566"
41 | stepfunctions = "http://localhost:4566"
42 | sts = "http://localhost:4566"
43 | }
44 | }
--------------------------------------------------------------------------------
/variables/terraform.tfvars:
--------------------------------------------------------------------------------
1 | instance_type = "t2.micro"
2 | environment = "dev"
--------------------------------------------------------------------------------
/variables/variables.tf:
--------------------------------------------------------------------------------
1 | variable "instance_type" {
2 | type = string
3 | description = "Instance type"
4 | default = "t2.micro"
5 | }
6 |
7 | variable "environment" {
8 | type = string
9 | description = "Environment"
10 | }
--------------------------------------------------------------------------------