Search code examples
amazon-web-servicesaws-cloudformationaws-codepipelineaws-codebuild

Passing EnvironmentVariables from AWS Codepipeline to CodeBuild


I am having trouble passing EnvironmentVariables from AWS Codepipeline to CodeBuild. The only documentation I can find on this topic is this AWS documentation which gives a basic example but as you see below, I require an imported sub value. All attempts give me the error: Value of property Configuration must be an object with String (or simple type) properties.

  - Name: EmptyHostingBucket
    Actions:
      - Name: EmptyHostingBucket
        RunOrder: 5
        ActionTypeId:
          Category: Build
          Owner: AWS
          Provider: CodeBuild
          Version: 1
        Configuration:
          ProjectName: !Ref CodeBuildEmptyBucket
          EnvironmentVariables:
            - Name: HOSTING_BUCKET
              Value:
                Fn::ImportValue: !Sub "${ProjectName}-website-hosting-bucket"
        InputArtifacts:
          - Name: SourceArtifacts
        OutputArtifacts:
          - Name: BuildEmptyBucket

Solution

  • This is probably because:

    The value for the EnvironmentVariables parameter takes the form of a JSON array of environment variable objects.

    Thus it should be as string, as shown here:

    EnvironmentVariables:
        !Sub
          - '[{"name":"HOSTING_BUCKET","value":"${BucketName}","type":"PLAINTEXT"}]'
          - BucketName: 
              Fn::ImportValue: !Sub "${ProjectName}-website-hosting-bucket"
    

    The above is example. Probably some adjustments still need to be made.