简体   繁体   中英

AWS CodePipeline: How to pass output from CloudFormation action to a CodeBuild action

I have an AWS CodePipeline defined in a CloudFormation template.

One of the steps of the pipeline is a CloudFormation action that builds a test environment for my application. The URL of the app's Elastic Load Balancer is one of the outputs defined in this template.

How do I access this output, so that I can use it in the next step, which is a CodeBuild action that will run api tests against the test environment?

I can specify an OutputFileName in the CloudFormation Deploy step, but the CodeBuild step would then need 2 input artifacts, the test code and the CloudFormation output file, and there is a maximum of 1 input artifact.

I thought that I could have a step that pushes the CloudFormation output file to S3, so that the api test step can then access it (or simply push it to S3 as part of the CloudFormation::Init code), but I am hoping for something more simple that I could configure in the CodePipeline steps (similar to !ImportValue in CloudFormation).

I also thought that I could assign a 'test url' eg http://test.myapp.com , in the CloudFormation step so that the api tests are always run against that specific url, which would be a great option, except that I am spinning up the test environment dynamically just before the tests (because I don't want the cost of it always running), and I'm not sure that the DNS will have propagated by the time the api tests start running.

Thanks in advance

Definition of CodePipeline CloudFormation action that spins up test environment

...
Name: DeployApplicationInfra
ActionTypeId:
  Category: Deploy
  Owner: AWS
  Version: 1
  Provider: CloudFormation
Configuration:
  ActionMode: CREATE_UPDATE
  Capabilities: CAPABILITY_IAM
  OutputFileName: test-application-infra-stack.outputs.json
  StackName: test-application-infra-stack
  RoleArn: !Ref CloudFormationServiceRoleArn
  TemplateConfiguration: AppInfraCfnTemplate::test-application-infra.config.json
  TemplatePath: AppInfraCfnTemplate::application-infra.yml
InputArtifacts:
  - Name: AppInfraCfnTemplate
OutputArtifacts:
  -  Name: TestApplicationInfraStackOutputs

Definition of CodePipeline CodeBuild action to run api tests

...
Name: RunApiTests
ActionTypeId:
  Category: Test
  Owner: AWS
  Version: 1
  Provider: CodeBuild
Configuration:
  ProjectName: !Ref ApiTestsBuildProject
InputArtifacts:
  - Name: AppSource

...
ApiTestsBuildProject:
  Type: AWS::CodeBuild::Project
  Properties:
    Name: companyinsight-api-tests-build
    Artifacts:
      Type: CODEPIPELINE
    Environment:
      ComputeType: BUILD_GENERAL1_SMALL
      Image: aws/codebuild/nodejs:8.11.0
      Type: LINUX_CONTAINER
    ServiceRole: !Ref CIServerBuildRoleArn
    Source:
      Type: CODEPIPELINE
      BuildSpec: !Sub |
        version: 0.2
        env:
          variables:
            BASE_URL: <Want to inject test app url here!!!>
        phases:
          install:
            commands:
              - npm install
          build:
            commands:
              - npm run api-test

Unfortunately there's no workaround to allow 2 input artifacts for CodeBuild from CodePipeline currently.

The alternative I'd recommend for now is to call aws cloudformation describe-stacks from the CLI in your build process and pull the ELB hostname from your stack outputs.

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