Benny
Benny

Reputation: 879

Getting error "One or more Fn::Sub intrinsic functions don't specify expected arguments" when trying to use variables in UserData in CloudFormation

I am getting this error when trying to use !Sub with variables in UserData in CloudFormation:

Template error: One or more Fn::Sub intrinsic functions don't specify expected arguments. Specify a string as first argument, and an optional second argument to specify a mapping of values to replace in the string

Why do I get this error?

This is my code:

LinuxEC2Instance:
Type: AWS::EC2::Instance
Properties:
  UserData:
    Fn::Base64: !Sub
      - arn_id: !If [TestEnvironment, 'id1', 'id2']
      - key: !If [TestEnvironment, 'key1', 'key2']
      - |
        ARN_ID=${arn_id}
        KEY=${key}
        
        echo $ARN_ID 
        echo $KEY

Upvotes: 2

Views: 4479

Answers (1)

Marcin
Marcin

Reputation: 238497

The first argument to Sub must be string. Thus you should change order in your UserData. For example:

    Fn::Base64: 
      !Sub
        - |
          #!/bin/bash -xe
       
          ARN_ID=${arn_id}
          KEY=${key}
        
          echo $ARN_ID 
          echo $KEY
        - arn_id: !If [TestEnvironment, 'id1', 'id2']
          key: !If [TestEnvironment, 'key1', 'key2']

Upvotes: 3

Related Questions