Beschreibung der Infrastruktur in Terraform fĂŒr die Zukunft. Anton Babenko (2018)



Viele Menschen kennen und verwenden Terraform in ihrer tĂ€glichen Arbeit, aber es gibt noch keine Best Practices dafĂŒr. Jedes Team muss seine eigenen AnsĂ€tze und Methoden erfinden.



Ihre Infrastruktur beginnt mit ziemlicher Sicherheit einfach: ein paar Ressourcen + ein paar Entwickler. Im Laufe der Zeit wĂ€chst es in alle möglichen Richtungen. Finden Sie Möglichkeiten, Assets in Terraform-Modulen zu gruppieren, Ihren Code in Ordnern zu organisieren und was könnte hier möglicherweise schief gehen? (berĂŒhmte letzte Worte)



Die Zeit vergeht und Sie haben das GefĂŒhl, dass Ihre Infrastruktur Ihr neues Haustier ist, aber warum? Sie sind besorgt ĂŒber ungeklĂ€rte Änderungen in der Infrastruktur, haben Angst, die Infrastruktur und den Code zu berĂŒhren - infolgedessen verzögern Sie neue Funktionen oder verringern die QualitĂ€t ...



Nach drei Jahren der Verwaltung einer Sammlung von Terraform-Community-Modulen fĂŒr AWS on Github und der langfristigen Wartung von Terraform in der Produktion ist Anton Babenko bereit, seine Erfahrungen zu teilen: wie man TF-Module schreibt, damit es in Zukunft nicht schadet.



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