title: AWS Elastic Beanstalk - Environment must have instance profile associated with it
body: <p>I am working on a project that will programmatically create environments in AWS elastic beanstalk. I am using the AWS SDK for PHP Version 3.</p>
  
  <p>My script creates the environment. From the AWS console, the environment is displayed in gray and says that it is terminated. Viewing the events shows that an error of "Environment must have instance profile associated with it".</p>
  
  <p><a href="https://i.stack.imgur.com/ALVd8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ALVd8.png" alt="Link to events error screenshot"></a></p>
  
  <p>I have tried using the access key and secret of two different users. One user has AmazonEC2FullAccess, IAMFullAccess, and AWSElasticBeanstalkFullAccess permissions. The other user has AWSAdmin permissions. Both users can create environments from the AWS console. </p>
  
  <p>I do not know how to associate an instance profile with an environment from the SDK. I do not see an option to do it with the createEnvironment function: <a href="https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-elasticbeanstalk-2010-12-01.html#createenvironment" rel="noreferrer">createEnvironment syntax</a>  I also do not see a way to do it when creating an instance of the ElasticBeanstalkClient object. </p>
  
  <p>My code is below. Thanks.</p>
  
  <pre><code>&lt;?php
      require 'vendor/autoload.php';
      use Aws\ElasticBeanstalk\ElasticBeanstalkClient;
      use Aws\Credentials\Credentials;
  
      $key = '***key***';
      $secret = '***secret***';
  
      $credentials = new Credentials($key, $secret);
      $ebClient = new ElasticBeanstalkClient([
          'region'  =&gt; 'us-east-2',
          'version' =&gt; '2010-12-01',
          'credentials' =&gt; $credentials
      ]);
  
      $ebEnv = $ebClient-&gt;createEnvironment([
          'ApplicationName' =&gt; 'app-from-sdk',
          'EnvironmentName' =&gt; 'env-from-sdk-1',
          'CNAMEPrefix'     =&gt; 'sdk-test1',
          'Description'     =&gt; 'Test environment created from SDK.',
          //'TemplateName'    =&gt; 'PHP 7.1 version 2.7.1',
          'SolutionStackName' =&gt; '64bit Amazon Linux 2018.03 v2.7.1 running PHP 
  7.1',
          'VersionLabel'    =&gt; 'Sample Application'
      ]);
      echo '&lt;pre&gt;';
      var_dump($ebEnv);
      echo '&lt;/pre&gt;';
  </code></pre>