在存储库的 cloudformation 模板中使用 AWS::CodeBuild::Project 环境变量

Using AWS::CodeBuild::Project Environment variable in cloudformation template of repository

我想为 Lambda 函数创建持续交付管道。

如图所示 docs, the custom environment variables of AWS::CodeBuild::Project 可以在 buildspec.yaml 中使用,例如:

aws cloudformation package --template-file template.yaml --s3-bucket $MYEVVARKEY --output-template-file outputtemplate.yaml

还想在存储库的 SAM 模板中使用那些 CodeBuild 项目环境变量。如下所示,我尝试使用美元符号,但它没有将其作为键而是作为文本:

# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: $MY_FN_NAME_ENV_VAR
      Role: $MY_ROLE_ARN_ENV_VAR
      Handler: index.handler
      Runtime: nodejs8.10
      CodeUri: ./

那么,是否可以在 SAM 模板中利用 CodeBuild 项目环境变量,如果可以,实现该目标需要什么表示法?

CloudFormation 不能引用环境变量,无论是 SAM 还是 plain。您可以做的是通过 CodeBuild buildspec.yaml 文件 (--parameters ParameterKey=name,ParameterValue=${MY_ENV_VAR}).

中的 shell 将环境变量作为参数传递

记得将相应的参数添加到您的 Parameters 部分。

如果您使用 aws cloudformation deploy,那么您应该使用 --parameter-overrides,这是一种更简单的形式:

--parameter-overrides \
    YourParam=${YOUR_ENV_VAR} \
    Foo=Bar \