
AWS CLIで旧NAT ゲートウェイのEIPを新しいNAT ゲートウェイで再利用してみた
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
こんにちは、AWS事業本部コンサルティング部の酒井です。
対向システムのIP制限の関係でElastic IP(EIP)を流用する必要があったので、手順をまとめてみました。
皆さんの環境構築の参考になれば幸いです。
Elastic IPの再利用手順
1.既存のNAT Gatewayの削除
EIPの関連付けのみを解除することができないので、NAT Gatewayを削除します。
$ aws ec2 delete-nat-gateway --nat-gateway-id {NatGatewayId}
{
    "NatGatewayId": "nat-xxxxxxxxxxxxxxxxx"
}
※1aws ec2 disassociate-addressコマンドを実行すると、以下のエラーになります。
$ aws ec2 disassociate-address --association-id {EIPのAssociationId}
An error occurred (AuthFailure) when calling the DisassociateAddress operation: You do not have permission to access the specified resource.
※2aws ec2 disassociate-nat-gateway-addressコマンドを実行すると、以下のエラーになります。
$ aws ec2 disassociate-nat-gateway-address --nat-gateway-id {NatGatewayId} --association-ids {EIPのAssociationId}
An error occurred (AuthFailure) when calling the DisassociateAddress operation: You do not have permission to access the specified resource.
セカンダリIPであれば、エラーなく関連付けの解除が可能です。
$ aws ec2 disassociate-nat-gateway-address --nat-gateway-id {NatGatewayId} --association-ids {セカンダリEIPのAssociationId}
{
    "NatGatewayId": "nat-xxxxxxx",
    "NatGatewayAddresses": [
        {
            "AllocationId": "eipalloc-xxxxxxx",
            "NetworkInterfaceId": "eni-xxxxxxxx",
            "PrivateIp": "xxx.xxx.xxx.xxx",
            "PublicIp": "xxx.xxx.xxx.xxx",
            "AssociationId": "eipassoc-xxxxxxxx",
            "IsPrimary": false,
            "Status": "disassociating"
        }
    ]
}
2. 新しいNAT Gatewayの作成と既存EIPの指定
$ aws ec2 create-nat-gateway --subnet-id {SubnetId} --allocation-id {EIPのAllocationId} --tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value={任意のタグ名}}]'
{
    "ClientToken": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "NatGateway": {
        "CreateTime": "2024-01-15T03:31:00+00:00",
        "NatGatewayAddresses": [
            {
                "AllocationId": "eipalloc-xxxxxxxxxxxxxxxxx",
                "IsPrimary": true,
                "Status": "associating"
            }
        ],
        "NatGatewayId": "nat-xxxxxxxxxxxxxxxxx",
        "State": "pending",
        "SubnetId": "subnet-xxxxxxxxxxxxxxxxx",
        "VpcId": "vpc-xxxxxxxxxxxxxxxxx",
        "Tags": [
            {
                "Key": "Name",
                "Value": "new-natgateway"
            }
        ],
        "ConnectivityType": "public"
    }
}
以下のようになっていれば完了です。

おわりに
AWS環境でのリソース管理は、しばしばElastic IPのような貴重なアセットの再利用を必要とします。
この記事を通じて、同一AWSアカウント内でEIPの再利用に役立てていただければ幸いです。
異なるアカウント間でEIPを移動する必要がある場合は、別途手順が必要になるので以下の記事を参考にしてみてください。
参考
【ネットワークインターフェースのデタッチで】You do not have permission to access the specified resource.が出る







