<p>I'm encountering following error message when I'm trying to deploy to EKS Cluster even I've already added CodeBuild IAM role to <code>aws-auth.yaml</code> like</p>
<pre><code>- rolearn: arn:aws:iam::&lt;AWS_ACCOUNT_ID&gt;:role/codebuild-eks
  username: codebuild-eks
  groups:
    - system:masters 
</code></pre>
<blockquote>
<p>error: unable to recognize &quot;deployment.yml&quot;: Get
http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080:
connect: connection refused</p>
</blockquote>
<p>And here is my codebuild command:</p>
<pre><code>version: 0.2
phases:
  install:
    commands:
      - curl -o kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.11.10/2019-06-21/bin/linux/amd64/kubectl
      - chmod +x ./kubectl
      - mkdir -p $HOME/bin &amp;&amp; cp ./kubectl $HOME/bin/kubectl &amp;&amp; export PATH=$HOME/bin:$PATH
      - kubectl version --short --client
      - curl -o aws-iam-authenticator https://amazon-eks.s3-us-west-2.amazonaws.com/1.13.7/2019-06-11/bin/linux/amd64/aws-iam-authenticator
      - chmod +x ./aws-iam-authenticator
      - mkdir -p $HOME/bin &amp;&amp; cp ./aws-iam-authenticator $HOME/bin/aws-iam-authenticator &amp;&amp; export PATH=$HOME/bin:$PATH
      - aws-iam-authenticator help
  pre_build:
    commands:
      - echo Entered the pre_build phase...
      - echo Logging in to Amazon EKS...
      - mkdir -p ~/.kube
      - aws s3 cp s3://ppshein-eks/config ~/.kube/config
      - export KUBECONFIG=$KUBECONFIG:~/.kube/config
      - aws eks --region $AWS_DEFAULT_REGION update-kubeconfig --name $AWS_CLUSTER_NAME
  build:
    commands:
      - echo Entered the build phase...
      - echo Change directory to secondary source
      - cd $CODEBUILD_SRC_DIR
      - echo List directory
      - ls -la
      - kubectl get pods --kubeconfig ~/.kube/config
      - kubectl apply -f deployment.yml
</code></pre>
<p>Problem is when CodeBuild run this <code>kubectl apply -f deployment.yml</code> statement, I've got error message but above one <code>kubectl get pods --kubeconfig ~/.kube/config</code> is working fine.</p>
<p>Please let me know which area I've missed to add or configure. Thanks.</p>

PPShein
<p>These errors indicates that kubectl was unable to reach the kubernetes server endpoint at 127.0.0.1:8080, or the local host. Since you have configured kubeconfig with the command <code>update-kubeconfig</code>, it seems multiple configs are being merged <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files" rel="nofollow noreferrer">1</a> due to this command:</p>
<pre><code>- export KUBECONFIG=$KUBECONFIG:~/.kube/config
</code></pre>
<p>To see the resultant config that kubectl sees, run this command before the failing command:</p>
<pre><code>- kubectl config view                      # Add this
- kubectl apply -f deployment.yml
</code></pre>
<p>To fix, I recommend to change as follows in pre_build phase:</p>
<pre><code>- export KUBECONFIG=~/.kube/config
</code></pre>
<p>Or, use '--context' flag with kubectl to select the correct context.</p>
<pre><code>- export KUBECONFIG=file1:file2
- kubectl get pods --context=cluster-1
- kubectl get pods --context=cluster-2
</code></pre>
