Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Availability Zone from Subnet Parameter in CloudFormation

I have

Parameters
  Zookeeper1SubnetParam:
    Description: Subnet where Zookeeper 1 should run
    Type: AWS::EC2::Subnet::Id
  Zookeeper1AZ:
    Description: Availability Zone of the Subnet
    Type: AWS::EC2::AvailabilityZone::Name

From this I'm creating an ENI (which requires a subnet) and an EBS Volume (which requires an availability zone).

Here's the ENI:

Zookeeper1IPResource:
    Properties:
      Description: Zookeeper1-IP
      GroupSet:
        - Fn::GetAtt:
            - ZookeeperSecurityGroup
            - GroupId
      PrivateIpAddress:
        Ref: Zookeeper1IPParam
      SubnetId:
        Ref: Zookeeper1SubnetParam
    Type: AWS::EC2::NetworkInterface

And here's the EBS:

Zookeeper1EBSVolume:
    Properties:
      AvailabilityZone:
        Ref: Zookeeper1AZ
      Size: 8
      VolumeType: gp2
    Type: AWS::EC2::Volume

I find it really bad for user experience, to also ask as a parameter for an availability zone, because it can be deducted from the selected subnet

Now, the million dollar question, how do I get the Availability Zone from the Subnet in CloudFormation? As far as I can tell, I can't do a GetAtt for AZ on my ENI.

Any solution welcome!

like image 488
Stephane Maarek Avatar asked Jul 25 '18 07:07

Stephane Maarek


People also ask

Can we define availability zones for subnet?

When you enable an Availability Zone for your load balancer, you specify one subnet from that Availability Zone. Note that you can enable at most one subnet per Availability Zone for your load balancer.

How do you reference parameters in CloudFormation?

Referencing a parameter within a template You use the Ref intrinsic function to reference a parameter, and AWS CloudFormation uses the parameter's value to provision the stack. You can reference parameters from the Resources and Outputs sections of the same template.

Can VPC Cross availability zones?

Amazon VPC is currently available in multiple Availability Zones in all Amazon EC2 regions. Q. Can a VPC span multiple Availability Zones? Yes.


1 Answers

To answer your question, you can't retrieve the Availability Zone from the Subnet.

But if you have total control of the template or resources that supplies the parameter to your template there are workarounds

If have control over the source the provides you the Subnet parameter, you can return also the Availability Zone from that source as an Outputs and supply it in your template as a parameter where you create ENI and EBS.

In addition, you could also create the Subnet in the same template you will create the ENI and EBS and use the { "Fn::GetAtt" : [ "mySubnet", "AvailabilityZone" ] }

Question(sorry, my rep can't allow me to comment yet)

Do you happen to have dynamic values or resources to be created that depends on availability zones? If yes, you can create Mappings and if that is not enough, you could add Conditions in your template.

like image 167
gzz Avatar answered Sep 21 '22 12:09

gzz