Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodePipeline construct doesn't allow naming Stages/Action from CFN parameters #6275

Closed
t04glovern opened this issue Feb 14, 2020 · 2 comments
Assignees
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline closing-soon This issue will automatically close in 4 days unless further comments are made. guidance Question that needs advice or information.

Comments

@t04glovern
Copy link

t04glovern commented Feb 14, 2020

We are trying to introduce a parameterised name for a pipeline stage action. However when trying to synth out the replace, we are receiving the following error Cannot use tokens in construct ID: ${Token[TOKEN.11]}.

We've tried to reference the parameter in other places in the template to confirm it's a problem exclusive to the Pipeline construct.

Reproduction Steps

import * as cdk from '@aws-cdk/core';
import codepipeline = require('@aws-cdk/aws-codepipeline');
import codepipeline_actions = require('@aws-cdk/aws-codepipeline-actions');
import lambda = require('@aws-cdk/aws-lambda');
import s3 = require('@aws-cdk/aws-s3');

import path = require('path');

export class P3Stack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);


    const actionName = new cdk.CfnParameter(this, 'action-name', {
        type: 'String',
        description: `Action name`,
    }).valueAsString;

    const temp_lambda = new lambda.Function(this, 'temp-lambda', {
      runtime: lambda.Runtime.NODEJS_10_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
    });

    const s3_source_bucket = new s3.Bucket(this, 'source-bucket', {
      versioned: true
    });

    const source_output = new codepipeline.Artifact();
    const source_action = new codepipeline_actions.S3SourceAction({
      actionName: 's3_source',
      bucket: s3_source_bucket,
      output: source_output,
      bucketKey: 'app.zip'
    });

    const deployStage = new codepipeline_actions.LambdaInvokeAction({
      lambda: temp_lambda,
      actionName: actionName
    })

    const pipeline = new codepipeline.Pipeline(this, 'pipeline', {
      pipelineName: `pipeline`,
      stages: [
        {
          stageName: 'Source',
          actions: [
            source_action
          ]
        },
        {
          stageName: 'Deploy',
          actions: [
            deployStage
          ]
        }
      ]
    });
  }
}

Error Log

Cannot use tokens in construct ID: ${Token[TOKEN.11]}

/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:252
      for (const child of node.node.children) {
                                    ^
TypeError: Cannot read property 'children' of undefined

Environment

  • CLI Version : 1.23.0 (build 01f326e)
  • OS : MacOS
  • Language : TypeScript

Other

TypeError: Cannot read property 'children' of undefined
    at visit (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:252:37)
    at visit (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:253:9)
    at visit (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:253:9)
    at visit (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:253:9)
    at visit (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:253:9)
    at ConstructNode.findAll (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:244:5)
    at Function.prepare (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:71:29)
    at Function.synth (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/construct.ts:40:10)
    at App.synth (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/app.ts:141:36)
    at process.App.process.once (/Users/nathan/Work/hq/p3/node_modules/@aws-cdk/core/lib/app.ts:120:45)
Subprocess exited with error 1

This is 🐛 Bug Report

@t04glovern t04glovern added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 14, 2020
@skinny85
Copy link
Contributor

Hey @t04glovern ,

thanks for opening the issue. Due to some technical reasons, you cannot use deploy-time values like parameters for the names of stages and actions in the CodePipeline construct. However, you can use escape hatches to work around this limitation. Try this:

        // this part is the same as your code
        const actionName = new cdk.CfnParameter(this, 'action-name', {
            type: 'String',
            description: `Action name`,
        }).valueAsString;

        const temp_lambda = new lambda.Function(this, 'temp-lambda', {
            runtime: lambda.Runtime.NODEJS_10_X,
            handler: 'index.handler',
            code: lambda.Code.fromInline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
        });

        const s3_source_bucket = new s3.Bucket(this, 'source-bucket', {
            versioned: true
        });

        const source_output = new codepipeline.Artifact();
        const source_action = new codepipeline_actions.S3SourceAction({
            actionName: 's3_source',
            bucket: s3_source_bucket,
            output: source_output,
            bucketKey: 'app.zip'
        });

        const deployStage = new codepipeline_actions.LambdaInvokeAction({
            lambda: temp_lambda,
            actionName: 'Deploy', // just use a specific name here
        });

        const pipeline = new codepipeline.Pipeline(this, 'pipeline', {
            pipelineName: `pipeline`,
            stages: [
                {
                    stageName: 'Source',
                    actions: [
                        source_action
                    ]
                },
                {
                    stageName: 'Deploy',
                    actions: [
                        deployStage
                    ]
                }
            ]
        });

        // this is the escape hatch
        const cfnPipeline = pipeline.node.defaultChild as codepipeline.CfnPipeline;
        cfnPipeline.addPropertyOverride('Stages.1.Actions.0.ActionName', actionName);

And the resulting template (fragment):

  pipelineDBECAE49:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      RoleArn:
        Fn::GetAtt:
          - pipelineRole55399C5D
          - Arn
      Stages:
        - Actions:
            - ActionTypeId:
                Category: Source
                Owner: AWS
                Provider: S3
                Version: "1"
              Configuration:
                S3Bucket:
                  Ref: sourcebucketE323AAE3
                S3ObjectKey: app.zip
              Name: s3_source
              OutputArtifacts:
                - Name: Artifact_Source_s3_source
              RoleArn:
                Fn::GetAtt:
                  - pipelineSources3sourceCodePipelineActionRoleBB7011C1
                  - Arn
              RunOrder: 1
          Name: Source
        - Actions:
            - ActionTypeId:
                Category: Invoke
                Owner: AWS
                Provider: Lambda
                Version: "1"
              Configuration:
                FunctionName:
                  Ref: templambdaD9BF7305
              Name: Deploy
              RoleArn:
                Fn::GetAtt:
                  - pipelineDeployCodePipelineActionRole251092A7
                  - Arn
              RunOrder: 1
              ActionName:
                Ref: actionname
          Name: Deploy

Note that until #1237 lands, you won't be able to deploy this template using cdk deploy though.

Thanks,
Adam

@skinny85 skinny85 added guidance Question that needs advice or information. @aws-cdk/aws-codepipeline Related to AWS CodePipeline and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 14, 2020
@skinny85 skinny85 self-assigned this Feb 14, 2020
@skinny85 skinny85 added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 19, 2020
@skinny85 skinny85 changed the title @aws-cdk/aws-codepipeline Doesn't allow references to CfnParameters CodePipeline construct doesn't allow naming Stages/Action from CFN parameters Mar 20, 2020
@SomayaB SomayaB added closing-soon This issue will automatically close in 4 days unless further comments are made. and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 30, 2020
@SomayaB
Copy link
Contributor

SomayaB commented Apr 3, 2020

Closing for now since there hasn't been a response in a while. Feel free to reopen.

@SomayaB SomayaB closed this as completed Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline closing-soon This issue will automatically close in 4 days unless further comments are made. guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants