Terraform cloudformation_stack未知变量到UserData上

时间:2019-01-23 10:58:10

标签: amazon-web-services stack amazon-cloudformation terraform

我有一个terraform,可以从Yaml文件在AWS上创建堆栈。

我的资源“ aws_cloudformation_stack”“ gitlab-runner”具有以下参数:

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

我的yaml文件具有以下语句:

  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"

我无法获取参数GitLabRunnerToken的值并将其注入UserData中:Base64:!Sub |

我在下面出现错误:

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

我尝试使用以下方法获取值:

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

但是我仍然无法获取和传递我的UserData LaunchConfiguration的值。

有任何线索吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

这是因为您使用$ {variable}格式。您可以通过这种方式将变量传递给cloud-init / userdata脚本。 Terraform愚蠢地用vars的{​​{1}}部分中的值替换任何“ $ {something}”。输{},就可以了。