custom-terraform

Allows user to bring in their own custom terraform module

This module allows a user to bring in their own custom terraform code into the opta ecosystem, to use in tandem with their other opta modules, and even reference them. All a user needs to do is specify the source to your module with the source input, and the desired inputs to your module (if any) via the terraform_inputs input.

Use local terraform files

Let’s create a AWS EC2 instance using terraform.

Here is the directory structure for this example:

.
├── opta.yaml       # the opta environment file for AWS
├── custom-tf.yaml  # define the opta custom-terraform module
└── my-terraform    # place all the terraform files in this directory
    └── main.tf

Run opta apply to create the custom terraform resources:

opta apply -c custom-tf.yaml
╒══════════╤══════════════════════════╤══════════╤════════╤══════════╕
│ module   │ resource                 │ action   │ risk   │ reason   │
╞══════════╪══════════════════════════╪══════════╪════════╪══════════╡
│ customtf │ aws_instance.my_instance │ create   │ LOW    │ creation │
╘══════════╧══════════════════════════╧══════════╧════════╧══════════╛
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Once you opta apply the service you should see your new EC2 instance up and running in the AWS console and be able to ssh into it.

When done, you can destroy the custom terraform resource:

opta destroy -c custom-tf.yaml

Use a remote terraform module

The source input uses terraform’s module source logic behind the scenes and so follows the same format/limitations. Thus, you can use this for locally available modules, or modules available remotely, like so:

environments:
  - name: staging
    path: "../opta.yaml"
name: customtf
modules:
  - type: custom-terraform
    name: bucket
    source: "terraform-aws-modules/s3-bucket/aws" # See https://registry.terraform.io/modules/terraform-aws-modules/s3-bucket/aws/latest
    version: "2.13.0" # version needs to be specified for remote registry modules
    terraform_inputs:
      bucket: "dummy-bucket-{aws.account_id}"
      acl: "private"
      versioning: 
        enabled: true

WARNING Be very, very, careful about what remote modules you are using, as they leave you wide open to supply chain attacks, depending on the security and character of the owner of said module. It’s highly advised to use either official modules or modules under your company’s control.

Using Outputs from your Custom Terraform Module

Currently you can use outputs of your custom terraform module in the same yaml, like so:

environments:
  - name: staging
    path: "../opta.yaml"
name: customtf
modules:
  - type: custom-terraform
    name: tf1
    source: "./my-terraform-1" # <-- This module has an output called output1
  - type: custom-terraform
    name: tf2
    source: "./my-terraform-2"
    terraform_inputs:
      input1: "${{module.tf1.output1}}" # <-- HERE. Note the ${{}} wrapping

These outputs, however, currently can not be used in other yamls (e.g. if you put custom terraform in an environment yaml its outputs can’t be used in the services), and will not show up in the opta output command. Work on supporting this is ongoing.

Fields

Name Description Default Required
source The source of your terraform module. For more info, check out https://www.terraform.io/language/modules/sources#module-sources None False
path_to_module Deprecated, use source None False
version The version of the remote module to use. For more info, check out https://www.terraform.io/language/modules/syntax#version None False
terraform_inputs The variables which you wish to pass into your custom module. {} False

Last modified May 23, 2022 : Docs for new release (#191) (ba6432d)