2
votes

I am creating two EKS templates and one VPC template. In VPC template I have created 3 private and 3 public subnets which is working fine. So, I want one EKS should be in public and one EKS should be in private subnets. So how can do it via cloudformation.

Type: List<AWS::EC2::Subnet::Id>

This gives me list of subnets which is 6.

Ec2SubnetId: 
     !Select [0, !Ref SubnetIds]

This selects 1st subnet from all subnets. But I am not sure how to select 3 particular public or private subnet only in cloudformation.

2
@AlexHarvey Marked.ImPurshu

2 Answers

2
votes

If I understand you want a list of private & public subnets your VPC stack created. You can export your public & private subnets from the VPC stack & use them in your EKS stacks.

Outputs:
  PublicSubnets:
    Description: Public Subnets
    Value: !Join [",", [!Ref PublicSubnet1, !Ref PublicSubnet2, !Ref PublicSubnet3]]
    Export:
      Name: PublicSubnets  
  PrivateSubnets:
    Description: Private Subnets
    Value: !Join [",", [!Ref PrivateSubnet1, !Ref PrivateSubnet2, !Ref PrivateSubnet3]]
    Export:
      Name: PrivateSubnets

I think you should now be able to refer them in your EKS stack as

Ec2SubnetId: 
 !Select [0, !ImportValue PrivateSubnets]

or maybe a Split function

Ec2SubnetId:
  !Select [0, !Split [",", !ImportValue PrivateSubnets]]

Let me know if that works.

0
votes

As I understand your question, you have an expression that returns the first Subnet ID, and that is:

!Select [0, !Ref SubnetIds]

If you wanted all three of the Subnets, you could write:

[!Select [0, !Ref SubnetIds], !Select [1, !Ref SubnetIds], !Select [2, !Ref SubnetIds]]

But that expression is the same as just writing:

!Ref SubnetIds

Because SubnetIds already is a List<AWS::EC2::Subnet::Id>.