Creating a dynamic yaml file by passing in variables

Viewed 584

I have a YAML file that I want to run to create a custom ENI in our AWS vpc:

apiVersion: crd.k8s.amazonaws.com/v1alpha1
kind: ENIConfig
metadata: 
  name: var.az
spec: 
  securityGroups: 
    - var.sg-eks
  subnet: var.subnet-expand-id

I want to create a terraform module that will dynamically create multiple of these yaml file by passing in the variables for securityGroup, subnet and metadata.name from our .tfvars file.

For example let's say I have a variable var "subnet" ={subnet_id1, subnet_id2, subnet_id3} and var "name" = {az1,az2,az3} I would want 3 different YAML files with the corresponding subnet_id along with the AZ it is in passed into the YAML file, then I would want to run kubectl apply -f on the generated files.

I was thinking about creating a template file like this:

%{ for subnet in subnets ~}
apiVersion: crd.k8s.amazonaws.com/v1alpha1
kind: ENIConfig
metadata: 
  name: var.az
spec: 
  securityGroups: 
    - var.sg-eks
  subnet: ${subnet}
%{ endfor ~}

So I'm expecting the above to generate different yaml files by iterating through the given list of subnets, name, and security groups; so I need some sort of nested for loop? However, I am not sure if I have the right idea or I'm completely off.

Then I plan on creating a null resource that should trigger kubectl apply -f on these yaml files.

Any guidance would be appreciated

1 Answers

You could try with https://kustomize.io, it is the tool to go with (and it is standard for Kubernetes).

Related