问题描述
我正在创建服务/前端。该服务是使用CloudFormation创建的。 ApiKey用于身份验证。 ApiKey的值不可用于CloudFormation,因此在创建CodeBuild环境时无法设置环境变量。我已经导出了ApiKey ID,希望通过aws cli在CodeBuild期间获取ApiKey值。
我正在呼叫get-api-key
,它将按预期返回我的键值。我只是似乎不知道如何将其设置为环境变量。我想念什么?
到目前为止,我已经尝试了一些方法。始终相同的结果,但找不到密钥。我的CloudFormation模板创建了一个名为'REACT_APP_API_KEY'的环境变量,并将其设置为空字符串(之所以这样做,是因为它在尝试导出新变量时会抱怨)。我已经通过printenv
命令确认“ REACT_APP_API_KEY”存在。
我的buildspec.yaml如下
version: 0.2
env:
variables:
S3_BUCKET: "nacho-bucket"
phases:
install:
runtime-versions:
nodejs: 10
commands:
- echo Installing npm dependencies
- npm install
build:
commands:
- echo Retrieving API key value
- aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq '.value'
- REACT_APP_API_KEY = $(aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq '.value')
- echo $REACT_APP_API_KEY
- printenv
- npm run build
post_build:
commands:
- echo deploying UI to environment $S3_BUCKET
- cd build
- 'aws s3 sync . s3://$S3_BUCKET --delete'
控制台输出:
[Container] 2020/10/19 19:19:36 Running command echo Retrieving API key value
Retrieving API key value
[Container] 2020/10/19 19:19:36 Running command aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq '.value'
"correctapikey"
[Container] 2020/10/19 19:19:42 Running command REACT_APP_API_KEY = $(aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq '.value')
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: REACT_APP_API_KEY: not found
[Container] 2020/10/19 19:19:42 Command did not exit successfully REACT_APP_API_KEY = $(aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq '.value') exit status 127
解决方法
Arg。我花了大部分时间试图弄清楚这一点。该修补程序在下面(在equals运算符周围不能有空格)。
- REACT_APP_API_KEY=$(aws apigateway get-api-key --api-key $API_KEY_ID --include-value | jq -r '.value')