使用CloudFormation有条件地定义Elastic Beanstalk环境变量

时间:2019-03-01 21:56:58

标签: amazon-web-services environment-variables amazon-cloudformation amazon-elastic-beanstalk

我想使用CloudFormation模板创建一个Elastic Beanstalk。我想定义一个环境变量ENV_VAR_1并将其值设置为模板参数var1的值。但是,如果ENV_VAR_1是一个空字符串,则根本不希望var1存在。即我不希望ENV_VAR_1没有价值。

首先,我尝试了Conditions,但是在创建"Encountered unsupported property Condition"资源的过程中得到了ElasticBeanstalkEnvironment

Parameters:
  var1:
    Type: String

Conditions:
  isVar1Empty: !Equals [ !Ref var1, "" ]

Resources:
  ElasticBeanstalkEnvironment:
    Type: 'AWS::ElasticBeanstalk::Environment'
    Properties:
      OptionSettings:
        - Namespace: 'aws:elasticbeanstalk:application:environment'
          Condition: isVar1Empty
          OptionName: ENV_VAR_1
          Value: !Ref var1

然后我尝试了AWS::NoValue

Parameters:
  var1:
    Type: String

Resources:
  ElasticBeanstalkEnvironment:
    Type: 'AWS::ElasticBeanstalk::Environment'
    Properties:
      OptionSettings:
        - Namespace: 'aws:elasticbeanstalk:application:environment'
          OptionName: ENV_VAR_1
          Value: !If [[!Equals [ !Ref var1, "" ]], !Ref 'AWS::NoValue', !Ref var1]

及其许多排列组合。结果相同:var1为空时,将ENV_VAR_1设置为""

来创建Elastic Beanstalk

2 个答案:

答案 0 :(得分:1)

条件将在资源级别应用...当前,您不能将条件应用于特定属性。

要满足这些确切要求(这有点难看),您可以做的是创建两个条件,一个条件否定另一个条件。然后在这两个条件下,让它们有条件地创建特定资源。

Parameters:
  var1:
    Type: String

Conditions:
  isVar1Empty: !Equals [ !Ref var1, "" ]
  isVar1NonEmpty: !Not [ !Equals [ !Ref var1, "" ] ]

Resources:
  ElasticBeanstalkEnvironmentWithVar1:
    Type: 'AWS::ElasticBeanstalk::Environment'
    Condition: isVar1NonEmpty
    Properties:
      OptionSettings:
        - Namespace: 'aws:elasticbeanstalk:application:environment'
          OptionName: ENV_VAR_1
          Value: !Ref var1
  ElasticBeanstalkEnvironmentWithoutVar1:
    Type: 'AWS::ElasticBeanstalk::Environment'
    Condition: isVar1Empty
    Properties:
      OptionSettings:
        - Namespace: 'aws:elasticbeanstalk:application:environment'

就像我说的...有点丑。请注意,只有当您具有一个或两个这样的变量时,这才真正有效。一旦添加了第二个或第三个“可选”参数,该参数很快就会开始失去控制。

更好的选择可能是使用诸如mustache之类的模板库来生成CloudFormation模板。

答案 1 :(得分:1)

在选项级别处理条件的另一种解决方法:

Conditions:    
    CreateProdResources: !Equals [!Ref Env, "prod"]

EBEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    Properties: 
      OptionSettings: 
       - Namespace : "aws:elasticbeanstalk:command"
         OptionName: Timeout
         Value : 1200
       - Namespace : !If [CreateProdResources, "aws:elbv2:listener:443", "aws:elasticbeanstalk:command"]
         OptionName: !If [CreateProdResources, Protocol, Timeout]
         Value : !If [CreateProdResources, HTTPS, 1200]
       - Namespace : !If [CreateProdResources, "aws:elbv2:listener:443", "aws:elasticbeanstalk:command"]
         OptionName: !If [CreateProdResources, SSLPolicy, Timeout]
         Value : !If [CreateProdResources, "ELBSecurityPolicy-2016-08", 1200]
       - Namespace : !If [CreateProdResources, "aws:elbv2:listener:443", "aws:elasticbeanstalk:command"]
         OptionName: !If [CreateProdResources, SSLCertificateArns, Timeout]
         Value : !If [CreateProdResources, !Ref ACMCertificate, 1200]

重复选项在 Elastic Beanstalk 中只考虑一次。