amazon-web-servicesamazon-elastic-beanstalkaws-sdkaws-php-sdk

AWS Elastic Beanstalk - Environment must have instance profile associated with it


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.

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".

Link to events error screenshot

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.

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: createEnvironment syntax I also do not see a way to do it when creating an instance of the ElasticBeanstalkClient object.

My code is below. Thanks.

<?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'  => 'us-east-2',
        'version' => '2010-12-01',
        'credentials' => $credentials
    ]);

    $ebEnv = $ebClient->createEnvironment([
        'ApplicationName' => 'app-from-sdk',
        'EnvironmentName' => 'env-from-sdk-1',
        'CNAMEPrefix'     => 'sdk-test1',
        'Description'     => 'Test environment created from SDK.',
        //'TemplateName'    => 'PHP 7.1 version 2.7.1',
        'SolutionStackName' => '64bit Amazon Linux 2018.03 v2.7.1 running PHP 
7.1',
        'VersionLabel'    => 'Sample Application'
    ]);
    echo '<pre>';
    var_dump($ebEnv);
    echo '</pre>';

Solution

  • You are missing the following attributes in your createEnvironment attribute map/hash you pass:

    OptionSettings.member.1.Namespace = aws:autoscaling:launchconfiguration
    OptionSettings.member.1.OptionName = IamInstanceProfile
    OptionSettings.member.1.Value = aws-elasticbeanstalk-ec2-role
    

    Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-create-api.html