terraform basics - docs.adfinis.com · check > terraform usage: terraform [-version] [-help] ...
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/42.jpg)
Tryit!terraforminitterraformplan-var-file=myrepo.tfvarsterraformapply-var-file=myrepo.tfvars
![Page 44: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help] [args] The available commands for execution are listed below. The most](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/44.jpg)
Attribution/LicenseSlidesAdfinisSyGroupAG,2017,Attribution-NonCommercial2.0(CCBY-NC2.0)
![Page 45: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help] [args] The available commands for execution are listed below. The most](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/45.jpg)
FeelFreetoContactUswww.adfinis-sygroup.ch
TechBlog
GitHub
![Page 46: Terraform Basics - docs.adfinis.com · Check > terraform Usage: terraform [-version] [-help] [args] The available commands for execution are listed below. The most](https://reader034.vdocuments.mx/reader034/viewer/2022052521/609e29f5519df6151c30f159/html5/thumbnails/46.jpg)