首页 > 解决方案 > 如何从 CloudFormation 模板中引用 json 配置值?

问题描述

我有以下 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

标签: amazon-web-servicesamazon-cloudformationaws-codepipelinegetattribute

解决方案


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

在下面的示例中,我创建了一个包含StackName. 要使用这个函数!FindInMap ,我们需要一个两级的 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

推荐阅读