title: Access AWS CodeBuild Variables in AWS CodePipeline
body: <p>I am trying to use the AWS CodeBuild Id as a tag for my docker image. This docker image is built in the build phase of CodeBuild. I want to get this Coudebuild Id which is my docker tag in AWS Code Pipeline phase. How do i access these codebuild environment variables in aws codepipeline?</p>
  <p>CodeBuild Phase:</p>
  <pre class="lang-yaml prettyprint-override"><code>    CodeBuildProject:
      Type: AWS::CodeBuild::Project
      Properties:
        Artifacts:
          Location: !Ref ArtifactBucket
          Type: &quot;S3&quot;
        Source:
          Location: !Sub ${ArtifactBucket}/source.zip
          Type: &quot;S3&quot;
          BuildSpec: |
            version: 0.1
            phases:
              pre_build:
                commands:
                  - $(aws ecr get-login --region $AWS_DEFAULT_REGION)
                  - sudo apt-get update
                  - echo Pulling maven image...
                  - docker pull maven:3.3-jdk-8
                  - echo done with the pre build phase
              build:
                commands:
                  - echo Build started on `date`
                  - printf &quot;%s&quot; $REPOSITORY_URI
                  - docker run -i --rm -w /opt/maven -v $PWD:/opt/maven -v $HOME/.m2:/root/.m2 maven:3.3-jdk-8 mvn clean install
                  - docker build --file Dockerfile --tag $REPOSITORY_URI:$CODEBUILD_BUILD_ID .
              post_build:
                commands:
                  - echo post build
                  - docker push $REPOSITORY_URI:$CODEBUILD_BUILD_ID
              discard-paths: yes
        Environment:
          ComputeType: &quot;BUILD_GENERAL1_LARGE&quot; 
          Image: &quot;aws/codebuild/docker:1.12.1&quot;
          Type: &quot;LINUX_CONTAINER&quot;
          EnvironmentVariables:
            - Name: AWS_DEFAULT_REGION
              Value: !Ref AWS::Region
            - Name: REPOSITORY_URI
              Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}
            - Name: PipelineName
              Value: !Ref PipelineName
        Name: !Ref AWS::StackName
        ServiceRole: !Ref CodeBuildServiceRole
  </code></pre>
  <p>Here my docker image is now a combination of my Repository Url and my codebuild id. I want to use this codebuild id in the deploy phase of aws codepipeline, how to get it?</p>
  <pre class="lang-yaml prettyprint-override"><code>     Pipeline:
      Type: AWS::CodePipeline::Pipeline
      Properties:
        Name: !Ref PipelineName
        RoleArn: !GetAtt CodePipelineServiceRole.Arn
        ArtifactStore:
          Type: S3
          Location: !Ref ArtifactBucket
        Stages:
          - Name: Source
            Actions:
              - Name: GitHubRepoSource
                ActionTypeId:
                  Category: Source
                  Owner: ThirdParty
                  Provider: GitHub
                  Version: 1
                Configuration:
                  Owner: !Ref GitHubUser
                  Repo: !Ref GitHubRepo
                  Branch: !Ref GitHubBranch
                  OAuthToken: !Ref GitHubToken
                OutputArtifacts:
                  - Name: GitHubRepoSource
                RunOrder: 1
          - Name: Build
            Actions:
              - Name: Build
                ActionTypeId:
                  Category: Build
                  Owner: AWS
                  Version: 1
                  Provider: CodeBuild
                Configuration:
                  ProjectName: !Ref CodeBuildProject
                InputArtifacts:
                  - Name: GitHubRepoSource
                OutputArtifacts:
                  - Name: BuildOutput
                RunOrder: 1
          - Name: Deploy
            Actions:
              - Name: Deploy
                ActionTypeId:
                  Category: Deploy
                  Owner: AWS
                  Version: 1
                  Provider: CloudFormation
                Configuration:
                  ChangeSetName: Deploy
                  ActionMode: CREATE_UPDATE
                  StackName: !Sub &quot;${AWS::StackName}-Service&quot;
                  Capabilities: CAPABILITY_NAMED_IAM
                  TemplatePath: https://s3.amazonaws.com/cicdoveraws-visa/service.yaml
                  RoleArn: !GetAtt CloudFormationExecutionRole.Arn
                  ParameterOverrides: !Sub |
                    {
                      &quot;Tag&quot; : &quot;${}&quot;,
                      &quot;DesiredCount&quot;: &quot;2&quot;,
                      &quot;Cluster&quot;: &quot;${ECSCluster}&quot;,
                      &quot;TargetGroup&quot;: &quot;${ECSTG}&quot;,
                      &quot;ImageName&quot;: &quot;${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}:&lt;Tag Name&gt;,
                      &quot;ContainerName&quot;: &quot;${ContainerName}&quot;,
                      &quot;Cpu&quot;: &quot;${Cpu}&quot;,
                      &quot;Memory&quot;: &quot;${Memory}&quot;,
                      &quot;ContainerPort&quot;: &quot;${ContainerPort}&quot; 
                    }
                InputArtifacts:
                  - Name: BuildOutput
                RunOrder: 1
  </code></pre>