jerryschen31 / learn

Scratch repo used for learning stuff
GNU General Public License v3.0
0 stars 0 forks source link

2023-02-13 review previous Terraform code #20

Open jerryschen31 opened 1 year ago

jerryschen31 commented 1 year ago

learn concepts from review

jerryschen31 commented 1 year ago

availability zones https://www.rackspace.com/blog/aws-101-regions-availability-zones#:~:text=There%20are%20anywhere%20between%20two,three%20or%20more%20per%20region.

aws ec2 describe-availability-zones --region

jerryschen31 commented 1 year ago

Terraform concepts

Infrastructure as Code (IAC): Terraform uses a declarative language to define and manage infrastructure resources. This means that infrastructure is defined as code, just like a software application, allowing you to easily version, review, and collaborate on infrastructure changes.

Resource: In Terraform, a resource is a piece of infrastructure that you want to create and manage, such as an AWS EC2 instance, a Google Cloud Storage bucket, or an Azure virtual network. Resources are defined in Terraform configuration files, and Terraform uses them to create, modify, or delete resources in your cloud provider.

Provider: A provider is a plugin in Terraform that allows you to interact with a specific cloud provider, such as AWS, Google Cloud, or Azure. Providers are responsible for creating, modifying, and deleting resources in the cloud provider's API.

State: Terraform keeps track of the state of the infrastructure it manages. The state file is a JSON file that keeps track of the resources that Terraform has created and their current configuration. Terraform uses this file to know what resources to create, update, or delete.

Plan: Before making changes to your infrastructure, Terraform creates a plan that shows you what changes it will make. This allows you to review the changes and ensure that they are correct before applying them.

Apply: The apply step is where Terraform actually makes changes to your infrastructure based on the plan that was generated in the previous step.

Modules: Modules are a way to organize and reuse Terraform code. Modules are self-contained units of Terraform configuration that can be used as building blocks to create more complex infrastructure.

jerryschen31 commented 1 year ago

Modules are a way to organize and reuse Terraform code. They are a self-contained unit of Terraform configuration that can be used as building blocks to create more complex infrastructure. Here is an example of how modules can be used in Terraform:

Let's say you want to create an AWS VPC with a public subnet, private subnet, and an EC2 instance in the private subnet. You can create a module for each of these resources and use them to create your infrastructure.

VPC module:

# modules/vpc/main.tf
resource "aws_vpc" "example" {
  cidr_block = var.cidr_block
}

resource "aws_internet_gateway" "example" {
  vpc_id = aws_vpc.example.id
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.example.id
}

resource "aws_route_table_association" "public" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public.id
}

variable "cidr_block" {
  type    = string
  default = "10.0.0.0/16"
}

Public Subnet module:

# modules/public_subnet/main.tf
resource "aws_subnet" "public" {
  vpc_id     = var.vpc_id
  cidr_block = var.cidr_block
}

variable "vpc_id" {
  type    = string
  default = ""
}

variable "cidr_block" {
  type    = string
  default = "10.0.1.0/24"
}

Private Subnet module:

# modules/private_subnet/main.tf
resource "aws_subnet" "private" {
  vpc_id     = var.vpc_id
  cidr_block = var.cidr_block
}

variable "vpc_id" {
  type    = string
  default = ""
}

variable "cidr_block" {
  type    = string
  default = "10.0.2.0/24"
}

EC2 Instance module:

# modules/ec2/main.tf
resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type
  subnet_id     = var.subnet_id
}

variable "ami" {
  type    = string
  default = "ami-0c55b159cbfafe1f0"
}

variable "instance_type" {
  type    = string
  default = "t2.micro"
}

variable "subnet_id" {
  type    = string
  default = ""
}

And then

# main.tf
module "vpc" {
  source = "./modules/vpc"
}

module "public_subnet" {
  source  = "./modules/public_subnet"
  vpc_id  = module.vpc.aws_vpc.example.id
}

module "private_subnet" {
  source  = "./modules/private_subnet"
  vpc_id  = module.vpc.aws_vpc.example.id
}

module "ec2" {
  source    = "./modules/ec2"
  subnet_id = module.private_subnet.aws_subnet.private.id
}

In this example, the main.tf file uses the vpc, public_subnet, private_subnet, and ec2 modules to create an AWS VPC with a public subnet, private subnet, and an EC2 instance in the private subnet. The modules each have their own main.tf file, which contains the resources and variables for that module.

Note that subnet_id for the EC2 instance is assigned after the private_subnet gets its id