title: service connection created using az cli not working
body: <p>I want to automate service connection creation for a given azure devops project as a part of my deploy script. I looked into Terraform but it does not support it yet. ARM template also does not have anything for service connection. </p>
  
  <p><a href="https://docs.microsoft.com/en-us/azure/devops/cli/service_endpoint?view=azure-devops" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/devops/cli/service_endpoint?view=azure-devops</a></p>
  
  <p>This link has some notes on how to do it. I have captured the post request body as the link says, and stored it in <strong>config.json</strong> file</p>
  
  <p>I am running the following command to create a service principle. </p>
  
  <pre><code>az devops service-endpoint create --service-endpoint-configuration config.json --organization "https://dev.azure.com/ojaskale" --project "Test Project"
  </code></pre>
  
  <p>This does create a service connection but this one does not work. If I create a service principle from console It works as expected. </p>
  
  <p>I am getting following error message.</p>
  
  <blockquote>
    <p>The pipeline is not valid. Job Build: Step Docker input
    containerRegistry references service connection
    <strong>acrserviceconnectiontest</strong> which could not be found. The service
    connection does not exist or has not been authorized for use. For
    authorization details, refer to <a href="https://aka.ms/yamlauthz" rel="nofollow noreferrer">https://aka.ms/yamlauthz</a>.</p>
  </blockquote>
  
  <p>I can see <strong>acrserviceconnectiontest</strong> in my build.yml. </p>
  
  <p>Azure guidelines say I need to copy the post requests body and fill in the required details. post body does not have <strong>servicePrincipalId</strong> by default. I did some research and figured out I need to create one and assign roles to it which I am doing. There are two scripts given here, I am following those. </p>
  
  <p><a href="https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-service-principal" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-service-principal</a></p>
  
  <p>Once I create service principle I get following output.</p>
  
  <blockquote>
    <p>Service principal ID: xxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    Service principal password: xxxxxxxxxxxxxxxxxxxxxxxxxx</p>
  </blockquote>
  
  <p>I have tried both of them in place of <strong>servicePrincipalId</strong> but none of them seem to work. </p>
  
  <p>service principle id of the working service connection is different than one I create using cli. </p>
  
  <p>I think I am very close to getting it working, as I can see service connection getting created but it does not work. Can someone point me what I am missing or doing wrong? Thanks.</p>