Terraform - terrain formation using G.E.C.K

 

 

Terraform installation

https://www.terraform.io/downloads.html

export PATH=/path/to/terraform/:$PATH

AWS Setup and first machine launch (first steps)

Create IAM keys. Locate AMI. EC2 AMI Locator

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

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

 

nano first.tf
terraform init
terraform apply
terraform destroy
#explain
terraform plan? terraform plan -out file; terraform apply file; rm file

Variables (demo 1)

provider.tf

provider "aws" {
    access_key = "${var.AWS_ACCESS_KEY}"
    secret_key = "${var.AWS_SECRET_KEY}"
    region = "${var.AWS_REGION}"
}

vars.tf

variable "AWS_ACCESS_KEY" {}
variable "AWS_SECRET_KEY" {}
variable "AWS_REGION" {
  default = "eu-west-1"
}
variable "AMIS" {
  type = "map"
  default = {
    us-east-1 = "ami-13be557e"
    us-west-2 = "ami-06b94666"
    eu-west-1 = "ami-0d729a60"
  }
}

instance.tf

resource "aws_instance" "example" {
  ami           = "${lookup(var.AMIS, var.AWS_REGION)}"
  instance_type = "t2.micro"
}

terraform.tfvars (.gitignore)

AWS_ACCESS_KEY = ""
AWS_SECRET_KEY = ""
AWS_REGION = ""

 

cat instance.tf
cat vars.tf
vim terraform.tfvars
add
AWS_ACCESSS_KEY = "********************************"
AWS_SECRET_KEY = "**********************************"
terraform plan
rm terraform.tfvars
terraform plan

Software Provisioning (demo 2)

 

 

vim terraform.tfvars
cat vars.tf
ssh-keygen -f mykey
open default security group
terraform plan
terraform apply

Outputting attributes (demo3)

cat instance.tf

 

Remote State (demo4)

create bucket (versioning)
aws configure
vim backend.tf
enter:
terraform {
backend "s3" {
	bucket: "name",
    key: "key"
}
}


terraform init

 

Datasources (demo-5)

cat securitygroup.tf
terraform apply

 

 

Templates

Other providers

Modules (demo-6)

cat key.tf
cat vars.tf
ssh-keygen -f mykey
cat modules.tf
terraform get
ls .terraform
taerraform apply

 

Terraform Commands (demo-9)

vim terraform.tfstate
terraform show | grep public_
cat output.tf
terraform output
terraform plan
terraform taint aws_instance.example
terrarorm graph
vim vpc.tf
terraforn fmt vpc.tf
terraform import aws_instance.example id

Terraform with Jenkins, ECR, ECS. (docker-demo-3)

get password, ...
install Parameterized trigger plugin
new job
git - https://github.com/wardviaene/docker-demo
shell:
docker build -t ecr_link/ecr_id:${GIT_COMMIT} .
#(go to jenkins, sudo su - jenkins, aws configure
eval "$(aws ecr get-login --region eu-west-1 --no-email )"
docker push ecr_link/ecr_id:${GIT_COMMIT}


another job
git - https://github.com/vkryzh/terraform-course
script:
cd docker-demo-3
(this project is parameterized, string, MYAPP_VERSION)


terraform apply -target aws_ecs_service.myapp-server -var MYAPP_SERVICE_ENABLE="1" -var MYAPP_VERSION=${MYAPP_VERSION}

 

 

 

 

 

 

 

 

 

Add comment

Loading