Descripción de la infraestructura en Terraform para el futuro. Anton Babenko (2018)



Muchas personas conocen y utilizan Terraform en su trabajo diario, pero todavía no existen las mejores prácticas para ello. Cada equipo tiene que inventar sus propios enfoques y métodos.



Es casi seguro que su infraestructura comience simple: algunos recursos + algunos desarrolladores. Con el tiempo, crece en todo tipo de direcciones. ¿Encuentra formas de agrupar activos en módulos de Terraform, organizar su código en carpetas y qué podría salir mal aquí? (últimas palabras famosas)



El tiempo pasa y sientes que tu infraestructura es tu nueva mascota, pero ¿por qué? Le preocupan los cambios inexplicables en la infraestructura, tiene miedo de tocar la infraestructura y el código; como resultado, retrasa la nueva funcionalidad o reduce la calidad ...



Después de tres años de administrar una colección de módulos comunitarios de Terraform para AWS en Github y el mantenimiento a largo plazo de Terraform en producción, Anton Babenko está listo para compartir su experiencia: cómo escribir módulos TF para que no duela en el futuro.



Terraform, , Terraform, , .



:





Disclaimer: 2018 — 2 . Terraform 0.11 . 2 2 , , . .





:





. - , . , -, .



Terraform open source , Terraform Amazon 2015- .



, . .



Terraform. HighLoad. .



Terraform-. , . pre-commit hook . .



. , , , Terraform- . , - . . , , «» .



. .



, . .





https://github.com/terraform-aws-modules

https://registry.terraform.io/namespaces/terraform-aws-modules



, Terraform AWS modules, GitHub, : VPC, Autoscaling, RDS.





, , - . , , Terraform, - . . . , , - .





Terraform 2014- , , . « ».



, , terraform.io. , . , .





Terraform- , - .





«aws_region».





, .





- , «terraform init» , , .





«terraform apply» , , . . . , Terraform .





. bucket, seasnail.





. , Amazon, AWS CloudFormation Google Cloud Deployment Manager, Azure Resource Manager. - public cloud . Terraform , 100 . ( )





, Terraform :



  • Terraform .
  • .
  • Terraform , .


2014- «» .





Terraform . API, :



  • 120 , .
  • , Terraform GitHub .
  • Jira .
  • New Relic-.
  • dropbox , .


Terraform-, API, Go.





, Terraform, - , - , main.tf, .





, , VPC.



VPC, 12 . , cidr_block IP- . .





, .





: , , , Terraform , GitHub- . . DNS-, . Terraform .





.





internet_gateway, , VPC . .





main.tf:





main.tf.





main.tf.



subnet. , NAT gateways, routes, routing tables subnets, 38 , 200-300 .





. . main.tf . . main.tf 10-20 Kb. , 10-20 Kb – . . . 10-20 Kb – user case, . , .



, . . , , , , . Terraform .





  • .
  • .




- . , . . 10-20 Kb – , , , network stack, . . . Application Load Balancer, deployment ES cluster, Kubernetes . ., 100 Kb. , , Terraform Terraform-.





Terraform- – Terraform-, . , Terraform-. , - -. . . . - Terraform-, . .





, 10-20-30 Kb . , - .



, , . , , , . , open source , , building blocks .





.





, .





.





. . , , .





, , . . , , .





, . security-group. 640- . security-croup Amazon – . security-group , . . Amazon . , VPC endpoint, prefix list, API , Terraform . Amazon API . , .





, .





, , , . , Terraform - , . , Terraform . . 0.11, - . , jsonnet, cookiecutter , .



.





.





, .





, .





VPC Application Load Balancer, security-group Elastic Container Service .





. , , , .



Terraform , , , . , , . -, .





, . , .





Terraform Registry — https://registry.terraform.io/



№ 0 – . . , open source, -, IP-, . . flexible. , , . Amazon . 650. .





- : « . , ». Amazon, Terraform. : « MSSQL». . . , , , time-.





, : MSSQL, , Terraform 0.11 time- .





. , . . , public open source, , , .





, , Terraform - . , Terraform- . , - . .



, Terraform . , , Terraform , , . . 100 200 , 100 200 , . , , , «Terraform init» .





https://github.com/mbtproject/mbt



. , . , (./). , - , Git clone . .



, , downsides. , versioning. .



. - pipeline, MBT, S3. . iam-user-1.0.0.zip 1 Kb, . .





, .





? – assume user. Assume user – , . , . , Terraform . .





, Amazon , , , shared key, , Terraform . , , . . . . , . - .





provisioner. , , , , .





, provisioner , , -. , -, , aws ec2, . . Linux Windows. , -, user cases.





, , , aws_instance, , , provisioner «local-exec» ansible-playbook.





– , . , local-exec , , launch_configuration.





launch_configuration, instance autoscaling group, launch_configuration «provisioner». «user data».





user data. instance, instance , user data, autoscaling group launch_configuration.





- provisioner, , provisioner, . .



null_resource. Null_resource – , . , API, autoscaling. , . .





http://bit.ly/common-traits-in-terraform-modules



. . . Terraform , , , open source, - . , . GitHub, . , , . , , , . .



-, , .



:



  • .
  • .
  • .
  • .
  • .


– , . .





, , . . , , , , . . - . , - , provisioner .



. : «- . ». .





, .



, . , 20 . . , , . , - , - . , - , . . . , , , , . .





. – . -. best practice Terraform.



deprecated . Terraform- , best practice, . . , . , «Terraform plan» Terraform , .



– , , 5 . - . , 15 . 15 AWS API , , . .



, , , - , 15 , - . , «Yes», - . . Terraform . . . , . – . Terraform 0.11 . 0.12 , : «, , ?».





, . . .



, , . . , . - . . : « , ». , .





? , , , . . - .



, ? -. .





– - . , , , . , , , .





- stack VPC EC2, . . autoscaling group, subnet, . - : ?



, . Terraform , , make- Terraform. , - , .





? - , ? , .





, . Terraform , Terraform.



: «, ». , . , Terraform Terraform, Terraform, . Terraform .





https://github.com/gruntwork-io/terragrunt/



, - , Terragrunt.



Terragrunt – , Terraform, .





Terraform- .





, .





.





. , Terragrunt.



, 1 700 GitHub . , . , Terraform.





, – Terragrunt. .





, .



. , .





- , , , Terraform , AWS- , , , .





, block.





output id , .





Terraform 0.11 – .





, users.





users, block resource, . , . . , , user3, , , , , , .





stateful-. stateful-? , . , AWS Access Key AWS Secret Key, . . user’, Access Secret Key. , - user’, user’ . , user , user’ , - .





. , Jsonnet. Jsonnet – Google.





json-, .





.



Terraform HCL, Json , Json, Terraform. .tf.json .





: terraform init, terramorm apply. user’.



, - . json-. , . .





Terraform , , Terraform. Terraform . , – Terraform. . Ansible, , .



, Terraform - , - .



. output, .





shell terraform output , . . .





. null_resource . local-exe, ID - .





, , Amazon public- edge cases.



edge cases , , AWS-, , ; ; , 2013- ; , VPC . . . Amazon .





, .



Terraform plan Terraform CLI. tfvars-, .



. Terraform plan – var . – var, – var, , . , , , , , , , . , . tfvars, , .



target . .



parallelism. 150 parallelism Amazon 10, , 100, , , - . , , Amazon , , .



Terraform , . Parallelism=1 – , , - AWS API Terraform-. : parallelism=1 , Terraform , , . .



: « , Terraform workspaces – ?». , , , .



Workspaces . , GitHub issues, Terraform workspaces. , . Terraform Enterprise – . Terraform HashiCorp , workspaces, . , . , .





? , – . Terraform . , . , .





« ». . – , 0.12.





0.12 – . , , , , , . , 0.12 .





! , , , , 0.12 .





! . ? ? ?



, . – . , . , : « », : « ?». , . , . . . . , . . . , .



, . . , . . . , , .



Terratest – , Terraform. . DSL, , rspec.



, ! . . , , provisioning, deployment. Provisioning , deployment - , , , . . , Terraform provisioning, Ansible deployment, Ansible nginx, Postgres. Ansible provisioning, , . Terraform - . , - , Terraform Ansible, ? , , , Ansible – , Terraform ?



, . , Terraform 2014- . . - configuration management Ansible. , user data launch_configuration. Ansible . . , .



in beautiful infrastructure, Packer, . Terraform data source launch_configuration. . . pipeline , Tracker, Terraform. build, .



! ! , RBS. Ansible provisioner . Ansible , . Terraform, Ansible, state . ?



. , Ansible – , autoscaling group. autoscaling group , launch_configuration. launch_configuration , , . Amazon Terraform ts , -, . , «autoscaling group», , DigitalOcean - , autoscaling group, API, IP-, dynamic inventory , Ansible . . . Amazon launch_configuration, dynamic inventory.




All Articles