Abdelrahman Ayman
Abdelrahman Ayman

Reputation: 23

SOLVED AWS CloudFormation Create stack. Unresolved resource dependencies

I am trying to create a VPC that has a couple of subnets, security group, and internet gateways. I get the error "Template format error: Unresolved resource dependencies [VPCSubnet03]"

here's part of my .yml file

Parameters:
  Subnet03CIDRBlock:
     Type: String
     Description: CIDR Block range for the public subnet 03 in the VPC if the region has more than 2 Availability Zones.


Conditions:
  Has2Azs:
    Fn::Or:
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-west-2
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-east-1
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - me-south-1
  
  HasMore:
    Fn::Not:
      - Condition: Has2Azs

Resources:
   VPCSubnet03:
      Condition: HasMore
      Type: AWS::EC2::Subnet
      Properties:
          VpcId: !Ref VPC
          CidrBlock: !Ref Subnet03CIDRBlock
          AvailabilityZone: !Select [ 2, !GetAZs '' ]
          Tags:
              - Key: Name
                Value: !Sub "${ProjectName}-Subnet03CIDRBlock"
       

and for reference, here's also my .json parameter file

[{
   "ParameterKey": "Subnet03CIDRBlock",
   "ParameterValue": "192.168.3.0/24"
},
{
   "ParameterKey": "ProjectName",
   "ParameterValue": "GreenBlueDeployment"
}]

I have googled for a bit and I have no idea what I am supposed to fix. AFAIK, all the dependencies in the VPCSubnet03 are defined.

EDIT: I had established a condition in my definition of the VPCSubnet03 and forgot to add the same condition in the respective association route table resource. Now the stack is being created!

Here's how the final sample looks like after updating my .yml file:

Parameters:
  Subnet03CIDRBlock:
     Type: String
     Description: CIDR Block range for the public subnet 03 in the VPC if the region has more than 2 Availability Zones.


Conditions:
  Has2Azs:
    Fn::Or:
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-west-2
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-east-1
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - me-south-1

  HasMore:
    Fn::Not:
      - Condition: Has2Azs

Resources:
   VPCSubnet03:
      Condition: HasMore
      Type: AWS::EC2::Subnet
      Properties:
          VpcId: !Ref VPC
          CidrBlock: !Ref Subnet03CIDRBlock
          AvailabilityZone: !Select [ 2, !GetAZs '' ]
          Tags:
              - Key: Name
                Value: !Sub "${ProjectName}-Subnet03CIDRBlock"

   Subnet03RouteTableAssociation:
      Condition: HasMore
      Type: AWS::EC2::SubnetRouteTableAssociation
      Properties:
          SubnetId: !Ref VPCSubnet03
          RouteTableId: !Ref RouteTable

Thank you again for all the help!

Upvotes: 1

Views: 1607

Answers (2)

Marcin
Marcin

Reputation: 238707

Your template is missing ProjectName and VPC parameters. Thus it should be:

Parameters:

  Subnet03CIDRBlock:
     Type: String
     Description: CIDR Block range for the public subnet 03 in the VPC if the region has more than 2 Availability Zones.

  ProjectName:
     Type: String
     Default: my-project-name

  VPC:
     Type: AWS::EC2::VPC::Id  


Conditions:
  Has2Azs:
    Fn::Or:
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-west-2
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - us-east-1
      - Fn::Equals:
        - {Ref: 'AWS::Region'}
        - me-south-1
  
  HasMore:
    Fn::Not:
      - Condition: Has2Azs

Resources:


   VPCSubnet03:
      Condition: HasMore
      Type: AWS::EC2::Subnet
      Properties:
          VpcId: !Ref VPC
          CidrBlock: !Ref Subnet03CIDRBlock
          AvailabilityZone: !Select [ 2, !GetAZs '' ]
          Tags:
              - Key: Name
                Value: !Sub "${ProjectName}-Subnet03CIDRBlock"

Upvotes: 0

Pat Myron
Pat Myron

Reputation: 4648

Recommend trying the CloudFormation Linter in VSCode to see more detailed errors inline:

Visual Studio Code screenshot

Would need to run cfn-lint on the full template to know for sure though

Upvotes: 0

Related Questions