Skip to content

Instantly share code, notes, and snippets.

@omarismail
Last active September 18, 2023 18:06
Show Gist options
  • Save omarismail/3c24e318412acbcaf042e44c2054bf4e to your computer and use it in GitHub Desktop.
Save omarismail/3c24e318412acbcaf042e44c2054bf4e to your computer and use it in GitHub Desktop.
provider-functions
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
locals {
arn_parts = provider::aws::parse_arn("arn:aws:s3:::examplebucket/developers/design_info.doc")
}
output "arn_service" {
value = local.arn_parts.service
}
# Another ex: provider::time::iso8601(time_static.example.rfc3339)
@l-with
Copy link

l-with commented Sep 18, 2023

The output typing should work in the same way as for resources and datasources and their attributes. The error should also be implemented in the same way as for other provider functionalities.

My thoughts to the example:

Parsing an arn im my opinion could be implemented with existing functions in terraform and thus can be implemented as part of a terraform module.

This approach matches good to functions that have a strong relation to a terraform provider and rather not to functions that are very general usable without a strong relation to a single provider.

The go implementation of parsing arn is not a direct part of the terraform provider aws. It is inside the go package arn.

The acceptance factor will depend on the overhead to integrate an already implemented go function into a provider implementation (lines of code). I take the liberty of naming the extreme here - the best would be a meta tool for generating the terraform provider integration of the function -:

terraform-generate-provider-func github.com/aws/aws-sdk-go/aws/arn Parse

Since go has no dynamic load of packages "terraform-generate-provider-func" would need at least two steps.

I am aware that this is not very realistic, but having an ideal goal in mind can help to lead to the right ideas.

@omarismail
Copy link
Author

Looks good, but would it support default/mandatory parameters values?

The arguments defined by the provider function declaration will all be required arguments to pass into the function call. There won't be the notion of optional values, at least at in the beginning.

Can parameters be passed from resource, data output blocks? or variables?

Yes

Would dependencies work with functions? For example if I need the output of a function to be used somewhere else like a resource block.

Yes

Also, we do not need to set the output type. I am assuming it inherits based on the function output?

No, the output type is defined by the Provider Function definition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment