0

I have an EC2 instance that I am deploying on AWS.

I am using an Amazon Linux 2, and I am passing a user data to it as such:

userdata_file.write(
        f'''
        #!/bin/bash\n
        export PAGERDUTYAPIKEY='mykey'\n
        sudo yum install git -y\n
        chmod +x ./basic_test.sh \n
        echo $PAGERDUTYAPIKEY> /home/ec2-user/pagerdutyapikey1.txt\n
        sudo ./basic_test.sh
        '''.strip()
    )

basic_test.sh

#!/bin/bash

echo "s/enterpagerdutyapikey/${PAGERDUTYAPIKEY}/g" > path.txt

However, when i run it, in the path.txt it is echoing as such:

s/enterpagerdutyapikey//g

But when i ssh in the server and run the same bash script, it echos as such:

s/enterpagerdutyapikey/mykey/g

Any idea why the environment variable $PAGERDUTYAPIKEY is rendering empty when i run through the userdata ?

6
  • You aren't using SENDGRIDAPIKEY in basic_test.sh; you're using PAGERDUTYAPIKEY. Commented Dec 20, 2021 at 15:02
  • Sorry for the confusion, i did a typo on stackoverflow. I updated my code. Apologies Commented Dec 20, 2021 at 15:04
  • 3
    Oh, sudo does not preserve arbitrary environment variables by default for security reasons. Commented Dec 20, 2021 at 15:08
  • 1
    Can you modify ./basic_test.sh? Just have it read directly from /home/ec2-user/pagedutyapikey1.txt` instead of using the environment. Commented Dec 20, 2021 at 15:09
  • Crossposted to ServerFault. Commented Dec 20, 2021 at 15:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.