I am trying to set environment variables with EC2s user data, but nothing i do seems to work
here are the User data scripts i tried
#!/bin/bash echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-23235232.us-east-1.elb.amazonaws.com" >> /env.sh source /env.sh
And another:
#!/bin/bash echo "#!/bin/bash" >> /env.sh echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-67323523.us-east-1.elb.amazonaws.com" >> /env.sh chmod +x /env.sh /env.sh
They both do absolutly nothing, and if i log in and issue the command source /env.sh
or /env.sh
it works. so this must be something forbidden that i am trying to do.
Here is the output from /var/log/cloud-init-output.log
using -e -x
+ echo 'export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709021.us-east-1.elb.amazonaws.com' + source /env.sh ++ export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709022.us-east-1.elb.amazonaws.com ++ HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709022.us-east-1.elb.amazonaws.com
Still, echo $HOST_URL
is empty
As requested, the full UserData script
#!/bin/bash set -e -x echo "export HOST_URL=checkEmai-LoadBala-ICHJ82KG5C7P-2141709021.us-east-1.elb.amazonaws.com" >> /env.sh source /env.sh /startup.sh staging 2649
When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives.
To set environment variablesSign in to the AWS Management Console and open the Amplify console . In the Amplify console, choose App Settings, and then choose Environment variables. In the Environment variables section, choose Manage variables. In the Manage variables section, under Variable, enter your key.
The main difference between Data and Metadata is that data is simply the content that can provide a description, measurement, or even a report on anything relative to an enterprise's data assets. On the other hand, metadata describes the relevant information on said data, giving them more context for data users.
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, for example, host name, events, and security groups. You can also use instance metadata to access user data that you specified when launching your instance.
I created an EC2 instance with Amazon Linux AMI 2018.03.0 and added this user data to it and it works fine. Refer to this answer for more details. After doing the stuffs in the user data script, the process exits. So, whatever environment variable you export will not be there in the next process.
One of the more configurable approach to define environment variables for EC2 instances, is to use Systems Manager Parameter Store. This approach will make it easier to manage different parameters for large number of EC2 instances, both encrypted using AWS KMSas well as in plain text.
1 Connect to your Windows instance. 2 Open C:\Program Files\Amazon\Ec2ConfigService\Ec2ConfigServiceSetting.exe . 3 For User Data, select Enable UserData execution for next service start . 4 Disconnect from your Windows instance. To run updated scripts the next time the instance is started, stop the instance and update the user data. ...
To enable user data execution with EC2Launch (Windows Server 2016 or later) Connect to your Windows instance. Disconnect from your Windows instance. To run updated scripts the next time the instance is started, stop the instance and update the user data. For more information, see View and update the instance user data .
One of the more configurable approach to define environment variables for EC2 instances, is to use Systems Manager Parameter Store. This approach will make it easier to manage different parameters for large number of EC2 instances, both encrypted using AWS KMS as well as in plain text. It will also allows to change the parameter values with minimal changes in EC2 instance level. The steps are as follows.
e.g Using Get-Parameter command to retrieve db_connection_string parameter(Unencrypted).
export DB_CONNECTION=$(aws --region=us-east-2 ssm get-parameter --name 'db_connection' --query 'Value')
Note: For more details in setting up AWS KMS Keys, defining encrypted strings, managing IAM policies & etc., refer the following articles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With