Terraform Tutorial
This page contains additional notes (based on testing on Oracle Linux 9) for the Terraform tutorial described here...
https://developer.hashicorp.com/terraform/tutorials/oci-get-started/install-cliThis page assumes Terraform has been installed
Build a Docker Container running NGINX
This section assumes Docker is installed and running
mkdir learn-terraform-docker-container
cd learn-terraform-docker-container
vi main.tf
provider "docker" {}
resource "docker_image" "nginx" { name = "nginx" keep_locally = false}
resource "docker_container" "nginx" { image = docker_image.nginx.image_id name = "tutorial"
ports { internal = 80 external = 8000 }}
terraform init
Initializing provider plugins...- Finding kreuzwerker/docker versions matching "~> 3.0.1"...- Installing kreuzwerker/docker v3.0.2...- Installed kreuzwerker/docker v3.0.2 (self-signed, key ID BD080C4571C6104C)
Partner and community providers are signed by their developers.If you'd like to know more about provider signing, you can read about it here:https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the providerselections it made above. Include this file in your version control repositoryso that Terraform can guarantee to make the same selections by default whenyou run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work.
If you ever set or change modules or backend configuration for Terraform,rerun this command to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do so if necessary.
sudo terraform apply
Terraform will perform the following actions:
# docker_container.nginx will be created + resource "docker_container" "nginx" { + attach = false + bridge = (known after apply) + command = (known after apply) + container_logs = (known after apply) + container_read_refresh_timeout_milliseconds = 15000 + entrypoint = (known after apply) + env = (known after apply) + exit_code = (known after apply) + hostname = (known after apply) + id = (known after apply) + image = (known after apply) + init = (known after apply) + ipc_mode = (known after apply) + log_driver = (known after apply) + logs = false + must_run = true + name = "tutorial" + network_data = (known after apply) + read_only = false + remove_volumes = true + restart = "no" + rm = false + runtime = (known after apply) + security_opts = (known after apply) + shm_size = (known after apply) + start = true + stdin_open = false + stop_signal = (known after apply) + stop_timeout = (known after apply) + tty = false + wait = false + wait_timeout = 60
+ ports { + external = 8000 + internal = 80 + ip = "0.0.0.0" + protocol = "tcp" } }
# docker_image.nginx will be created + resource "docker_image" "nginx" { + id = (known after apply) + image_id = (known after apply) + keep_locally = false + name = "nginx" + repo_digest = (known after apply) }
Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.
Enter a value: yes
docker_image.nginx: Creating...docker_image.nginx: Still creating... [10s elapsed]docker_image.nginx: Creation complete after 13s [id=sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24nginx]docker_container.nginx: Creating...docker_container.nginx: Creation complete after 1s [id=2d14c09db09e288f3b266091018c9209e957cf94a44b59e0bdcb0a28e4e6b7e4]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
sudo docker ps
You should now be able to connect to nginx in your web browser using: http://localhost:8000
sudo terraform destroy
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy
Terraform will perform the following actions:
# docker_container.nginx will be destroyed - resource "docker_container" "nginx" { - attach = false -> null - command = [ - "nginx", - "-g", - "daemon off;", ] -> null - container_read_refresh_timeout_milliseconds = 15000 -> null - cpu_shares = 0 -> null - dns = [] -> null - dns_opts = [] -> null - dns_search = [] -> null - entrypoint = [ - "/docker-entrypoint.sh", ] -> null - env = [] -> null - group_add = [] -> null - hostname = "2d14c09db09e" -> null - id = "2d14c09db09e288f3b266091018c9209e957cf94a44b59e0bdcb0a28e4e6b7e4" -> null - image = "sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24" -> null - init = false -> null - ipc_mode = "private" -> null - log_driver = "json-file" -> null - log_opts = {} -> null - logs = false -> null - max_retry_count = 0 -> null - memory = 0 -> null - memory_swap = 0 -> null - must_run = true -> null - name = "tutorial" -> null - network_data = [ - { - gateway = "172.17.0.1" - global_ipv6_address = "" - global_ipv6_prefix_length = 0 - ip_address = "172.17.0.3" - ip_prefix_length = 16 - ipv6_gateway = "" - mac_address = "02:42:ac:11:00:03" - network_name = "bridge" }, ] -> null - network_mode = "default" -> null - privileged = false -> null - publish_all_ports = false -> null - read_only = false -> null - remove_volumes = true -> null - restart = "no" -> null - rm = false -> null - runtime = "runc" -> null - security_opts = [] -> null - shm_size = 64 -> null - start = true -> null - stdin_open = false -> null - stop_signal = "SIGQUIT" -> null - stop_timeout = 0 -> null - storage_opts = {} -> null - sysctls = {} -> null - tmpfs = {} -> null - tty = false -> null - wait = false -> null - wait_timeout = 60 -> null
- ports { - external = 8000 -> null - internal = 80 -> null - ip = "0.0.0.0" -> null - protocol = "tcp" -> null } }
# docker_image.nginx will be destroyed - resource "docker_image" "nginx" { - id = "sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24nginx" -> null - image_id = "sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24" -> null - keep_locally = false -> null - name = "nginx" -> null - repo_digest = "nginx@sha256:104c7c5c54f2685f0f46f3be607ce60da7085da3eaa5ad22d3d9f01594295e9c" -> null }
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
docker_container.nginx: Destroying... [id=2d14c09db09e288f3b266091018c9209e957cf94a44b59e0bdcb0a28e4e6b7e4]docker_container.nginx: Destruction complete after 0sdocker_image.nginx: Destroying... [id=sha256:eea7b3dcba7ee47c0d16a60cc85d2b977d166be3960541991f3e6294d795ed24nginx]docker_image.nginx: Destruction complete after 1s
Destroy complete! Resources: 2 destroyed.
Build an OCI VCN
oci session authenticate
Enter learn-terraform for your profile name.The token has a 60 minute TTL. If you need to, refresh using...
oci session refresh --profile learn-terraform
mkdir learn-terraform-oci
cd learn-terraform-oci
touch main.tf
provider "oci" { region = "uk-london-1" auth = "SecurityToken" config_file_profile = "learn-terraform"}
resource "oci_core_vcn" "internal" { dns_label = "internal" cidr_block = "172.16.0.0/20" compartment_id = "<your_tenancy_OCID_here>" display_name = "My internal VCN"}
terraform init
Initializing provider plugins...- Finding latest version of oracle/oci...- Installing oracle/oci v5.11.0...- Installed oracle/oci v5.11.0 (signed by a HashiCorp partner, key ID 1533A49284137CEB)
Partner and community providers are signed by their developers.If you'd like to know more about provider signing, you can read about it here:https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the providerselections it made above. Include this file in your version control repositoryso that Terraform can guarantee to make the same selections by default whenyou run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work.
If you ever set or change modules or backend configuration for Terraform,rerun this command to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do so if necessary.
terraform fmt
terraform validate
Applies a consistent format to all configurations in the current directory
Ensures the configuration is syntactically valid and internally consistent
terraform apply
Terraform will perform the following actions:
# oci_core_vcn.internal will be created + resource "oci_core_vcn" "internal" { + byoipv6cidr_blocks = (known after apply) + cidr_block = "172.16.0.0/20" + cidr_blocks = (known after apply) + compartment_id = "ocid1.tenancy.oc1..aaaaaaaahdt5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy5dca" + default_dhcp_options_id = (known after apply) + default_route_table_id = (known after apply) + default_security_list_id = (known after apply) + defined_tags = (known after apply) + display_name = "My internal VCN" + dns_label = "internal" + freeform_tags = (known after apply) + id = (known after apply) + ipv6cidr_blocks = (known after apply) + ipv6private_cidr_blocks = (known after apply) + is_ipv6enabled = (known after apply) + is_oracle_gua_allocation_enabled = (known after apply) + state = (known after apply) + time_created = (known after apply) + vcn_domain_name = (known after apply) }
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.
Enter a value: yes
oci_core_vcn.internal: Creating...oci_core_vcn.internal: Creation complete after 1s [id=ocid1.vcn.oc1.uk-london-1.amaaaaaacyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyzqiq6q]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform show
terraform state list
oci_core_vcn.internal
Add a subnet
cd ~/learn-terraform-oci
vi main.tf
terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create
Terraform will perform the following actions:
# oci_core_subnet.dev will be created + resource "oci_core_subnet" "dev" { + availability_domain = (known after apply) + cidr_block = "172.16.0.0/24" + compartment_id = "ocid1.tenancy.oc1..aaaaaaaahdt5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy5dca" + defined_tags = (known after apply) + dhcp_options_id = (known after apply) + display_name = "Dev subnet 1" + dns_label = "dev" + freeform_tags = (known after apply) + id = (known after apply) + ipv6cidr_block = (known after apply) + ipv6cidr_blocks = (known after apply) + ipv6virtual_router_ip = (known after apply) + prohibit_internet_ingress = (known after apply) + prohibit_public_ip_on_vnic = true + route_table_id = (known after apply) + security_list_ids = (known after apply) + state = (known after apply) + subnet_domain_name = (known after apply) + time_created = (known after apply) + vcn_id = "ocid1.vcn.oc1.uk-london-1.amaaaaaacyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyqiq6q" + virtual_router_ip = (known after apply) + virtual_router_mac = (known after apply) }
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.
Enter a value: yes
oci_core_subnet.dev: Creating...oci_core_subnet.dev: Creation complete after 2s [id=ocid1.subnet.oc1.uk-london-1.aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt56sa]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Change Subnet Name
cd ~/learn-terraform-oci
vi main.tf
terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place
Terraform will perform the following actions:
# oci_core_subnet.dev will be updated in-place ~ resource "oci_core_subnet" "dev" { ~ display_name = "Dev subnet 1" -> "Dev subnet" id = "ocid1.subnet.oc1.uk-london-1.aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt56sa" # (17 unchanged attributes hidden) }
Plan: 0 to add, 1 to change, 0 to destroy.
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.
Enter a value: yes
oci_core_subnet.dev: Modifying... [id=ocid1.subnet.oc1.uk-london-1.aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt56sa]oci_core_subnet.dev: Modifications complete after 0s [id=ocid1.subnet.oc1.uk-london-1.aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt56sa]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.