Terraform ============================= Samples ******************************************************************************** ### Basic: # Don't forget to # export AWS_ACCESS_KEY_ID=(your access key id) # export AWS_SECRET_ACCESS_KEY=(your secret access key) provider "aws" { region = "eu-central-1" version = "~> 2.43" } variable "server_port" { description = "The port the server will use for HTTP requests" type = number default = 8080 } resource "aws_security_group" "instance" { name = "terraform-example-instance" ingress { from_port = var.server_port to_port = var.server_port protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_instance" "example" { ami = "ami-0cc0a36f626a4fdf5" instance_type = "t2.micro" vpc_security_group_ids = [aws_security_group.instance.id] user_data = <<-EOF #!/bin/bash echo "Hello, World" > index.html nohup busybox httpd -f -p ${var.server_port} & EOF tags = { Name = "terraform-example" } } output "public_ip" { value = aws_instance.example.public_ip description = "The public IP of the web server" #https://www.terraform.io/docs/providers/aws/d/instance.html#public_ip } ******************************************************************************** ### Deploy a cluster of web servers https://blog.gruntwork.io/an-introduction-to-terraform-f17df9c6d180 provider "aws" { region = "eu-central-1" version = "~> 2.43" } variable "server_port" { description = "The port the server will use for HTTP requests" type = number default = 8080 } data "aws_availability_zones" "all" {} resource "aws_security_group" "instance" { name = "terraform-example-instance" ingress { from_port = var.server_port to_port = var.server_port protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_security_group" "elb" { name = "terraform-example-elb" # Allow all outbound egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } # Inbound HTTP from anywhere ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_autoscaling_group" "example" { launch_configuration = aws_launch_configuration.example.id availability_zones = data.aws_availability_zones.all.names min_size = 2 max_size = 10 load_balancers = [aws_elb.example.name] health_check_type = "ELB" tag { key = "Name" value = "terraform-asg-example" propagate_at_launch = true } } resource "aws_elb" "example" { #Amazon’s Elastic Load Balancer (ELB) name = "terraform-asg-example" security_groups = [aws_security_group.elb.id] availability_zones = data.aws_availability_zones.all.names health_check { target = "HTTP:${var.server_port}/" interval = 30 timeout = 3 healthy_threshold = 2 unhealthy_threshold = 2 } # This adds a listener for incoming HTTP requests. listener { lb_port = 80 lb_protocol = "http" instance_port = var.server_port instance_protocol = "http" } } resource "aws_launch_configuration" "example" { image_id = "ami-0cc0a36f626a4fdf5" instance_type = "t2.micro" security_groups = [aws_security_group.instance.id] user_data = <<-EOF #!/bin/bash echo "Hello, World" > index.html nohup busybox httpd -f -p "${var.server_port}" & EOF lifecycle { create_before_destroy = true } } output "clb_dns_name" { value = aws_elb.example.dns_name description = "The domain name of the load balancer" } ******************************************************************************** ### Terraform 0.11 to 0.12 terraform11 0.12checklist terraform12 0.12upgrade terraform12 init For automatic check: LINE="*************************************************************************************************************" ls | while read ARGS; do echo $ARGS; echo $LINE; terraform12 0.12upgrade $ARGS;done ******************************************************************************** _BY: Farid Ahmadian_ _TAG: terraform_ _DATE: 2020-01-29 10:24:11_