如何从 CloudFormation 模板中引用 json 配置值?

How to refer a json configuration value from within a CloudFormation template?

我有以下 cloudformation 模板。 MyCodePipeline 有一个名为 DeployAction 的阶段操作。它的配置值之一是 StackName: TestStackName。我应该使用哪个函数从该模板中获取该键 (StackName) 的值 (TestStackName)?我无法使用 !GetAtt,因为 CodePipeline 只有 Version 属性可用。

Resources:

  MyCodePipeline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      RoleArn: !GetAtt MyCodePipelineRole.Arn
      ArtifactStore:
        Location: !Ref ArtifactsStore
        Type: S3
      Stages:
        - Name: Source
          Actions:
          - Name: SourceAction
            ActionTypeId:
              Category: Source
              Owner: ThirdParty
              Version: '1'
              Provider: GitHub
            OutputArtifacts:
              - Name: SourceArtifact
            Configuration:
              Owner: GitHubOwner
              Repo: GitHubRepo
              PollForSourceChanges: 'false'
              Branch: GitHubBranch
              OAuthToken: GitHubToken
            RunOrder: 1
        - Name: Deploy
          Actions:
            - Name: DeployAction
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Version: '1'
                Provider: CloudFormation
              Configuration:
                ActionMode: CREATE_UPDATE
                StackName: TestStackName --> I want to export this name

  MyCodePipelineRole:
    Type: AWS::IAM::Role
    Properties: 
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - 'codepipeline.amazonaws.com'
            - 'cloudformation.amazonaws.com'
            - 'apigateway.amazonaws.com'
          Action:
          - 'sts:AssumeRole'

  ArtifactsStore:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      VersioningConfiguration:
        Status: Enabled
      BucketName: my-artifacts-store

Outputs:
  TestStackName:
    Description: MyCodePipeline.DeployActionStage.Configuration.TestStackName
    Value: ????? --> What should I use here to get the TestStackName?
    Export:
      Name: MyCodePipeline-DeployActionStage-Configuration-TestStackName

我们可以将该名称提供给 CodePipeline 和输出,方法是将该名称提取到 Mappings section

在下面的示例中,我创建了一个包含 StackName 的映射结构。要使用函数!FindInMap we need a two-level map,所以看起来有点复杂。欢迎提出改进建议:)

Mappings:
  MetaInfo:
    Names:
      StackName: TestStackName

Resources:

  MyCodePipeline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      ...
      Stages:
        ...
        - Name: Deploy
          Actions:
            - Name: DeployAction
              ...
              Configuration:
                ActionMode: CREATE_UPDATE
                StackName: !FindInMap [MetaInfo, Names, StackName]
...

Outputs:
  TestStackName:
    Description: MyCodePipeline.DeployActionStage.Configuration.TestStackName
    Value: !FindInMap [MetaInfo, Names, StackName]
    Export:
      Name: MyCodePipeline-DeployActionStage-Configuration-TestStackName

这在 CloudFormation 控制台中提供了以下输出:

Key: TestStackName
Value: TestStackName
Description: MyCodePipeline.DeployActionStage.Configuration.TestStackName
Export name: MyCodePipeline-DeployActionStage-Configuration-TestStackName