Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Environment Variables on EC2 instance via User Data

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 
like image 516
Gleeb Avatar asked Dec 10 '15 15:12

Gleeb


People also ask

What is user data in EC2 instance?

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.

How do I add an environment variable in AWS?

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.

What is the difference between metadata and user data?

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.

What is instance user data?

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.

Is it possible to export environment variables from EC2 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.

How to define environment variables for AWS EC2 instances?

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.

How to update user data in EC2 config service?

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. ...

How to enable user data execution with ec2launch (Windows Server 2016)?

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 .


1 Answers

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.

  • Define string parameters (Encrypted with KMS or Unencrypted) in EC2 Systems Manager Parameter Store.
  • In the IAM role EC2 assumes, give required permission to access the parameter store.
  • Using the AWS CLI commands for EC2 System Manager, read the parameters and export to environment variables in User Data section using Get-Parameter or Get-Parameters AWS CLI commands and controlling command output as required.

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.

  • Securing Application Secrets with EC2 Parameter Store
  • Simple Secrets Management via AWS’ EC2 Parameter Store
like image 62
Ashan Avatar answered Sep 17 '22 15:09

Ashan