Rodrigo Andrade
Rodrigo Andrade

Reputation: 449

Terraform cloudformation_stack unkown variable onto UserData

I have a terraform the create a stack on AWS from an yaml file.

My resource "aws_cloudformation_stack" "gitlab-runner" has the following parameters:

Token               = "GAdt_YVHgcp5QM_Nms65"
IAMRoleName         = "${module.gitlab-iam.iam_role_name}"

My yaml file has the following statements:

  Parameters:
  GitLabRunnerToken:
Description: >-
  Registration token for GitLab Runner. Registration token must contain
  exactly 20 alphanumeric characters
AllowedPattern: '^[-_a-zA-Z0-9]*$'
Type: String
MinLength: '20'
MaxLength: '20'
NoEcho: true

  Resources:
  ...
  LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
  ImageId: !FindInMap [AWSRegionToAMI, !Ref 'AWS::Region', AMIID]
  SecurityGroups:
  - !Ref SecurityGroup
  InstanceType: !Ref InstanceType
  IamInstanceProfile: !Ref GitlabRunnerInstanceProfile
  KeyName: !Ref KeyName
  BlockDeviceMappings:
  - DeviceName: /dev/xvdb
    Ebs:
      VolumeSize: !Ref 'VolumeSize'
      VolumeType: !Ref 'VolumeType'
      DeleteOnTermination: !Ref 'DeleteOnTermination'
  UserData:
    Fn::Base64: !Sub |
      #!/bin/bash -xe

      docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
      --non-interactive \
      --executor "docker" \
      --docker-image alpine:3 \
      --url "https://gitlab.affinitas.de" \
      --registration-token ${GitLabRunnerToken} \
      --description "docker-runner" \
      --tag-list "docker,aws" \
      --run-untagged \
      --locked="false"

I am not able to get the value of the parameter GitLabRunnerToken and inject inside UserData: Base64: !Sub |

I got error below:

<template_file>:160,34-51: Unknown variable; There is no variable named "GitLabRunnerToken".

I tried fetch the value using:

1. ${GitLabRunnerToken}
2. Ref: "GitLabRunnerToken"
3. !Ref: "GitLabRunnerToken"
4. !ImportValue "GitLabRunnerToken"

But I am still not able to get and pass the valeu for my UserData, LaunchConfiguration.

Any clue on it?

Thank you.

Upvotes: 0

Views: 472

Answers (1)

base1024
base1024

Reputation: 11

It's because you use ${variable} format. You pass variables to your cloud-init/userdata scripts this way. Terraform foolishly tires to replace anything "${something}" with values in the vars section of template_file. Lose {} and you'll be fine.

Upvotes: 1

Related Questions