parametersaws-cloudformationaws-codepipelineaws-codebuilddynamicparameters

Using deploy time parameters in CDK 'codepipeline_actions.CloudFormationCreateUpdateStackAction'


I am clearly not understanding how dynamic parameters can be used when deploying a template in CodePipeline. Here is the basic scenario:

CodeBuild BUILD Action - Here I have retrieved, for instance, a value for COMMIT_NUMBER. This can be set as an environment variable, or saved to a file... whatever needs to be done. In a post_build step I package up the template using "sam package ...."

CloudFormation DEPLOY stage - Here I use

new codepipeline_actions.CloudFormationCreateUpdateStackAction({
      actionName: `The_Deploy`,
      templatePath: buildOutput.atPath(TEMPLATE_FILE_NAME),
      parameterOverrides, --These are known when I synth the pipeline
      stackName: envStackName,
      cfnCapabilities: [CfnCapabilities.AUTO_EXPAND, CfnCapabilities.ANONYMOUS_IAM],
      adminPermissions: true,
      role: buildRole,
      runOrder: runOrder || 1
    });

to deploy the template that was packaged up. In the props for this method, there is a parameterOverrides property, but anything in there would have to be known at build time.

My question is HOW do i set dynamic parameter values that are known in the build step, to the parameters consumed by the deploy step.

Thanks for any clarification!


Solution

  • Thank you @Ronan Cunningham, for the link : here. The KEY point that clarified how to do this was this sentence below, that I have not found explained well otherwise:

    "Since namespaces are native CodePipeline feature, they are not restricted only to CloudFormation actions. Now you can for example define CodeBuild environment variables in a dynamic way as well (it is also possible to define export variables in CodeBuild - these values will will become step output values):" ....

    To bring it all together here are the key pieces that allowed me to set an environment variable in the codebuild action and pass it to a cloudformation parameter IN CDK:

    1.) add env variable and export variable in the build project

    const buildProject = new codebuild.PipelineProject(construct, `buildProject`, {
        buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        env: {
          "variables": {
            "MY_IMPORTANT_INFO": "some-default-value-here"
          },
          "exported-variables": [ "MY_IMPORTANT_INFO" ]
        },
        phases: {
          install: {...},
          build: {
            commands: [ NOTE... variable above can be changed here...]
          }
        },
        artifacts: {...}
        role: buildRole
      })
    

    2.) Add namespace to the build action:

    new codepipeline_actions.CodeBuildAction({
          actionName: `The_Build`,
          project: buildProject,
          input: sourceOutput,
          outputs: [buildOutput],
          role: buildRole,
          variablesNamespace: 'build'
        })
    

    3.) In the deployment stage, I can now set the ParameterOverride by setting the parameter name to the environment variable that was exported in the build step:

    new codepipeline_actions.CloudFormationCreateUpdateStackAction({
          actionName: `The_Deploy`,
          templatePath: buildOutput.atPath(TEMPLATE_FILE_NAME),
          parameterOverrides: [
            {"COMMIT_NUMBER": "#{build.MY_IMPORTANT_INFO}"}
          ],
          stackName: envStackName,
          cfnCapabilities: [CfnCapabilities.AUTO_EXPAND, CfnCapabilities.ANONYMOUS_IAM],
          adminPermissions: true,
          role: buildRole,
          runOrder: runOrder || 1
        });
    

    Hallelujah!

    And, thanks again to Ronan for the informative link!