Develop Stack Template ¶
Cluster.dev gives you freedom to modify existing templates or create your own. You can add inputs and outputs to already preset units, take the output of one unit and send it as an input to another, or write new units and add them to a template.
In our example we shall use the tmpl-development sample to create a K3s cluster on AWS. Then we shall modify the project stack template by adding new parameters to the units.
Workflow steps ¶
-
Create a project following the steps described in Create Own Project section.
-
To start working with the stack template, cd into the template directory and open the
template.yamlfile: ./template/template.yaml.Our sample stack template contains 3 units. Now, let's elaborate on each of them and see how we can modify it.
-
The
create-bucketunit uses a remote Terraform module to create an S3 bucket on AWS:name: create-bucket type: tfmodule providers: *provider_aws source: terraform-aws-modules/s3-bucket/aws version: "2.9.0" inputs: bucket: {{ .variables.bucket_name }} force_destroy: trueWe can modify the unit by adding more parameters in inputs. For example, let's add some tags using the
insertYAMLfunction:name: create-bucket type: tfmodule providers: *provider_aws source: terraform-aws-modules/s3-bucket/aws version: "2.9.0" inputs: bucket: {{ .variables.bucket_name }} force_destroy: true tags: {{ insertYAML .variables.tags }}Now we can see the tags in
stack.yaml:name: cdev-tests-local template: ./template/ kind: Stack backend: aws-backend variables: bucket_name: "tmpl-dev-test" region: {{ .project.variables.region }} organization: {{ .project.variables.organization }} name: "Developer" tags: tag1_name: "tag 1 value" tag2_name: "tag 2 value"To check the configuration, run
cdev plan --tf-plancommand. In the output you can see that Terraform will create a bucket with the defined tags. Runcdev apply -l debugto have the configuration applied. -
The
create-s3-objectunit uses local Terraform module to get the bucket ID and save data inside the bucket. The Terraform module is stored in s3-file directory,main.tffile:name: create-s3-object type: tfmodule providers: *provider_aws source: ./s3-file/ depends_on: this.create-bucket inputs: bucket_name: {{ remoteState "this.create-bucket.s3_bucket_id" }} data: | The data that will be saved in the S3 bucket after being processed by the template engine. Organization: {{ .variables.organization }} Name: {{ .variables.name }}The unit sends 2 parameters. The bucket_name is retrieved from the
create-bucketunit by means ofremoteStatefunction. The data parameter uses templating to retrieve the Organization and Name variables fromstack.yaml.Let's add to data input bucket_regional_domain_name variable to obtain the region-specific domain name of the bucket:
name: create-s3-object type: tfmodule providers: *provider_aws source: ./s3-file/ depends_on: this.create-bucket inputs: bucket_name: {{ remoteState "this.create-bucket.s3_bucket_id" }} data: | The data that will be saved in the s3 bucket after being processed by the template engine. Organization: {{ .variables.organization }} Name: {{ .variables.name }} Bucket regional domain name: {{ remoteState "this.create-bucket.s3_bucket_bucket_regional_domain_name" }}Check the configuration by running
cdev plancommand; apply it withcdev apply -l debug. -
The
print_outputsunit retrieves data from two other units by means ofremoteStatefunction: bucket_domain variable fromcreate-bucketunit and s3_file_info fromcreate-s3-objectunit: -
Having finished your work, run
cdev destroyto eliminate the created resources.