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".
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>';
An instance profile is a container for an IAM role that you can use to pass role information to an EC2 instance when the instance starts.
In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment.
An AWS Elastic Beanstalk environment is a collection of AWS resources running an application version. You can deploy multiple environments when you need to run multiple versions of an application. For example, you might have development, integration, and production environments.
Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list. If you have many environments, use the search bar to filter the environment list.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With