Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import bash script as UserData in AWS CFN template using Troposphere #1711

Open
Yogesharuna opened this issue May 28, 2020 · 1 comment
Open

Comments

@Yogesharuna
Copy link

0

I am trying to create an AWS template using Troposphere and I am stuck at a point where I have to use my Bash Script as UserData. My main problem being Ref used in Bash Script. I am importing Bash Script as a file read and encode them to Base64 as:

UserData= Base64(Join("", [open('path to my bashscript', "r").read() .splitlines(True)]))

My bash script has Ref used as:
#!/bin/bash
set -x
export -f check_instance_service_status
timeout Ref('ValueReferenced') bash -c "check_instance_service_status $ELB_NAME"

The output I am getting is:
"#!/bin/bash\n",
"set -x\n",
"\n",
" export -f check_instance_service_status\n",
" timeout Ref('ValueReferenced') bash -c "check_instance_service_status $ELB_NAME"\n",
" RC=$?\n",

However, I want the Ref to be converted to proper JSON compatible as:
" timeout "",
{
"Ref": "ValueReferenced"
},

@urluba
Copy link

urluba commented Jul 13, 2020

Hi,

You have to use the Fn::Sub function, before the call to Base64. By example:

    instance_launchconfig = template.add_resource(
        asg.LaunchConfiguration(
            "InstanceLaunchConfiguration",
            ImageId=Ref(parameters["instance_ami"]),
            InstanceType=Ref(parameters["instance_type"]),
            UserData=Base64(
                Sub(
                    # Your script here
                )
            ),
        )
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants