{"id":1124,"date":"2017-09-25T16:12:29","date_gmt":"2017-09-25T21:12:29","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1124"},"modified":"2017-09-26T08:33:00","modified_gmt":"2017-09-26T13:33:00","slug":"compute-instance-in-oci-using-terraform","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/compute-instance-in-oci-using-terraform\/","title":{"rendered":"Compute Instance in OCI using terraform"},"content":{"rendered":"

Begin Update 9\/26\/17.<\/strong><\/p>\n

It is possible to reference the subnet as follow:<\/p>\n

\r\nsubnet_id = "${oci_core_subnet.PrivSubnetAD1.id}"\r\n<\/pre>\n

My problem and workaround originally is because I am using modules. I would prefer modules so I can organize and subdivide better but it caused above to not work. Plus subdividing the work may cause more issues around losing access to variables\/references.
\nEnd Update 9\/26\/17<\/strong><\/p>\n

Most likely there is a better way to do this but since I spent some time on it I am jotting down my notes. When creating compute instances with terraform in Oracle Cloud Infrastructure(Oracle Bare Metal Services) you have to specify the subnet_id. The id or ocid as called in OCI is a long unique string.<\/p>\n

So if you are looking at automating the terraform build you may struggle with not knowing subnet_id when creating a compute instance. As I said there may be better ways to do this and maybe the AWS plugin for terraform handles this already. I did this with the OCI plugin and came up with the below script and using some custom API calls to create terraform variables of the subnets. Just showing the bash script for some ideas on flow and how it glues together. The terraform source and api calls not shown here.<\/p>\n

\r\n#!\/bin\/bash\r\n\r\nbuild_root="\/home\/rrosso\/.terraform.d\/MY-PROTO"\r\napi_snippets_location="\/home\/rrosso\/oraclebmc"\r\nterraform_bin="\/home\/rrosso\/.terraform.d\/terraform"\r\n\r\nfunction pause(){\r\n   read -p "$*"\r\n}\r\n\r\n## Check if environment variables are set. Make very sure correct tenancy, compartment etc\r\n## Or if hard coding these look in main.tf files for correct tenancy, compartment etc\r\nenv | grep TF_\r\n\r\npause "Press [Enter] key if Variables look ok and if ready to proceed with networking build"\r\n\r\ncd "$build_root\/networking"\r\n$terraform_bin apply\r\n\r\npause "Press [Enter] key if networking build went ok and ready to generate a list of subnet ocid's"\r\n\r\ncd "$api_snippets_location"\r\npython get_subnet_ocids.py -t ocid1.tenancy.oc1..<cut_long_number_here> -c MYPROTO -v DEV >> $build_root\/compute\/webservers\/variables.tf \r\npython get_subnet_ocids.py -t ocid1.tenancy.oc1..<cut_long_number_here> -c MYPROTO -v DEV >> $build_root\/compute\/bastions\/variables.tf \r\n\r\npause "Press [Enter] key if variables.tf looks ok with new subnet ocid's and ready to proceed building compute instances"\r\n\r\ncd "$build_root\/compute"\r\n$terraform_bin apply\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Begin Update 9\/26\/17. It is possible to reference the subnet as follow: subnet_id = "${oci_core_subnet.PrivSubnetAD1.id}" My problem and workaround originally<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87,89,88],"tags":[],"class_list":["post-1124","post","type-post","status-publish","format-standard","hentry","category-oci","category-oracle-bare-metal-cloud-services","category-terraform"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}