3 Easy Ways to Get Started with OCI Stacks

oci terraform Sep 09, 2023

We have spent a lot of time talking about HashiCorp and Terraform over the past few years. Terraform is the awesome "infrastructure as code" tool for cloud infrastructure automation. While it is powerful, there can be a learning curve for teams learning to use it effectively. If you and your team are trying to start using Terraform with OCI, one way to get a jump start is to use Stacks.

Stacks are a part of OCI Resource Manager and allow you to automate provisioning using Terraform. This is basically adding another layer on top of Terraform to help manage your code and deployment process. You can interact with a Stack via the OCI Console, CLI, or REST API. There are even hooks to git repositories like GitHub and GitLab. The use of a drift detection report is another cool feature that shows any differences between your Terraform and what is actually deployed in OCI.

Not only do Stacks provide more management options, but they also can generate Terraform code for you! This is great for many reasons, but I think it really shines for teams just learning about Terraform. Below are 3 easy ways to have OCI Stacks generate Terraform code for you!

Create an Instance Stack

One thing OCI makes incredibly easy is creating an Instance. You can open the Create compute instance page, add an SSH key, then click the Create button and be done. That is like 3 clicks! Of course, most of the time we will want to create our Instance with a little more information. This is done easily enough through the Console page but let's try this in Terraform.

  • Go through the same process you normally would when creating an Instance via the Console. However, at the very end click the Save as stack button instead.

  • Enter the name, description, compartment, and Terraform version information, then click Next.

  • On the Configure Variables page, click Next and on the Review page click Create.

You now have a stack created with all the Terraform code needed to create an Instance! To use the stack, use the Plan, Apply, or Destroy actions. There are other actions as well under the More Actions dropdown. The drift detection report is found there.

To review the Terraform code that was generated, click on Download under Stack Information: Terraform Configuration. This will download a zip file containing the Terraform configuration for this stack. For this example, it should be a single main.tf file. Open this file in a text editor to review. You should find everything needed to create the instance and related resources you specified on the Console page.

You can now take this Terraform code and adjust it as you'd like. When you are ready to test some changes, go to Stack Information: Terraform Configuration and click Upload. Another option is to create a new Stack and select My Configuration to upload it there.

Create a Stack from Template

We have created a basic Instance as a stack, but what if want and Instance that is preconfigured for us. Maybe we want an Instance with WordPress deployed for us automatically. OCI provides a large number of stack templates to choose from. You can even create your own personal Private Templates if you'd like.

Using the OCI Console, navigate to Developer Services > Resource Manager > Stacks. Click on Create stack to bring up the creation wizard. In the Stack information section you will be prompted to select the type of configuration. Select the Template option and then click Select template. Browse through the list of templates and select one. For this example we will select Deploy a WordPress instance on OCI and then click Select template.

Click Next and then fill out the Configure variables section. For our example we need to provide WordPress administrator username and password. Click Next and then review our stack information. Finally click on Create and your stack will be ready to go! Just like the previous example, we can now plan, apply, destroy or just review this stack.

Create a Stack from Compartment

Creating a basic Instance or a templated set of resources is great, but what if you have already configured resources in OCI and would like that converted to a Stack? Depending on the situation it might be easiest to create a stack from an existing compartment.

Just like before, navigate to Developer Services > Resource Manager > Stacks. Click on Create stack to bring up the creation wizard. This time in the Stack information section you will select Existing compartment. You will then need to provide some information to assist in the resource discovery. Select the compartment and region where your resources are located. If you only want certain types of resources discovered, you can pick and choose which provider services to use. Otherwise just select all and everything in the compartment will be discovered and added to your new stack.

Click Next and your should find nothing for Configure variables. Click Next once more to review and your are ready to Create!

Conclusion

Those are just 3 quick examples of how to get started with Terraform and Resource Manager Stacks. There are obviously more robust solutions for dealing with Terraform but I feel stacks can be pretty powerful. Whether you are looking to just generate some Terraform code to review and learn or trying to seriously manage some real infrastructure, Stacks are worth a look!