Search code examples
phpamazon-web-servicesaws-sdkaws-php-sdk

DependencyViolation occurred when I delete subnet after terminate instance using aws-sdk


I control AWS environment using AWS-SDK PHP. When I delete all test environment, the problem has been occurred.

I read the document and I already know DependencyViolation is occurred if I have running instances in target subnet.

So I terminated running instance first, and after that, I tried to delete subnet, but DependencyViolation error is occurred.

I tried these sequences using this code.

<?php
$options = [
    /**
     * information about region, version ..
     */
];
$client = new Ec2Client($options);

$promise = $client->terminateInstancesAsync([
    'InstanceIds' => ['instance-id'],
])->then(function ($result) use ($client) {
    return $client->deleteSubnetAsync([
        'SubnetId' => 'subnet-id',
    ]);
});

I guess the reason of error is "instance is not terminated perfactly but delete subnet action is run".

But I have no idea what I have to do for delete subnet without DependencyViolation.


Solution

  • I could use deleteSubnet method after using terminateInstnaces method without any errors using Waiters.

    I used these code.

    $promise = $client->terminateInstancesAsync([
        'InstanceIds' => ['instance-id'],
    ])->then(function ($result) use ($client) {
        $client->waitUntil('InstanceTerminated', [
            'InstanceIds' => ['instance-id'],
        ]);
        return $client->deleteSubnetAsync([
            'SubnetId' => 'subnet-id',
        ]);
    });