Search code examples
aws-lambdaaws-cloudformationaws-codepipelineaws-step-functions

How to read a value from a json file in AWS Codepipeline?


My question: How can Codepipeline read the value of a field in a json file which is in SourceCodeArtifact?

I have Gthub repo that contains a file imageManifest.json which looks like this:

{
  "image_id": "docker.pkg.github.com/my-org/my-repo/my-app",
  "image_version": "1.0.1"
}

I want my AWS Codepipeline Source stage to be able to read the value of image_version from imageManifest.json and pass it as a parameter to a CloudFormation action in a subsequent stage of my pipeline.

For reference, here is my source stage.

        Stages:
        - Name: GitHubSource
          Actions:
          - Name: SourceAction
            ActionTypeId:
              Category: Source
              Owner: ThirdParty
              Version: '1'
              Provider: GitHub
            OutputArtifacts:
              - Name: SourceCodeArtifact
            Configuration:
              Owner: !Ref GitHubOwner
              Repo: !Ref GitHubRepo
              OAuthToken: !Ref GitHubAuthToken

And here is my deploy stage:

        - Name: DevQA
          Actions:
          - Name: DeployInfrastructure
            InputArtifacts:
              - Name: SourceCodeArtifact
            ActionTypeId:
              Category: Deploy
              Owner: AWS
              Provider: CloudFormation
              Version: '1'
            Configuration:
              StackName: !Ref AppName
              Capabilities: CAPABILITY_NAMED_IAM
              RoleArn: !GetAtt [CloudFormationRole, Arn]
              ParameterOverrides: !Sub '{"ImageId": "${image_version??}"}'

Note that image_version in the last line above is just my aspirational placeholder to illustrate how I hope to use the image_version json value.

How can Codepipeline read the value of a field in a json file which is in SourceCodeArtifact? StepFunctions? Lambda? CodeBuild?


Solution

  • You can use a CodeBuild step in between Source and Deploy stages.

    In CodeBuild step, read the image_version from SourceArtifact (artifact produced by soruce stage) and write to an artifact 'Template configuration' file 1 which is a configuration property of the CloudFormation action. This file can hold parameter values for your CloudFormation stack. Use this file instead of ParameterOverrides you are currently using.