How to set Environment Variables on EC2 instance via User Data

前端 未结 9 742
北荒
北荒 2020-12-14 07:34

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

#!/b         


        
9条回答
  •  盖世英雄少女心
    2020-12-14 08:26

    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

提交回复
热议问题