简体   繁体   中英

Cloudformation Unable to Use Outputted Parameters with Nested Stacks

I'm trying my hand at Cloudformation nested stacks. The idea is that I create a VPC, S3 bucket, Codebuild project, and Codepipeline pipeline using Cloudformation.

My Problem: Cloudformation is saying that the following parameters (outputted by child stacks) require values:

  • Vpc
  • PrivateSubnet1
  • PrivateSubnet2
  • PrivateSubnet3
  • 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. With regards to these three templates the problem is that I am unable to use an output BucketName from S3Stack in my CodePipelineStack

My Code:

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

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 . This is where cloudformation is seeing my parameter BucketName as value-less.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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