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
已提問 2 年前檢視次數 1183 次
3 個答案
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
已回答 2 年前
  • 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
已回答 2 年前
-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
已回答 2 年前
  • 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)`
    

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南