'Why variables that I export in bash script does not work on EC2?

When I create EC2 inst. I use bash script into user data where I export variable AWS credentials and then run the command to copy files from S3 bucket. But this command is not executed.

#! /bin/bash 
export AWS_ACCESS_KEY_ID=MYACCESSKEY
export AWS_SECRET_ACCESS_KEY=MYSECRETKEY
aws s3 cp s3://mys3bucket/ ./

How to fix it?



Solution 1:[1]

It is run on its own bash process which dies at the end of your script.

Exported variables are preserved only for the lifetime of your script and they are also visible from child processes of your script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Temo Hatna