title: passing environment variables from codebuild to codedeploy ec2 instances
body: <p>I'm new to sre. i building an aws codepipeline using cdk. i need to pass the rds instance information from my rds stack to my codepipeline(ec2) stack. I need a .env file in my ec2 instances. based on my research i saw there is something called environment variables that can do it for me instead of generating a .env file from codebuild. i set up a few environment variables(plain text) in codebuild and try to pass those environment variables into the ec2 instances that was deployed from the codedeploy. i was able to get the correct environment variable values in buildspec.yml. but when i tried to run <code>echo $DB_HOST</code> in ec2 terminal. i got nothing. here is my set up:</p>
  <p>codebuild environment variables:</p>
  <p><a href="https://i.stack.imgur.com/ucUhn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ucUhn.png" alt="enter image description here" /></a></p>
  <p>buildspec.yml</p>
  <pre><code>version: 0.2
  env:
    exported-variables:
      - DB_HOST
      - DB_PORT
      - DB_DATABASE
      - DB_PASSWORD
      - DB_USERNAME
  phases:
    install:
      commands:
        - echo $DB_HOST
        - export DB_HOST=$DB_HOST
    pre_build:
      commands:
        - export DB_HOST=$DB_HOST
  artifacts:
    files:
      - '**/*'
    name: myname-$(date +%Y-%m-%d)
  </code></pre>
  <p>my appspec.yml</p>
  <pre><code>version: 0.0
  os: linux
  files:
    - source: /
      destination: /var/www/html/
  hooks:
    BeforeInstall:
      - location: script/BeforeInstall.sh
        runas: root
    AfterInstall:
      - location: script/AfterInstall.sh
        runas: root
  </code></pre>
  <p>AfterInstall.sh</p>
  <pre><code>#!/bin/bash
  # Set permissions to storage and bootstrap cache
  sudo chmod -R 0777 /var/www/html/storage
  sudo chmod -R 0777 /var/www/html/bootstrap/cache
  #
  cd /var/www/html
  
  #
  # Run composer
  composer install --ignore-platform-reqs
  </code></pre>
  <p>please help me to pass those environment variables from codebuild to codedeploy ec2. or is there any other way to generate .env file for codebuild?</p>