Invalid value for "vars" parameter: vars map does not contain key issue

I’m getting error saying Invalid value for “vars” parameter: vars map does not contain key in my lanuch-template:

resource "aws_launch_template" "eks-cluster-launch-template" {
  name = join("", [var.tags.Environment, "-eks-cluster-launch-template"])
  image_id      = data.aws_ami.eks-worker-ami.id
  instance_type = var.node-instance-type
  key_name      = var.key-name

  user_data = base64encode(templatefile(join("", [path.module, "/userdata.tpl"]), {
    eks-cluster-name            = var.eks-cluster-name
    kublet-extra-args           = var.kublet-extra-args

  }))
...

Template file:

#!/bin/bash
set -o xtrace
/etc/eks/bootstrap.sh --apiserver-endpoint '${aws_eks_cluster.eks.endpoint}' --b64-cluster-ca '${aws_eks_cluster.eks.certificate_authority.0.data}' '${var.eks-cluster-name}' --kubelet-extra-args '${var.kublet-extra-args}'

error output from terraform plan:

Error: Invalid function argument

  on modules/eks/launch-template.tf line 7, in resource "aws_launch_template" "eks-cluster-launch-template":
   7:   user_data = base64encode(templatefile(join("", [path.module, "/userdata.tpl"]), {
   8: 
   9: 
  10: 
  11: 
  12: 
  13: 

Invalid value for "vars" parameter: vars map does not contain key
"aws_eks_cluster", referenced at modules/eks/userdata.tpl:3,47-62.

In your templatefile, change:

'${var.eks-cluster-name}' --kubelet-extra-args '${var.kublet-extra-args}'

to:

'${eks-cluster-name}' --kubelet-extra-args '${kublet-extra-args}'

It doesn’t make any difference. Same error.

  Error: Invalid function argument
  
    on modules/eks/launch-template.tf line 7, in resource "aws_launch_template" "eks-cluster-launch-template":
     7:   user_data = base64encode(templatefile(join("", [path.module, "/userdata.tpl"]), {
     8: 
     9: 
    10: 
  
  Invalid value for "vars" parameter: vars map does not contain key
  "aws_eks_cluster", referenced at modules/eks/userdata.tpl:3,47-62.

Ah, found it. You can’t reference ‘${aws_eks_cluster.eks.endpoint}’ directly in your template file. You need to pass that in as a parameter in the same way that you’re passing ‘eks-cluster-name’ in.

Your map needs to be:

{
    eks-cluster-name  = var.eks-cluster-name
    kublet-extra-args = var.kublet-extra-args
    cert-authority    = aws_eks_cluster.eks.certificate_authority.0.data
    eks-endpoint      = aws_eks_cluster.eks.endpoint
}

and then your template file would be:

/etc/eks/bootstrap.sh --apiserver-endpoint '${eks-endpoint}' --b64-cluster-ca '${cert-authority}' '${eks-cluster-name}' --kubelet-extra-args '${kublet-extra-args}'
1 Like

Just to add further information, if you face issues regarding a variable that is created inside the template file then you have 2 solutions for that:

  1. Escape that variable in shell script using an extra $ before it. For example, aws_eks_cluster=$${aws_eks_cluster}
  2. Add that variable as aws_eks_cluster = "$aws_eks_cluster" under the list of variables for file in terraform script and it will replace ${aws_eks_cluster} with $aws_eks_cluster and the script will still work.
1 Like