This article discusses the use of Placeholders in Resource Definitions to dynamically allocate values to configuration using the Humanitec Platform Orchestrator. It is intended for platform engineers and developers who want to learn more about Resource Definitions and Placeholders and how they can make Resource Definition management more efficient and manageable.
Placeholders
Placeholders are used to allocate values dynamically to the configuration in Humanitec. This is handy when the values are not known during configuration or want them to be flexible depending on Resource outputs, for example, naming a Workload.
Using Placeholders allows for a less-is-more principle of Resource Definitions management in Humanitec. As you will see with our namespace example, even if you have 37 different namespaces in your dev and staging clusters, you only need one or two namespace Resource Definitions in Humanitec powering them. The same goes for any of the standard Resource Definitions, even those that are Terraform-based.
Resource Definitions
Resource Definitions are used to:
- Configure how Resources are provisioned. By provisioning, we mean making a Resource available when Workloads are deployed.
- Define Matching Criteria for the Resource Definition. This specifies which Resource Definition to select for a particular context. For example, you can provision a Postgres DB for a Workload in AWS for a Workload in the development Environment but use GCP for your production Environment.
There are various types of Resource Definitions depending on the Driver they use. The following are a few notable examples:
- Terraform: Uses Terraform module to create Resources whenever this definition is used.
- Static: It does not create Resources but returns pre-canned static values without creating or updating infrastructure. These can be used for Workloads to connect to Resources.
- Driver-specific: Humanitec comes bundled with Drivers, such as S3, or Workload that can create Resources of various types.
Overview
In this tutorial, you will:
- Create a static namespace Resource Definition with Placeholders ${context.app.id} and ${context.env.id}
- Assign this Resource Definition a global Matching Criteria, so it applies to all Applications
This means all your Workloads will be placed in a namespace named concatenating the Application id and the Environment id.
Prerequisites
Before starting this tutorial, you’ll need a Humanitec account (if you don’t have an account yet, explore the full functionality of Humanitec and test it free for 45 days).
Personas
This tutorial is ideal for the following:
- Platform Engineers looking to understand how Resource Definitions work
- Developers who want to learn where Workloads are deployed based on Resource Definitions and Placeholders
Step 1: Create a static namespace Resource Definition
Go to the Resource Management section and click on Add Resource Definition
Then select Kubernetes namespace
Select static
Enter the following:
ID:
Kubernetes namespace:
and then select Add Kubernetes Namespace
Step 2: Add Criteria for the Resource Definition.
Go into Matching criteria for the Resource Definition called ns-Placeholder-tutorial we just created and click on Add new criteria
Don’t enter anything; select Save
This will apply all criteria to this Resource Definition, meaning all your Workloads will use this namespace. If you want to be more specific, add different criteria here.
This applies to all criteria:
In this step, you have:
- Created a Resource Definition with a static Driver
- Configured it to use the Application id + Environment id to name the namespace where the Workloads will be allocated. By default, these namespaces are a randomly generated UUID.
- Configured Matching Criteria so that all our Workloads in all apps use this Resource Definition and name the namespaces accordingly
Step 3: Create an App
Go to Applications and select Create new app with the following name:
In this step you have:
- Created an App. This is where Workloads are logically grouped. Generally, developers are assigned an Application where their microservices live and connect with each other.
Step 4: Create a Workload
Go into your app and click on Create new Workload
Use the following name for your Workload
Step 5: Configure your Workload
Click on Create new container
Add the following image name for it
And click on Add image
For the container name, pick anything you want and click on Create:
Now go to Variables and add a variable with key
and Value
Click on Save:
Next, go to Command overrides and click on Edit YAML and enter the following code and click on Save
Finally go to Argument overrides click on Edit YAML add the following code and click on Save:
In this step you have:
- Created a Workload with a simple alpine container
- Defined a variable with a Placeholder that gives the value of the namespace being used.
- Defined command and argument overrides to output the variable so that it shows the namespace being used. (You can also do this by viewing the shared Resources the Workload is using, but this makes it easier to showcase it.)
Step 6: Deploy the Workload
Go back to your Application and click on Deploy
For the comment, write whatever you want:
After a short while, your Workload should show as running:
The output should show that the Workload was deployed to the correct namespace:
In this step, we have deployed the Workload and verified the namespace in the log output.
Learn more
Conclusion
In this tutorial, you learned about the importance of Resource Definitions and how they are used to provision Resources when deployed Workloads. You also learned about Placeholders, which can be used to allocate values dynamically to the configuration in Humanitec. We created a static namespace Resource Definition with Placeholders and granted it a global Matching Criteria, so it applies to all Applications. We then created a Workload and verified that it was deployed to the correct namespace.
Understanding Resource Definitions and Placeholders is essential for both platform engineers and developers. It allows for more flexible and dynamic Resource allocation, reducing the need for many Resource Definitions. This, in turn, makes Resource Definition management more efficient and manageable.
Next, you can explore other Resource Drivers and learn how to use them to provision Resources. You can also learn how to deploy Score Workloads with Humanitec, giving you practical experience in deploying Workloads with Resource Definitions and Placeholders.
Start exploring the power of Resource Definitions and Placeholders today! If you don’t have an account yet, start a free trial or schedule a demo.
‍