How to pass variables/outputs from CloudFormationCreateUpdateStackAction to next stages?

1

This is PreProdApplicationStack, it has a output exportName: "Url". I want to access this output/variable after CloudFormation deployment stage.

...
this.url = new CfnOutput(this, `Url`, {
      description: "api url",
      exportName: `Url`,
      value: api.url,
    });

This is deployPreProd action

const deployPreProd =
      new aws_codepipeline_actions.CloudFormationCreateUpdateStackAction({
        actionName: "DeployPreProdApplication",
        templatePath: cdkBuildOutput.atPath(
          "PreProdApplicationStack.template.json"
        ),
        stackName: "PreProdApplicationStack",
        adminPermissions: true,
        variablesNamespace: "PreProdVariables",
        outputFileName: "PreProdOutputs",
        output: preProdOutput,
      });

Then I want to to access variables/outputs from the deployed template in a build action

// build action
    const integtestBuildAction = new aws_codepipeline_actions.CodeBuildAction({
      environmentVariables: {
        SERVICE_URL: {
          value: "HOW TO ACCESS VARIABLES/OUTPUTS from deployPreProd?",
        },
      },
      actionName: "IntegTest",
      project: integtestCodeBuild,
      input: sourceOutput,
    });
hai
asked 2 years ago1164 views
3 Answers
1

CDK has a Fn.importValue method you can use to access exported values: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Fn.html - it should help if the stack that has the exported value exists and is in the same region as your calling stack.

AWS
guy
answered 2 years ago
  • Thank you, I understand the cross-stack reference. But I wanted to pass a stack outputs to a CodeBuild action as an environment variable. For example, I want to pass API URL after deployment for some integration test in CodeBuild.

0

Yes, I mentioned in my first reply that importValue requires

the stack that has the exported value exists and is in the same region as your calling stack

If the stack hasn't been created the outputs won't exist.

It may be possible to pass the value of api.url directly if this is all in one CDK app, by creating stack properties that you pass to the stack that requires them, but I don't know from the info you've given if api.url will be synthesised at that point.

It may be (again, hard to know without seeing all your code) that the solution you have arrived at is the only one that will work in your case, as you can only access the exported value after the stack that exports it has been created.

AWS
guy
answered 2 years ago
-1

Have you tried the following?

import * as cdk from 'aws-cdk-lib';

// build action
    const integtestBuildAction = new aws_codepipeline_actions.CodeBuildAction({
      environmentVariables: {
        SERVICE_URL: {
          value: cdk.Fn.importValue('Url'),
        },
      },
AWS
guy
answered 2 years ago
  • Error because the importValue("Url") is not existed yet. When we writing the pipeline, none stacks deployed yet, so the Url is not existed. We need to access the Url at runtime instead. My work around is that, in the BuildAction I run a query to find the output Url. However, this is not convenient.

    `SERVICE_URL=$(aws cloudformation describe-stacks --stack-name PreProdApplicationStack --query "Stacks[0].Outputs[?OutputKey=='UrlPreProd'].OutputValue" --output text)`
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions