Skip to content
Snippets Groups Projects
Commit 98e14015 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3217980 by Xiaohua Guan, yas, baldwinlouie: Fix errors while launching...

Issue #3217980 by Xiaohua Guan, yas, baldwinlouie: Fix errors while launching an EC2 instance from AWS Cloud Launch Template
parent 9ffb0e44
No related branches found
No related tags found
No related merge requests found
......@@ -62,14 +62,16 @@ class Ec2BatchOperations {
// Get instance IAM roles associated to instances.
$instance_iam_roles = [];
$associations_result = $ec2Service->describeIamInstanceProfileAssociations();
foreach ($associations_result['IamInstanceProfileAssociations'] ?: [] as $association) {
$instance_iam_roles[$association['InstanceId']]
= $association['IamInstanceProfile']['Arn'];
if (!empty($associations_result)) {
foreach ($associations_result['IamInstanceProfileAssociations'] ?: [] as $association) {
$instance_iam_roles[$association['InstanceId']]
= $association['IamInstanceProfile']['Arn'];
}
}
// Get volumeIDs associated to instances.
$block_devices = [];
if (isset($instance['BlockDeviceMappings'])) {
if (!empty($instance['BlockDeviceMappings'])) {
foreach ($instance['BlockDeviceMappings'] ?: [] as $block_device) {
$block_devices[] = $block_device['Ebs']['VolumeId'];
}
......@@ -84,7 +86,7 @@ class Ec2BatchOperations {
$cloud_context,
Instance::TAG_LAUNCHED_BY_UID
);
if (!isset($instance['Tags'])) {
if (empty($instance['Tags'])) {
$instance['Tags'] = [];
}
foreach ($instance['Tags'] ?: [] as $tag) {
......@@ -133,7 +135,8 @@ class Ec2BatchOperations {
'InstanceId' => $instance['InstanceId'],
'Attribute' => 'disableApiTermination',
]);
$termination_protection = $attribute_result['DisableApiTermination']['Value'];
$termination_protection = !empty($attribute_result) ? $attribute_result['DisableApiTermination']['Value'] : FALSE;
// Get user data.
$attribute_result = $ec2Service->describeInstanceAttribute([
......@@ -146,14 +149,16 @@ class Ec2BatchOperations {
}
// Instance IAM roles.
$iam_role = $instance_iam_roles[$instance['InstanceId']] ?? NULL;
$iam_role = !empty($instance_iam_roles[$instance['InstanceId']])
? $instance_iam_roles[$instance['InstanceId']]
: NULL;
// Use NetworkInterface to look up private IPs. In EC2-VPC,
// an instance can have more than one private IP.
$network_interfaces = [];
$private_ips = FALSE;
if (isset($instance['NetworkInterfaces'])) {
if (!empty($instance['NetworkInterfaces'])) {
$private_ips = $ec2Service->getPrivateIps($instance['NetworkInterfaces']);
foreach ($instance['NetworkInterfaces'] ?: [] as $interface) {
$network_interfaces[] = $interface['NetworkInterfaceId'];
......@@ -172,24 +177,23 @@ class Ec2BatchOperations {
$entity->setInstanceState($instance['State']['Name']);
// Set attributes that are available when system starts up.
$public_ip = NULL;
if ($private_ips !== FALSE) {
$entity->setPrivateIps($private_ips);
}
if (isset($instance['PublicIpAddress'])) {
$public_ip = $instance['PublicIpAddress'];
}
$public_ip = !empty($instance['PublicIpAddress']))
? $instance['PublicIpAddress']
: NULL;
if (!empty($instance['NetworkInterfaces'])) {
if (!empty($instance['NetworkInterfaces'][0]['Association']) && !empty($instance['NetworkInterfaces'][0]['Association']['CarrierIp'])) {
$public_ip = $instance['NetworkInterfaces'][0]['Association']['CarrierIp'];
}
}
if (isset($instance['PublicDnsName'])) {
if (!empty($instance['PublicDnsName'])) {
$entity->setPublicDns($instance['PublicDnsName']);
}
if (isset($instance['PrivateDnsName'])) {
if (!empty($instance['PrivateDnsName'])) {
$entity->setPrivateDns($instance['PrivateDnsName']);
}
......@@ -222,13 +226,11 @@ class Ec2BatchOperations {
'tenancy' => $instance['Placement']['Tenancy'],
'instance_state' => $instance['State']['Name'],
'public_dns' => $instance['PublicDnsName'],
'public_ip' => $instance['PublicIpAddress'] ?? NULL,
'private_dns' => $instance['PrivateDnsName'] ?? NULL,
'key_pair_name' => $instance['KeyName'],
'is_monitoring' => $instance['Monitoring']['State'],
'vpc_id' => $instance['VpcId'] ?? NULL,
'subnet_id' => $instance['SubnetId'] ?? NULL,
'source_dest_check' => $instance['SourceDestCheck'] ?? NULL,
'public_ip' => !empty($instance['PublicIpAddress']) ? $instance['PublicIpAddress'] : NULL,
'private_dns' => !empty($instance['PrivateDnsName']) ? $instance['PrivateDnsName'] : NULL,
'vpc_id' => !empty($instance['VpcId']) ? $instance['VpcId'] : NULL,
'subnet_id' => !empty($instance['SubnetId']) ? $instance['SubnetId'] : NULL,
'source_dest_check' => !empty($instance['SourceDestCheck']) ? instance['SourceDestCheck'] : NULL,
'ebs_optimized' => $instance['EbsOptimized'],
'root_device_type' => $instance['RootDeviceType'],
'root_device' => $instance['RootDeviceName'],
......@@ -237,8 +239,8 @@ class Ec2BatchOperations {
'virtualization' => $instance['VirtualizationType'],
'reservation' => $instance['reservation_id'],
'ami_launch_index' => $instance['AmiLaunchIndex'],
'host_id' => $instance['Placement']['HostId'] ?? NULL,
'affinity' => $instance['Placement']['Affinity'] ?? NULL,
'host_id' => !empty($instance['Placement']['HostId']) ? $instance['Placement']['HostId']) : NULL,
'affinity' => !empty($instance['Placement']['Affinity']) ? $instance['Placement']['Affinity'] : NULL,
'state_transition_reason' => $instance['StateTransitionReason'],
'instance_lock' => FALSE,
'launch_time' => strtotime($instance['LaunchTime']->__toString()),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment