EKS : UDMY

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Github - Repo -- owned by kalyan reddy daida
https://github.com/stacksimplify/terraform-on-aws-eks

On Youtube : Shorter Video
https://www.youtube.com/watch?v=IJBHIuURA3g&list=PLLh98oBzdb7YLnm-67RnNIjrD98g1w2js

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 










terraform init

terraform init is a command used in HashiCorp Terraform, which is an infrastructure-as-code tool used for managing and provisioning infrastructure resources. The terraform init command initializes a Terraform configuration, setting up the environment for a new or existing project. Here's what it does:

  1. Plugin Initialization: When you create or work with a new Terraform configuration (a set of .tf files that define your infrastructure), terraform init is used to initialize the working directory. This involves downloading and installing any required provider plugins, which are responsible for interacting with the various infrastructure platforms (e.g., AWS, Azure, Google Cloud) that you intend to manage using Terraform.

  2. Backend Configuration: Terraform supports different backend configurations to store its state files, which contain information about your infrastructure. During initialization, you can specify the backend configuration, such as the storage location for the state files. This allows you to store state files remotely (e.g., in an S3 bucket) for collaboration and state management.

  3. Module Installation: If you are using external Terraform modules (reusable configurations), terraform init will download and install the modules specified in your configuration. Modules can be sourced from various sources, including the Terraform Module Registry or local paths.

  4. Lock File Generation: Terraform generates a terraform.tfstate.lock.json file to prevent concurrent modifications to the state file by different users.

  5. Validation and Dependency Checks: During initialization, Terraform validates the configuration files, checks for syntax errors, and resolves any dependencies between resources or modules.

  6. Variable Initialization: If you have defined input variables in your configuration, Terraform will prompt you to provide values for those variables interactively, or you can specify them using variable files or environment variables.

Here's how you use the terraform init command:

terraform init

By running this command in the root directory of your Terraform configuration, you ensure that your project is properly set up for planning and applying changes to your infrastructure. It's a necessary step before using other Terraform commands like terraform plan and terraform apply.

Remember that you typically only need to run terraform init once when you first set up your project or when there are changes to your provider requirements, backend configuration, or module sources.




https://github.com/stacksimplify/hashicorp-certified-terraform-associate/tree/main/02-Terraform-Basics/02-02-Terraform-Command-Basics
  • Pre-Conditions-1: Ensure you have default-vpc in that respective region
  • Pre-Conditions-2: Ensure AMI you are provisioning exists in that region if not update AMI ID
  • Pre-Conditions-3: Verify your AWS Credentials in $HOME/.aws/credentials


terraform init

files that get downloaded.

.terraform -- provider 

.terraform.lock.hcl


Multiple ways to comment out.


Provider Block: The provider block is used to define the cloud or infrastructure provider you are working with, such as AWS, Azure, Google Cloud, or others. It specifies the provider's details and authentication credentials.

provider "aws" {
  region = "us-west-1"
}
 

Resource Block: The resource block is used to define a specific infrastructure resource, such as virtual machines, databases, networks, etc. It contains attributes that define the desired state of the resource.

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

3. Data Block
: The data block is used to retrieve information from the provider or external sources. It allows you to query and fetch data to be used in your configuration.

data "aws_ami" "example" {
most_recent = true
owners = ["self"]
}


Variable Block: The variable block is used to define input variables that can be parameterized in your configuration. These variables can be set when running Terraform and allow for dynamic and reusable configurations.

variable "instance_type" {
  description = "The type of EC2 instance"
  type        = string
  default     = "t2.micro"
}


Output Block: The output block is used to define values that you want to display as the output when Terraform applies your configuration. This is useful for providing information to users after the infrastructure is created.

 output "instance_ip" {
  description = "The public IP address of the instance"
  value       = aws_instance.example.public_ip
}

Module Block: The module block is used to define and encapsulate reusable sets of resources and configurations. Modules enable you to create more modular and maintainable configurations.

 module "vpc" {
  source = "./modules/vpc"
 
  name = "my-vpc"
}


Meta- Argument

In Terraform, meta-arguments, also known as meta-parameters or meta-attributes, are special configuration settings that are applied to resource, module, and provider blocks. These meta-arguments provide additional information about how Terraform should manage the associated resources or configurations. They help you control various aspects of your infrastructure and how Terraform interacts with it. Here are some common meta-arguments and their purposes:

  1. count: The count meta-argument is used to create multiple instances of a resource or module based on a numeric value. It allows you to dynamically create or manage multiple resources based on a specified count.

resource "aws_instance" "example" {
  count = 3

  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}


for_each: The for_each meta-argument is similar to count, but it allows you to create and manage multiple instances of a resource or module based on a map or set of values.

variable "instances" {
  type    = map
  default = {
    web     = "t2.micro"
    database = "db.t2.small"
  }
}

resource "aws_instance" "example" {
  for_each = var.instances

  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = each.value
}
 

depends_on: The depends_on meta-argument specifies explicit dependencies between resources. It ensures that Terraform creates resources in the specified order to satisfy dependencies.

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

resource "aws_security_group" "web_sg" {
  # ...

  depends_on = [aws_instance.web]
}


lifecycle: The lifecycle meta-argument is used to control certain lifecycle behaviors of a resource, such as preventing certain operations like deletion or updates.

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  lifecycle {
    prevent_destroy = true
  }
}

provider: The provider meta-argument is used to associate a specific provider configuration with a resource. This is useful when you're working with multiple providers in a single configuration.

resource "aws_instance" "example" {
  provider = aws.us_west_provider

  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}










Comments

Popular posts from this blog

EKS : -