简体   繁体   English

Cloudformation 无法将输出参数与嵌套堆栈一起使用

[英]Cloudformation Unable to Use Outputted Parameters with Nested Stacks

I'm trying my hand at Cloudformation nested stacks.我正在尝试 Cloudformation 嵌套堆栈。 The idea is that I create a VPC, S3 bucket, Codebuild project, and Codepipeline pipeline using Cloudformation.这个想法是我使用 Cloudformation 创建了一个 VPC、S3 存储桶、Codebuild 项目和 Codepipeline 管道。

My Problem: Cloudformation is saying that the following parameters (outputted by child stacks) require values:我的问题: Cloudformation 说以下参数(由子堆栈输出)需要值:

  • Vpc虚拟主机
  • PrivateSubnet1私有子网 1
  • PrivateSubnet2私有子网2
  • PrivateSubnet3私有子网3
  • BucketName桶名

These params should have values as the value exists when I look at a completed child stack in the console.当我在控制台中查看完整的子堆栈时,这些参数应该具有值,因为该值存在。

I'll just show the templates for the parent, s3, and codepipeline.我将只展示父级、s3 和代码管道的模板。 With regards to these three templates the problem is that I am unable to use an output BucketName from S3Stack in my CodePipelineStack至于这三个模板的问题是,我无法使用从输出BucketName S3Stack在我CodePipelineStack

My Code:我的代码:

cfn-main.yaml cfn-main.yaml

AWSTemplateFormatVersion: 2010-09-09

Description: root template for codepipeline poc

Parameters:

  BucketName:
    Type: String

  VpcName:
    Description: name of the vpc
    Type: String
    Default: sandbox

  DockerUsername:
    Type: String
    Description: username for hub.docker
    Default: seanturner026

  DockerPassword:
    Type: String
    Description: password for hub.docker
    Default: /codebuild/docker/password

  Environment:
    Type: String
    Description: environment
    AllowedValues:
      - dev
      - prod
    Default: dev

  Vpc:
    Type: AWS::EC2::VPC::Id

  PrivateSubnet1:
    Type: AWS::EC2::Subnet::Id

  PrivateSubnet2:
    Type: AWS::EC2::Subnet::Id

  PrivateSubnet3:
    Type: AWS::EC2::Subnet::Id

  GithubRepository:
    Type: String
    Description: github repository
    Default: aws-codepipeline-poc

  GithubBranch:
    Type: String
    Description: github branch
    Default: master

  GithubOwner:
    Type: String
    Description: github owner
    Default: SeanTurner026

  GithubToken:
    Type: String
    Description: github token for codepipeline
    NoEcho: true

Resources:
  VpcStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      Parameters:
        VpcName: !Ref VpcName
      TemplateURL: resources/vpc.yaml

  S3Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: resources/s3.yaml

  CodeBuildStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      Parameters:
        Environment: !Ref Environment
        DockerUsername: !Ref DockerUsername
        DockerPassword: !Ref DockerPassword
        Vpc: !GetAtt VpcStack.Outputs.VpcId
        PrivateSubnet1: !GetAtt VpcStack.Outputs.PrivateSubnetId1
        PrivateSubnet2: !GetAtt VpcStack.Outputs.PrivateSubnetId2
        PrivateSubnet3: !GetAtt VpcStack.Outputs.PrivateSubnetId3
      TemplateURL: resources/codebuild.yaml

  CodePipelineStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      Parameters:
        Environment: !Ref Environment
        GithubRepository: !Ref GithubRepository
        GithubBranch: !Ref GithubBranch
        GithubOwner: !Ref GithubOwner
        GithubToken: !Ref GithubToken
        S3: !GetAtt S3Stack.Outputs.BucketName
      TemplateURL: resources/codepipeline.yaml

s3.yaml s3.yaml

AWSTemplateFormatVersion: 2010-09-09

Description: s3 bucket for aws codepipeline poc

Resources:
  S3:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: "aws-sean-codepipeline-poc"

Outputs:
  BucketName:
    Description: S3 bucket name
    Value: !Ref S3

codepipeline.yaml -- Please see ArtifactStore . codepipeline.yaml - 请参阅ArtifactStore This is where cloudformation is seeing my parameter BucketName as value-less.这就是 cloudformation 将我的参数BucketName视为BucketName

AWSTemplateFormatVersion: 2010-09-09

Description: codepipeline for aws codepipeline poc

Parameters:

  BucketName:
    Type: String

  Environment:
    Type: String
    Description: environment
    AllowedValues:
      - dev
      - prod
    Default: dev

  GithubRepository:
    Type: String
    Description: github repository
    Default: aws-codepipeline-poc

  GithubBranch:
    Type: String
    Description: github branch
    Default: master

  GithubOwner:
    Type: String
    Description: github owner
    Default: SeanTurner026

  GithubToken:
    Type: String
    Description: github token for codepipeline
    NoEcho: true

Resources:
  CodePipelineRole:
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: !Join
        - ""
        - - !Ref AWS::StackName
          - "-code-pipeline-role-"
          - !Ref Environment
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          Effect: "Allow"
          Principal:
            Service: "codepipeline.amazonaws.com"
          Action: "sts:AssumeRole"

  CodePipelinePolicy:
    Type: "AWS::IAM::Policy"
    Properties:
      PolicyName: !Join
        - ""
        - - !Ref AWS::StackName
          - "-code-pipeline-policy-"
          - !Ref Environment
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          Effect: Allow
          Action:
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:PutLogEvents
            - s3:putObject
            - s3:getObject
            - codebuild:*
          Resource:
            - "*"
      Roles:
        - !Ref CodePipelineRole

  Pipeline:
    Type: "AWS::CodePipeline::Pipeline"
    Properties:
      Name: !Join
        - ""
        - - "code-pipeline-poc-"
          - !Ref AWS::StackName
      ArtifactStore:
        Location: !Ref BucketName
        Type: S3
      RestartExecutionOnUpdate: true
      RoleArn: !Join
        - ""
        - - "arn:aws:iam::"
          - !Ref AWS::AccountId
          - ":role/"
          - !Ref CodePipelineRole
      Stages:
        - Name: checkout-source-code
          Actions:
            - Name: SourceAction
              RunOrder: 1
              ActionTypeId:
                Category: Source
                Owner: ThirdParty
                Provider: GitHub
                Version: 1
              Configuration:
                Owner: !Ref GithubOwner
                Repo: !Ref GithubRepository
                Branch: !Ref GithubBranch
                PollForSourceChanges: true
                OAuthToken: !Ref GithubToken
              OutputArtifacts:
                - Name: source-code

        - Name: docker-build-push
          Actions:
            - Name: build-push-job
              RunOrder: 1
              InputArtifacts:
                - Name: source-code
              ActionTypeId:
                Category: Build
                Owner: AWS
                Provider: CodeBuild
                Version: 1
              Configuration:
                ProjectName: !Ref BuildPushJob
              OutputArtifacts:
                - Name: build-push-job

Sorry if this is too verbose.对不起,如果这太冗长了。 If missed above, the problem is that ArtifactStore in the codepipeline.yaml is seeing my parameter BucketName as value-less, despite the value being outputted by S3Stack.如果上面遗漏了,问题是codepipeline.yaml中的ArtifactStore将我的参数BucketName视为无值,尽管值是由 S3Stack 输出的。

您将参数作为S3传递,但模板期望它作为BucketName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM