Skip to main content

Terraform Provisioner

Runs a step with Terraform. Source: provisioner-terraform. For new bundles, prefer the OpenTofu provisioner.

Structure​

The step's path holds a Terraform module.

To use a dependency lock file (.terraform.lock.hcl), keep it in the module directory when you publish, and check it into source control.

State is kept in Massdriver's HTTP state backend, one state per instance and step. The provisioner writes the backend configuration, so the module must not declare a backend.

Actions​

ActionRuns
planterraform plan, then Checkov against the plan
provisionterraform plan, Checkov, terraform apply, then publishes resources
decommissionterraform plan -destroy, terraform apply, then deletes the step's resources

Tooling​

  • Checkov scans the plan for policy and compliance violations on plan and provision.

Configuration​

KeyTypeDefaultDescription
log_levelstringSets TF_LOG: TRACE, DEBUG, INFO, WARN, ERROR, JSON, or OFF.
jsonbooleanfalseAdds -json to plan and apply.
checkov.enablebooleantrueRuns Checkov.
checkov.quietbooleantrueShows only failed checks (--quiet).
checkov.halt_on_failurebooleanfalseFails the deployment on a failed check (drops --soft-fail).
ssh.private_keystringSSH private key for private module repositories. When set, the provisioner rewrites https:// git URLs to SSH.

Inputs​

Terraform reads variables from JSON files, so the provisioner copies params.json and connections.json into the module directory unchanged, as _params.auto.tfvars.json and _connections.auto.tfvars.json. Each param and each dependency is a variable of the same name.

mass bundle build writes _massdriver_variables.tf with a typed variable declaration for each param and dependency. To reshape a value, use a locals block.

Resources​

A step publishes the resources declared in the bundle's resources block in one of two ways.

massdriver_resource​

The massdriver_resource resource of the Massdriver provider:

hcl
resource "massdriver_resource" "bucket" {
field = "bucket"
name = "S3 bucket ${aws_s3_bucket.main.bucket}"
resource = jsonencode({
arn = aws_s3_bucket.main.arn
region = var.region
})
}

field is the resource's name in massdriver.yaml. resource is the payload, which must match the resource type's schema.

jq templates​

After terraform apply, the provisioner renders a jq template for each resource. The template is steps[].resources.<name>.template in massdriver.yaml, or a resource_<name>.jq file in the module directory. See Provisioners: Resources.

The template reads params, connections, envs, secrets, and outputs, where outputs holds the module's outputs.

Take a bucket bundle with one param, one dependency, and one resource:

massdriver.yamlyaml
steps:
- path: src
provisioner: terraform

params:
required:
- region
properties:
region:
type: string

dependencies:
aws_authentication:
resource_type: aws-iam-role@~1
required: true

resources:
bucket:
resource_type: aws-s3-bucket@~1
required: true

Output only the values the resource needs:

hcl
output "bucket_arn" {
value = aws_s3_bucket.main.arn
}

The template's input is:

json
{
"params": {
"region": "us-east-1"
},
"connections": {
"aws_authentication": {
"arn": "arn:aws:iam::012345678910:role/role_name",
"external_id": "s0mes3cr3tv@lue"
}
},
"envs": {},
"secrets": {},
"outputs": {
"bucket_arn": "arn:aws:s3:::the_bucket_name"
}
}

Build the resource in src/resource_bucket.jq:

src/resource_bucket.jqjq
{
"arn": .outputs.bucket_arn,
"region": .params.region
}

Or inline, in massdriver.yaml:

yaml
steps:
- path: src
provisioner: terraform
resources:
bucket:
template: '{arn: .outputs.bucket_arn, region: .params.region}'

To build the whole payload in Terraform instead, output it as an object and return it from the template, such as .outputs.bucket.