amazon-web-services - VPC错误: Unresolved resource dependencies [VPC] in the Resources block of the template

标签 amazon-web-services aws-cloudformation amazon-vpc subnet

我正在使用cloudformation创建一个vpc,但运行它时显示错误。我已经创建了带有互联网网关和2个子网的vpc,一个是公共(public)的,另一个是私有(private)的,但是当我将yaml文件上传到cloudformation时,它显示出现以下错误 我的 yml 文件:

---
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]

我尝试在 cli 中使用验证功能,但错误相同,无法找出错误。

enter image description here

enter image description here

最佳答案

您对 !Ref VPC 的引用应为 !Ref myVPC!Ref VPC 需要名为 VPC 的参数或资源存在,您已将 VPC 资源命名为 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

关于amazon-web-services - VPC错误: Unresolved resource dependencies [VPC] in the Resources block of the template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62682734/

相关文章:

amazon-web-services - 将旧 RDS 流量重定向到 AWS 中的新 RDS

postgresql - AWS RDS "pg_hba.conf rejects connection for host"

amazon-web-services - 如何确定使用 AWS Terraform 资源所需的 AWS IAM 策略权限?

java - 调配 AutoScaling 策略失败 : EMR instance group doesn't exist

amazon-web-services - 在 AWS CodeBuild 中缓存 Gradle 包装器

amazon-web-services - AWS : serve multiple sites under the same domain

java - AWS 运行状况检查 URL 配置错误

amazon-web-services - 如何为 lambda 超时设置 CloudWatch 警报?

amazon-web-services - AWS - 启用延迟维护的 RedShift CloudFormation

amazon-web-services - 我们如何限制 IAM 用户仅通过 Cloudformation 启动 EC2 实例和 VPC?