How to import Resource ID into UserData in Cloudformation

Viewed 169

I have a cloudformation template with an efs filesystem and an instance launch configuration. I need to reference the efs filesystem id in the UserData script, as I want to mount the efs on launch. I've tried it with echo ${!Ref EFSFileSystem} like this:

  EFSFileSystem:
    Type: AWS::EFS::FileSystem
    Properties:
        ...


  InstanceLc:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      ...
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
          aws s3 cp s3://aws-cloudinit/install.sh - | bash
          echo ${!Ref EFSFileSystem}

but in the logs I can see that this gives me an error:

/var/lib/cloud/instance/scripts/part-001: line 4: ${Ref EFSFileSystem}: bad substitution

I've found some examples (f.ex. https://github.com/awslabs/aws-cloudformation-templates/blob/master/community/services/EFS/efs_with_automount_to_ec2.json), but I wasn't able to apply them to my UserData construct.

What is the proper Syntax to import the efs filesystem id in UserData?

1 Answers

You have a !Sub on the string already, so instead of ${!Ref EFSFileSystem} you just need to do ${EFSFileSystem}. That will be replaced with the default output of the resource, in this case, the Id.

Related