简体   繁体   English

将环境变量从 codebuild 传递到 codedeploy ec2 实例

[英]passing environment variables from codebuild to codedeploy ec2 instances

I'm new to sre.我是新来的。 i building an aws codepipeline using cdk.我使用 cdk 构建了一个 aws 代码管道。 i need to pass the rds instance information from my rds stack to my codepipeline(ec2) stack.我需要将 rds 实例信息从我的 rds 堆栈传递到我的 codepipeline(ec2) 堆栈。 I need a .env file in my ec2 instances.我的 ec2 实例中需要一个 .env 文件。 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.根据我的研究,我看到有一种叫做环境变量的东西可以为我做,而不是从 codebuild 生成 .env 文件。 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.我在 codebuild 中设置了一些环境变量(纯文本),并尝试将这些环境变量传递到从 codedeploy 部署的 ec2 实例中。 i was able to get the correct environment variable values in buildspec.yml.我能够在 buildspec.yml 中获得正确的环境变量值。 but when i tried to run echo $DB_HOST in ec2 terminal.但是当我尝试在 ec2 终端中运行echo $DB_HOST时。 i got nothing.我什么都没有。 here is my set up:这是我的设置:

codebuild environment variables:代码构建环境变量:

在此处输入图片说明

buildspec.yml构建规范.yml

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)

my appspec.yml我的 appspec.yml

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

AfterInstall.sh安装后.sh

#!/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

please help me to pass those environment variables from codebuild to codedeploy ec2.请帮助我将这些环境变量从 codebuild 传递到 codedeploy ec2。 or is there any other way to generate .env file for codebuild?或者有没有其他方法可以为代码构建生成 .env 文件?

You can't do this the way you expect it.你不能按照你期望的方式做到这一点。 The proper way is to pass them through SSM Secrets Manager or SSM Paramter Store .正确的方法是通过SSM Secrets ManagerSSM Paramter Store传递它们。

So in your setup, CodeBuild will populate the SSM Secrets Manager or SSM Paramter Store (or you populate them before hand youself), and CodeDeploy will read these secret stores for the parameters.因此,在您的设置中,CodeBuild 将填充 SSM Secrets Manager 或 SSM 参数存储(或者您自己事先填充它们),并且 CodeDeploy 将读取这些秘密存储以获取参数。

I found a way to work around with it.我找到了解决它的方法。 here is my solution: since I'm able to get all the environment variables in build stage.这是我的解决方案:因为我能够在构建阶段获得所有环境变量。 i manage to build a .env file in build stage.我设法在构建阶段构建了一个.env文件。 I have a few environment variables coming in to build stage from secret manager or as plain text.我有一些环境变量从秘密管理器或纯文本进入构建阶段。 first, i created a .env.exmaple file in my project root directory:首先,我在我的项目根目录中创建了一个.env.exmaple文件:

...
APP_ENV=local
APP_KEY=

...

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=root
DB_PASSWORD=

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=
...

second, i updated my buildspec.yml file and replace each value with environment variable values using sed commands其次,我更新了buildspec.yml文件并使用sed命令将每个值替换为环境变量值

version: 0.2
env:
  exported-variables:
    - DB_HOST
    - DB_DATABASE
    - DB_PASSWORD
    - DB_USERNAME
  secrets-manager:
    MAIL_PASSWORD: "email-token:MAIL_PASSWORD"
    AWS_ACCESS_KEY_ID: "aws-token:AWS_ACCESS_KEY_ID"
    AWS_SECRET_ACCESS_KEY: "aws-token:AWS_SECRET_ACCESS_KEY"
    AWS_DEFAULT_REGION: "aws-token:AWS_DEFAULT_REGION"
    AWS_BUCKET : "aws-token:AWS_BUCKET"
    AWS_URL : "aws-token:AWS_URL"
phases:
  build:
    commands:
      - cp .env.example .env
      - sed -i "s/DB_HOST=127.0.0.1/DB_HOST=$DB_HOST/g" .env
      - sed -i "s/DB_DATABASE=/DB_DATABASE=$DB_DATABASE/g" .env
      - sed -i "s/DB_USERNAME=root/DB_USERNAME=$DB_USERNAME/g" .env
      - sed -i "s/DB_PASSWORD=/DB_PASSWORD=$DB_PASSWORD/g" .env
      - sed -i "s/APP_ENV=local/APP_ENV=$APP_ENV/g" .env
      - sed -i "s/MAIL_PASSWORD=/MAIL_PASSWORD=$MAIL_PASSWORD/g" .env
...
      - sed -i "s@AWS_URL=@AWS_URL=$AWS_URL@g" .env
artifacts:
  files:
    - '**/*'
  name: myname-$(date +%Y-%m-%d)

in this way, i am able to create a .env file for the deploy stage.通过这种方式,我可以为部署阶段创建一个.env文件。 i think to notice here is that if your value contain / (for example url), you need to use @ to instead of / for sed commands我想在这里要注意的是,如果您的值包含/ (例如 url),则需要使用@ to 而不是/对于sed命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM