terraform infransture as code , immutable, aws..

Bismillah insha allah learn 
Terraform
https://hackernoon.com/introduction-to-aws-with-terraform-7a8daf261dc0
1)
.aws/credentials

aws configure
[terraform]
aws_access_key_id = xxxx
aws_secret_access_key = xxx/xxxx/xxx

2)
mkdir terraform

3)
main.tf
provider "aws" {
region = "eu-west-3"
shared_credentials_file = "/home/eon1/.aws/credentials"
profile = "terraform"
}

4)
create AWS machine

resource "aws_instance" "web" {
ami = "ami-0e55e373"
instance_type = "t1.micro"
tags {
Name = "eralabs"
}
}

5)
cloud-images.ubuntu.com

6)
AWS IAM

CLI
aws ec2 describe-images --filters "Name=platform,Values=windows"
"Name=root-device-type,Values=ebs"

ex: describing ubuntu amis
aws ec2 describe-images --filters "Name=name,Values=ubuntu*"

7) AWS cheat sheet

8)
terraform init

9) terraform plan
# will not create any resource on your AWS cloud

10)
+ created
- deleted

+ aws_instance.web
      id:                           
      ami:                          "ami-0e55e373"
      associate_public_ip_address: 
      availability_zone:           

11)  working with variables
variable "region" {
default = "eu-west-3"
}
#call  it from terraform file
${var.region}
---

variable "region" {
  default = "eu-west-3"
}
provider "aws" {
  region                  = "${var.region}"
  shared_credentials_file = "/home/eon01/.aws/credentials"
  profile                 = "terraform"
}
resource "aws_instance" "web" {
  ami = "ami-0e55e373"
  instance_type = "t1.micro"
  tags {
    Name = "eralabs"
  }
}
---
12)
variable "shared_credentials_file" {
default = "/home/eon1/.aws/credentials"
}

variable "profile" {
default = "terraform"
}

provider "aws" {
regiion = "${var.region}"
shared_credentials_file = "${var.shared_credentials_file}"
profile = "${var.profile}"
}

...

13)
dubline region (eu-west1)
paris region (eu-west-3)

---

14)
variable "my_ami" {
type = "map"
default = {
eu-west-1 = "ami-f90a4880"
eu-west-3 = "ami-0355373"
}
description = "I added only 2 regions: Paris and Dublin. You can as many regions as you want."
}

ami = "${lookup(var.my_ami, var.region)}"

# test using  -> terraform plan

15)
#new region as argument
terraform plan -var region=eu-west-1

terraform plan -var region=eu-west-1 -var profile=default

16) seperate configuration from execution code

variables.tfvars
region = "eu-west-1"
shared_credentials_file = "/home/eon1/.aws/credentials"
profile = "terraform"

my_ami = {
"eu-west-1" = "ami-f90a4880"
"eu-west-3" = "ami-0355e373"
}

#main.tf
variable "region" {}
variable "shared_credentials_file" {}
variable "profil" {}
variable "my_ami" {
type = "map"
}

provider "aws" {
region = "${var.region}"
shared_credentials_file = "${var.shared_credentials_file}"
profile = "${var.profile}"
}

resource "aws_instance" "web" {
ami = "${lookup(var.my_ami, var.region)}"
instance_type = "t1.micro"
tags {
Name = "eralabs"
}
}

# run
terraform plan -var-file=variables.tfvars

17)
to execute create our ec2 machine
terraform apply -var-file=variables.tfvars

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
Terraform will perform the following actions:
  + aws_instance.web
      id:                           
      ami:                          "ami-f90a4880"

Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
Enter a value: yes
aws_instance.web: Creating...
  ami:                          "" => "ami-f90a4880"
aws_instance.web: Still creating... (10s elapsed)
aws_instance.web: Creation complete after 19s (ID: i-055aaa2cab2436ab4)
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

18)
Immutable infrastructure
immutable resource or component is replaced
for every deployment.
For instance, servers are never modified after the deployment.

when an update is needed, a new server should be created from a base/common image with new updates.


SSH key

#main.tf
resource "aws_instace" "web" {
ami = "${lookup(var.my_ami, var.region)}"
instance_type = "t1.micro"
key_name = "${var.key_name}"

    tags {
    Name = "eralabs"
    }
    ..

#variables.tfvars
key_name = "my_key.kp"

}

#terraform plan
-/+ aws_instance.web (new resource required)
      id:                           "i-055aaa2cab2436ab4" => (forces new resource)
      ami:                          "ami-f90a4880" => "ami-f90a4880"
      associate_public_ip_address:  "true" =>
      availability_zone:            "eu-west-1a" =>
      ebs_block_device.#:           "0" =>


 ->
 public registry
 https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/1.5.0

 main2.tf

 module "ec2_cluster" {
  source = "terraform-aws-modules/ec2-instance/aws"
  name           = "my-cluster"
  instance_count = 5
  ami                    = "ami-ebd02392"
  instance_type          = "t2.micro"
  key_name               = "user1"
  monitoring             = true
  vpc_security_group_ids = ["sg-12345678"]
  subnet_id              = "subnet-eddcdzz4"
  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

#terraform init

No comments:

Post a Comment