Hi. I’m trying to pass variables in the EC2 userda...
# getting-started
a
Hi. I’m trying to pass variables in the EC2 userdata but it’s not pulling the values. What could be the issue?
Copy code
region = aws.get_region()

user_data = f"""
#!/bin/bash
sudo systemctl start docker
$(aws ecr get-login --no-include-email --region {region})
docker pull {ecr.repository_url}:latest
Copy code
~ userData: "88b7a8609501c5a4f07efd83f5a4c10be14d0405" => "\n#!/bin/bash\nsudo systemctl start docker\n$(aws ecr get-login --no-include-email --region <pulumi_aws.get_region.AwaitableGetRegionResult object at 0x10a8adb20>)\ndocker pull <pulumi.output.Output object at 0x10af258b0>:latest
p
You probably need to use
apply
to wait for the region response. And I think
region
will be an object (it is in Typescript), so you’ll probably want something like:
Copy code
region = pulumi.output(aws.get_region()).name
If that doesn’t work, try:
Copy code
region = aws.get_region()

user_data = region.apply(lambda r: f"""
#!/bin/bash
sudo systemctl start docker
$(aws ecr get-login --no-include-email --region {r.name})
docker pull {ecr.repository_url}:latest""")
You may need the same thing for the repository URL, in which case you may want to use `all`: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#all
b
thanks Matthew, you beat to me it
a
Thanks!
I got this sorta working. I need to convert to base64 to use as userdata for a launch template. I’m getting the following error:
Copy code
region = aws.get_region()
region_id = region.id

# Convert the userdata into Base64
message = Output.all(r=region_id, e=ecr_endpoint) \
    .apply(lambda args: f"""
        #!/bin/bash
        sudo systemctl start docker
        $(aws ecr get-login --no-include-email --region {args['r']})
        docker pull {args['e']}:latest
        docker run --rm --name "xxxxx-ingest-large" -e PYTHONPATH=/app/myapp {args['e']}:latest 
        distributed-ingest -ephemeral -type medium && sudo shutdown now
        """)

message_bytes = message.encode('utf-8')
base64_bytes = base64.b64encode(message_bytes)
base64_userdata = base64_bytes.decode('utf-8')
Copy code
File "./__main__.py", line 342, in <module>
        message_bytes = message.encode('utf-8')
    TypeError: 'Output' object is not callable
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
b
@astonishing-dinner-89046 you need to do the base64 encode inside the apply
a
@billowy-army-68599 still no go. Is it in the right place? I’m new to python
Copy code
message = Output.all(r=region_id, e=ecr_endpoint) \
    .apply(base64.b64encode(lambda args: f"""
        #!/bin/bash
        sudo systemctl start docker
        $(aws ecr get-login --no-include-email --region {args['r']})
        docker pull {args['e']}:latest
        docker run --rm --name "xxxxx-ingest-large" -e PYTHONPATH=/app/myapp {args['e']}:latest 
        distributed-ingest -ephemeral -type medium && sudo shutdown now
        """))
Copy code
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/base64.py", line 58, in b64encode
        encoded = binascii.b2a_base64(s, newline=False)
    TypeError: a bytes-like object is required, not 'function'
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
b
it needs to be inside the lambda, the anonymous function
so
base64.b64encode(f"""string""")
a
i actually tried that earlier but still didn’t work
Copy code
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/base64.py", line 58, in b64encode
        encoded = binascii.b2a_base64(s, newline=False)
    TypeError: a bytes-like object is required, not 'str'
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
b
encode it into a an array of bytes:
Copy code
base64.b64encode(f"""string""".encode('utf-8'))
a
@billowy-army-68599 Works! I also had to add .decode at the end. Thanks again for the help!
Copy code
base64.b64encode(f"""string""".encode('utf-8')).decode('utf-8'))
❤️ 1