amazon-web-servicesaws-cloudformationamazon-vpcsubnet

VPC error: Unresolved resource dependencies [VPC] in the Resources block of the template


I am creating a vpc using cloudformation but it shows an error when I run it .I have created the vpc with internet gateway and 2 subnets one is public and another one is private but when I upload the yaml file to cloudformation it shows the following error My yml file:

---
Description: An AWS VPC with two subnets.
AWSTemplateFormatVersion: 2010-09-09
Resources:
  myVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 11.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      InstanceTenancy: default
  InternetGateway:
    Type: AWS::EC2::InternetGateway
  VPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref myVPC
      InternetGatewayId: !Ref InternetGateway
  SubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: us-east-2a
      VpcId: !Ref myVPC
      CidrBlock: 11.0.1.0/24
      MapPublicIpOnLaunch: true
  SubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: us-east-2a
      VpcId: !Ref myVPC
      CidrBlock: 11.0.0.0/24
      MapPublicIpOnLaunch: false
  RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref myVPC
  InternetRoute:
    Type: AWS::EC2::Route
    DependsOn: VPCGatewayAttachment
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway
      RouteTableId: !Ref RouteTable
  SubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref RouteTable
      SubnetId: !Ref SubnetA
  SubnetBRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref RouteTable
      SubnetId: !Ref SubnetB
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: "Internet Group"
      GroupDescription: "SSH traffic in, all traffic out."
      VpcId: !Ref myVPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: "22"
          ToPort: "22"
          CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
        - IpProtocol: -1
          CidrIp: 0.0.0.0/0
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow http to client host
      VpcId: !Ref myVPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0

  RDSSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow http to client host
      VpcId: !Ref myVPC

  RDSSecurityGroupIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: !Ref RDSSecurityGroup
      IpProtocol: tcp
      FromPort: 3306
      ToPort: 3306
      SourceSecurityGroupId: !Ref InstanceSecurityGroup

  myDBSubnetGroup:
    Type: "AWS::RDS::DBSubnetGroup"
    Properties:
      DBSubnetGroupDescription: "description"
      SubnetIds:
        - !Ref SubnetB

  wahajwebserver:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0bdcc6c05dec346bf
      InstanceType: t2.micro
      KeyName: wahaj(webserver)
      SubnetId:
        Ref: SubnetA
      SecurityGroupIds: [!Ref InstanceSecurityGroup]

  wahajdbRDS:
    Type: AWS::RDS::DBInstance
    Properties:
      AllocatedStorage: 20
      AvailabilityZone: us-east-2c
      DBInstanceClass: db.t2.micro
      DBInstanceIdentifier: wahajwebserver
      DBName: wahajdb
      DBSubnetGroupName: !Ref myDBSubnetGroup
      DeleteAutomatedBackups: true
      Engine: MySQL
      MasterUsername: wahajdb
      MasterUserPassword: wahajdb
      VPCSecurityGroups: [!Ref RDSSecurityGroup]

I have tried using validate function in the cli but the error is same unable to figure out the mistake.

enter image description here

enter image description here


Solution

  • Your references to !Ref VPC should be !Ref myVPC. The !Ref VPC expects either a parameter or resource named VPC to exist, you've given your VPC resource the name myVPC.

    ---
    Description: An AWS VPC with two subnets.
    AWSTemplateFormatVersion: 2010-09-09
    Resources:
      myVPC:
        Type: AWS::EC2::VPC
        Properties:
          CidrBlock: 11.0.0.0/16
          EnableDnsSupport: true
          EnableDnsHostnames: true
          InstanceTenancy: default
      InternetGateway:
        Type: AWS::EC2::InternetGateway
      VPCGatewayAttachment:
        Type: AWS::EC2::VPCGatewayAttachment
        Properties:
          VpcId: !Ref myVPC
          InternetGatewayId: !Ref InternetGateway
      SubnetA:
        Type: AWS::EC2::Subnet
        Properties:
          AvailabilityZone: us-east-2a
          VpcId: !Ref myVPC
          CidrBlock: 11.0.1.0/24
          MapPublicIpOnLaunch: true
      SubnetB:
        Type: AWS::EC2::Subnet
        Properties:
          AvailabilityZone: us-east-2a
          VpcId: !Ref myVPC
          CidrBlock: 11.0.0.0/24
          MapPublicIpOnLaunch: false
      RouteTable:
        Type: AWS::EC2::RouteTable
        Properties:
          VpcId: !Ref myVPC
      InternetRoute:
        Type: AWS::EC2::Route
        DependsOn: VPCGatewayAttachment
        Properties:
          DestinationCidrBlock: 0.0.0.0/0
          GatewayId: !Ref InternetGateway
          RouteTableId: !Ref RouteTable
      SubnetARouteTableAssociation:
        Type: AWS::EC2::SubnetRouteTableAssociation
        Properties:
          RouteTableId: !Ref RouteTable
          SubnetId: !Ref SubnetA
      SubnetBRouteTableAssociation:
        Type: AWS::EC2::SubnetRouteTableAssociation
        Properties:
          RouteTableId: !Ref RouteTable
          SubnetId: !Ref SubnetB
      SecurityGroup:
        Type: AWS::EC2::SecurityGroup
        Properties:
          GroupName: "Internet Group"
          GroupDescription: "SSH traffic in, all traffic out."
          VpcId: !Ref myVPC
          SecurityGroupIngress:
            - IpProtocol: tcp
              FromPort: "22"
              ToPort: "22"
              CidrIp: 0.0.0.0/0
          SecurityGroupEgress:
            - IpProtocol: -1
              CidrIp: 0.0.0.0/0