0

Right now I have an EBS storage volume in the us-east-2b availability zone. However, I have some code that is automating the process of creating an instance and it's using us-east-2 as its availability zone. The problem is that the instance seems to be created in whatever availability zone it wants -- us-east-2a, us-east-2b, us-east-2c... and so when I try to attach a volume to the instance, it gives me the following error:

Aws::EC2::Errors::InvalidVolumeZoneMismatch (The volume 'vol-XXXX' is not in the same availability zone as instance 'i-XXXX')

In order to provide some consistency and avoid having this problem, I'd just like to create an instance in a specific availability zone such as us-east-2b so that it matches the same of the EBS storage volume.

I'm also using the ruby aws-sdk for this. Any idea if this is possible?

If I try specifying us-east-2b, then I get the following error:

2.5.1 :080 > @ec2 = Aws::EC2::Resource.new(region: 'us-east-2b')
Traceback (most recent call last):
        2: from (irb):80
        1: from (irb):80:in `new'
ArgumentError (:region option must a region name, not an availability zone name; try `us-east-2' instead of `us-east-2b')

so I don't understand how it's possible to solve this problem. Do I need to just keep re-creating instances in the xxx-2 availability zone until I magically end up in xxx-2b? Or do I need to figure out another way to keep replicating snapshots back and forth so that the EBS storage volume is consistent across all 3 availability zones in us-east-2a/b/c?

LewlSauce
  • 151
  • 6

1 Answers1

0

Missed a step in the documentation and realized that I could specify a specific availability zone (e.g. 2a, 2b, 2c) by providing the subnet_id when using .create_instance

For example:

        instances = @ec2.create_instances({
            min_count: 1, 
            max_count: 1,
            image_id: @ec2_config[:image_id],
            key_name: @ec2_config[:key_name],
            security_group_ids: [@ec2_config[:security_group_id]],
            instance_type: @ec2_config[:instance_type],
            subnet_id: "subnet-xxxx" # availability zone us-east-2b (same as ebs volume)
        })

LewlSauce
  • 151
  • 6
  • That sounds correct. You had specified a region "us-east-2" rather than an availability zone "us-east-2a" – Tim Apr 27 '20 at 23:45
  • @Tim yeah I can't specify us-east-2b when creating a new instance for some reason, only us-east-2 – LewlSauce Apr 27 '20 at 23:47
  • 1
    This is more of a Ruby API question. A subnet is mapped to a single availability zone, so by specifying the subnet you're basically specifying the AZ. – Tim Apr 27 '20 at 23:48