OCI Terraform
OCI Terraform
OCI Terraform
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-2
Objectives
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-3
Quick Introduction of Terraform
• Terraform is written by the team at Hashicorp.
• “Infrastructure as Code” tool for building and managing infrastructure efficiently and
elegantly.
• Terraform - Create, combine and manage infrastructure across multiple providers
• Terraform also integrates with configuration management and provisioning tools like Chef,
Puppet and Ansible.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-4
Terraform State file
• Terraform stores the state of your managed infrastructure from the last time
Terraform was run.
• Terraform uses this state to create plans and make changes to your
infrastructure.
terraform.tfstate
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-5
Terraform Local State File
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-6
Terraform Remote State File
• Writes the state data to a remote data store
Configuring and using remote backends is easy and you can get it configured
with Object Storage:
terraform {
backend "http" {
update_method = "PUT"
address = "https://objectstorage.<region>.oraclecloud.com/<my-access-uri>"
}
}
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-7
Terraform – Targeting resources
• You can use the -target flag on both the terraform plan and terraform apply
commands.
• It allows you to target a resource or more if you specify multiple -target flags
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-8
Terraform Modules
• Portable Terraform configurations (packages)
• Allow separation of concerns and responsibilities among teams
• Modules are just Terraform configurations inside a folder
module "vcn" {
source = "./vcn"
compartment_ocid = "${var.compartment_ocid}"
tenancy_ocid = "${var.tenancy_ocid}"
vcn_dns_name = "${var.vcn_dns_name}"
label_prefix = "${var.label_prefix}"
vcn_name = "${var.vcn_name}"
vcn_cidr = "${var.vcn_cidr}"
subnet_cidr = "${var.subnet_cidr}"
availability_domains = "${var.availability_domains}"
}
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 1-9
Terraform Provisioners
• Provisioners are used to execute scripts on a local or remote machine as part of resource
creation or destruction.
• Provisioners can be used to bootstrap a resource, cleanup before destroy, run
configuration management, etc.
• Terraform can also integrate with provisioners like Chef, puppet, Ansible, shells scripts.
provisioner "local-exec" {
command = "ansible-playbook -i '${self.public_ip},' --private-key
${var.ssh_private_key} setup.yml" }
provisioner "local-exec" {
command = "echo ${oci_core_instance.web.private_ip} >> private_ips.txt"
}
provisioner "remote-exec" {
connection {
agent = false
timeout = "10m"
host = "${data.oci_core_vnic.InstanceVnic.public_ip_address}"
user = "opc"
private_key = "${var.ssh_private_key}"
}
inline = [
"touch ~/IMadeAFile.Right.Here",
]
}
}
provider "oci" {
auth = "InstancePrincipal"
region = "${var.region}"
}
cloud.oracle.com/tryit