【问题标题】:serverless framework AWS pseudo parameters stack name无服务器框架 AWS 伪参数堆栈名称
【发布时间】:2020-08-14 06:39:14
【问题描述】:

问题

  • 在不硬编码堆栈名称的情况下,在 serverless.yml 文件中获取 cloudformation 堆栈输出的正确方法是什么?

步骤

我有一个 serverless.yml 文件,我在其中导入了一个 cloudformation 模板来创建一个 ElastiCache 集群。

当我尝试这样做时,我得到了这个错误:

  Serverless Error ---------------------------------------

  Invalid variable reference syntax for variable AWS::StackName. You can only reference env vars, options, & files. You can check our docs for more info.


在我的文件中,我想将来自 cloudformation 堆栈的 ElastiCacheAddress 输出作为环境变量公开。我正在使用serverless pseudo-parameters plugin 来获取输出:

# Here is where I try to reference the CF output value
service: hello-world

provider:
  name: aws
  # ...
  environment:
    cacheUrl: ${cf:#{AWS::StackName}.ElastiCacheAddress}

# Reference to the CF template
resources: 
  - '${file(./cf/cf-elasticache.yml)}'

CF 模板来自AWS Samples GitHub repository

带有输出的sn-p在这里:

  ElastiCacheAddress:
    Description: ElastiCache endpoint address
    Value: !If [ IsRedis, !GetAtt ElastiCacheCluster.RedisEndpoint.Address, !GetAtt ElastiCacheCluster.ConfigurationEndpoint.Address]
    Export:
      Name: !Sub ${AWS::StackName}-ElastiCacheAddress

【问题讨论】:

标签: amazon-cloudformation serverless-framework


【解决方案1】:

您可以使用解决方法来解决这些语法警告。 在这种情况下,我建议您创建一个custom 节点来设置您想要重用的变量。然后,您可以仅使用无服务器框架语法引用这些变量,以避免该错误,如下所示:

# Here is where I try to reference the CF output value
service: hello-world

custom:
  stackName:'#{AWS::StackName}'

provider:
  name: aws
  # ...
  environment:
    cacheUrl: ${cf:${self:custom.stackName}.ElastiCacheAddress}

# Reference to the CF template
resources: 
  - '${file(./cf/cf-elasticache.yml)}'

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 2017-04-20
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2019-07-23
    • 2019-08-04
    相关资源
    最近更新 更多