terraform basics - docs.adfinis.com · check > terraform usage: terraform [-version] [-help] ...

46
Terraform Basics

Upload: others

Post on 30-Dec-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

TerraformBasics

Page 2: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Besmart.Thinkopensource.

Page 3: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Write,Plan,andCreateInfrastructureasCode

Page 4: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Agenda

IntroductiontoTerraformSetupHCLMyfirstTerraformConfigurationObjects

Page 5: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

IntroductiontoTerraformWhatisit?

Page 6: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Facts

Firstrelease:2014-07-28Writtenby:HashiCorpWrittenin:Go

Page 7: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Wikipediasays...Itallowsuserstodefineadatacenterinfrastructureinahigh-levelconfigurationlanguage,fromwhichitcancreateanexecutionplantobuildtheinfrastructure[...].

Page 8: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

itessentiallyisInfrastructureasCode

Page 9: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Setuphttps://learn.hashicorp.com/terraform/getting-started/install.html

Page 10: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Check>terraform

Usage:terraform[-version][-help]<command>[args]

Theavailablecommandsforexecutionarelistedbelow.Themostcommon,usefulcommandsareshownfirst,followedbylesscommonormoreadvancedcommands.Ifyou'rejustgettingstartedwithTerraform,stickwiththecommoncommands.Fortheothercommands,pleasereadthehelpanddocsbeforeusage.

Commoncommands:applyBuildsorchangesinfrastructureconsoleInteractiveconsoleforTerraforminterpolationsdestroyDestroyTerraform-managedinfrastructureenvWorkspacemanagementfmtRewritesconfigfilestocanonicalformatgetDownloadandinstallmodulesfortheconfigurationgraphCreateavisualgraphofTerraformresourcesimportImportexistinginfrastructureintoTerraforminitInitializeaTerraformworkingdirectoryoutputReadanoutputfromastatefileplanGenerateandshowanexecutionplanprovidersPrintsatreeoftheprovidersusedintheconfigurationpushUploadthisTerraformmoduletoAtlastorunrefreshUpdatelocalstatefileagainstrealresourcesshowInspectTerraformstateorplantaintManuallymarkaresourceforrecreationuntaintManuallyunmarkaresourceastaintedvalidateValidatestheTerraformfilesversionPrintstheTerraformversionworkspaceWorkspacemanagement

Allothercommands:debugDebugoutputmanagement(experimental)force-unlockManuallyunlocktheterraformstatestateAdvancedstatemanagement

Page 11: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

HCLHashiCorpconfigurationlanguagehttps://github.com/hashicorp/hcl

Page 12: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

HCL/JSONresource"azurerm_resource_group""workshoptest-01"{name="rg-adsy-workshoptest-01"location="westeurope"}

{"resource":{"azurerm_resource_group":{"workshoptest-01":{"name":"rg-adsy-workshoptest-01","location":"westeurope"}

}}}

Page 13: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

MyfirstTerraform

Page 14: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

main.tfresource"azurerm_resource_group""main"{name="main"location="${var.location}"}

resource"azurerm_virtual_network""main"{name="main_network"address_space=["10.0.0.0/16"]location="${azurerm_resource_group.main.location}"resource_group_name="${azurerm_resource_group.main.name}"}

Page 15: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

variables.tfvariable"location"{default="WestEurope"}

Page 16: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

output.tfoutput"rgname"{description="Thenameofourresourcegroup"value="${azurerm_resource_group.main.name}"}

Page 17: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Tryit>terraforminit>terraformplan

Page 18: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Adaptithttps://www.terraform.io/docs/providers/index.html

Page 19: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

ConfigurationObjects

resourcedataprovidervariableoutputlocalsmoduleterraform

Page 20: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

resourceresource"azurerm_resource_group""main"{name="my-resource-group"location="WestUS2"}

Definesaninfrastructureresource

https://www.terraform.io/docs/configuration/resources.html

Page 21: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

datadata"azurerm_public_ip""mypubip"{name="${azurerm_public_ip.ip-01.name}"resource_group_name="${azurerm_virtual_machine.myhost-01.resource_group_name}"}

Definesadatasourcethatcanbereused.Mustbeuniqueincombinationof<TYPE>and<NAME>.

https://www.terraform.io/docs/configuration/data-sources.html

Page 22: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

providerprovider"azurerm"{version="=1.23.0"}

Definesproviderstouse.Versionpinningisrecommended.

https://www.terraform.io/docs/configuration/providers.html

Page 23: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

variablevariable"user_name"{type="string"default="user"description="TheUsernameofournewuser"}

Definesvariables.Canbeofdifferenttypes(string,map,listorboolean)

https://www.terraform.io/docs/configuration/variables.html

Page 24: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

outputoutput"address"{value="${data.azurerm_public_ip.mypubip.ip_address}"description="TheIPaddressofournewhost"}

Definesdataoutputs.Usedforautomationandcollectinginformation.

https://www.terraform.io/docs/configuration/outputs.html

Page 25: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

localslocals{user_name="user"}

Defineslocalvariablesinsideamodule.

https://www.terraform.io/docs/configuration/locals.html

Page 26: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

modulemodule"akscluster"{source="azure/aks/defaultcluster"version="1.1.0"nodes=6}

Definesaterraformmodule.Variablescanbepassedtothemodule.Versionpinningisrecommended.

https://www.terraform.io/docs/configuration/modules.html

Page 27: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

terraformterraform{required_version=">0.7.0"}

Definesterraformconfiguration.

https://www.terraform.io/docs/configuration/terraform.html

Page 28: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

meta-parametersCanbeappliedtoallresourcedefinitionsregardlessoftype

count(notapplicabletomodules)depends_on(notapplicabletomodules)providerlifecyclecreate_before_destroy

prevent_destroyignore_changes

Page 29: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

count(notapplicabletomodules)resource"azurerm_virtual_machine""main"{count=3#creates3virtualmachinesname="VM-${count.index}"}

Createsmultipleinstancesoftheresource

Page 30: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

depends_on(notapplicabletomodules)resource"azurerm_virtual_machine""main"{#forcescreationofmodulecomponentsbeforethisVMdepends_on=["${module.aks.fqdn}"]}

Createsadependencywhendefaultdependencymanagementfails

Page 31: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

providerprovider"azurerm"{alias="us"location="westus"version="~>1.23.0"}

resource"azurerm_virtual_machine""us"{#forceusageofthewestus-providerprovider="azurerm.us"}

Specifiestheprovidertouse.Makesmostsensewhenthesameproviderisusedmultipletimes(https://www.terraform.io/docs/configuration/resources.html#multiple-provider-instances)

Page 32: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

lifecycleLifecyclebehaviouroftheresource.Knows3attributes:

create_before_destroyprevent_changesignore_changes

Page 33: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

create_before_destroyresource"azurerm_dns_a_record""website"{create_before_destroy=true}

Forcecreationofanewresourcebeforetheoldresourceisdeleted.UsefulforexampleforDNSrecords.

Page 34: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

prevent_destroyresource"azurerm_kubernetes_cluster""main"{prevent_destroy=true}

Anyplanthatwantstodestroythisresourcewillfail.

Page 35: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

ignore_changesresource"azurerm_kubernetes_cluster""main"{ignore_changes=["vm_size"]}

Whenoneofthespecifiedattributeschangenoactionwillbetaken.

Page 36: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

ProvidersAproviderconnectsterraformconfigurationwithacorrespondingAPI

Page 37: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

DifferentProviders

AzureAWSGoogleCloudGithubKubernetesPowerDNSMySQLmuchmore...https://www.terraform.io/docs/providers/index.html

Page 38: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

FirststepswithGithub

Page 39: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

main.tfprovider"github"{token="${var.github_token}"organization="${var.github_organization}"version="~>1.3"}

resource"github_repository""tf-example"{name="${var.github_repo_name}"description="${var.github_repo_desc}"}

Page 40: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

variables.tfvariable"github_token"{

default="abcd"}

variable"github_organization"{default="myorga"}

variable"github_repo_name"{default"tf-test"}

variable"github_repo_desc"{default"test-repoforterraform"}

Page 41: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

myrepo.tfvarsgithub_token="w841ce33fcfc1wde71fdb50c4dd852c63108b118"github_orga="adfinis-sygroup"github_repo_name="tf-test"github_repo_desc="testdescription"

Page 42: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Tryit!terraforminitterraformplan-var-file=myrepo.tfvarsterraformapply-var-file=myrepo.tfvars

Page 43: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Moreinformationhttps://www.terraform.io/docs

Page 44: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most

Attribution/LicenseSlidesAdfinisSyGroupAG,2017,Attribution-NonCommercial2.0(CCBY-NC2.0)

Page 46: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help]  [args] The available commands for execution are listed below. The most