amazon-web-services - 云信息和参数存储 : How to select parameter for the environment

标签 amazon-web-services aws-cloudformation amazon-systems-manager

我想从 CloudFormation 模板中的参数存储中读取数据库的 URL。对于单个 URL 来说这很简单,但我不知道如何根据不同的环境更改 URL。

我有四个环境(开发、集成、预生产和生产),它们的详细信息存储在 Parameter Store 中的四个不同路径上:

/database/dev/url
/database/int/url
/database/ppe/url
/database/prod/url

我现在想在通过 CloudFormation 部署时选择正确的数据库 URL。我怎样才能做到这一点?

Parameters:
  Environment:
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - int
      - ppe
      - prod
  DatabaseUrl:
    Type: 'AWS::SSM::Parameter::Value<String>'
    # Obviously the '+' operator here won't work - so what do I do?
    Default: '/database/' + Environment + '/url'

最佳答案

这个功能并不像人们希望的那么简洁。您必须实际传递要从参数存储中查找的每个参数的名称/路径。

模板:

AWSTemplateFormatVersion: 2010-09-09

Description: Example

Parameters:

  BucketNameSuffix:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /example/dev/BucketNameSuffix

Resources:

  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub parameter-store-example-${BucketNameSuffix}

如果您不向模板传递任何参数,BucketNameSuffix 将使用存储在 /example/dev/BucketNameSuffix 中的值填充。例如,如果您想使用 prod 值(由 /example/prod/BucketNameSuffix 指向),那么您应该为参数 BucketNameSuffix 指定值code>,但您应该传递要使用的参数的替代名称,而不是传递实际值,因此您需要传递 /example/prod/BucketNameSuffix

aws cloudformation update-stack --stack-name example-dev \
--template-body file://./example-stack.yml

aws cloudformation update-stack --stack-name example-prod \
--template-body file://./example-stack.yml \
--parameters ParameterKey=BucketNameSuffix,ParameterValue=/example/prod/BucketNameSuffix

关于此问题的一篇不太好的 AWS 博客文章:https://aws.amazon.com/blogs/mt/integrating-aws-cloudformation-with-aws-systems-manager-parameter-store/

因为传递数百万无意义的参数看起来很愚蠢,所以我实际上可能会生成一个特定于环境的模板并在生成的模板中设置正确的Default:,以便对于prod环境默认将是/example/prod/BucketNameSuffix,然后我可以更新prod堆栈而不传递任何参数。

关于amazon-web-services - 云信息和参数存储 : How to select parameter for the environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705041/

相关文章:

amazon-web-services - CloudFormation Fn::Transform 操作:较低 -> 语法错误

amazon-web-services - 为什么 AWS CloudWatch 加密日志组会破坏 session 管理器?

amazon-web-services - AWS S3 CLI - 无法连接到终端节点 URL

amazon-web-services - AWS lambda : "Unable to import module ' app': No module named 'models' "

django - 无法在 AWS Elastic Beanstalk 上创建新的 RDS 数据库

amazon-web-services - 如何在更新可用时自动更新 AWS Windows EC2 实例

amazon-web-services - 使用 AWS SSM 服务在启动时更新 Raspberry Pi

amazon-web-services - AWS Codepipeline 会将符号链接(symbolic link)传递给工件中的 Codebuild

aws-cloudformation - 在cloudformation模板中使用变量

amazon-web-services - CloudFormation 中 Eventbridge Scheduler 的重试策略