【问题标题】:AWS CDK ignores taskSubnets selectionAWS CDK 忽略 taskSubnets 选择
【发布时间】:2022-01-15 19:43:46
【问题描述】:

我有一个烧瓶应用程序,我正在尝试将它部署到登陆区域,使用 CDK(Typescript,v2.5.0)作为 Fargate 实例。

登陆区是我需要使用的现有 VPC,带有独立子网和私有子网。

我已经尝试了所有我能想到的组合来让负载平衡器(尝试了应用平衡和网络平衡)来使用隔离子网,但没有任何效果。

我从cdk synth 得到的错误是

deploy/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.ts:401
      throw new Error(`There are no '${subnetType}' subnet groups in this VPC. Available types: ${availableTypes}`);
            ^
*Error: There are no 'Public' subnet groups in this VPC. Available types: Isolated*

这是我的代码:

import * as cdk from "@aws-cdk/core";
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as ecsp from "aws-cdk-lib/aws-ecs-patterns";

export class DeployStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {

    super(scope, id, props);

    const defaultnonprodVPC = "existing-vpc";

    const defaultVPC = ec2.Vpc.fromLookup(this,
      "defaultVPC",
      {
        isDefault: false,
        vpcId: defaultnonprodVPC,
        tags: { "aws-cdk:subnet-type": "isolated" }
      }
    );

    const knownIsolatedSubnets = defaultVPC.isolatedSubnets;

    const monitoringSubnets = defaultVPC.selectSubnets(
      {
        subnetType: ec2.SubnetType.PRIVATE_ISOLATED
      }
    );

    const networkBalancedFargateService = new ecsp.NetworkLoadBalancedFargateService(this,
      "ConnectorMonitorService", {
        memoryLimitMiB: 512,
        desiredCount: 1,
        cpu: 256,
        taskImageOptions: {
          image: ecs.ContainerImage.fromAsset("../src")
        },
        taskSubnets:
          {
            subnetType: ec2.SubnetType.PRIVATE_ISOLATED
          },
        vpc: defaultVPC
      });
  }
}

taskSubnets 更改为任何一个

{ subnets: { knownIsolatedSubnets } }

subnetGroupName: "subnet-existing-subnet-name"

monitoringSubnets

cdk synth 没有影响。设置 assignPublicIp: false 也不会改变任何事情。

我做错了什么或错过了什么?

【问题讨论】:

  • cdk.context.json 是否缓存了预期的 vpc 和子网 context?另外,在合成之前仔细检查您是否使用tsc 编译打字稿。

标签: flask aws-cdk aws-fargate


【解决方案1】:

NetworkLoadBalancedFargateService 有一个属性publicLoadBalancer,默认为true。这使得负载均衡器面向 Internet,这在您的情况下是不正确的。您需要将其设置为 false 以便私有或隔离子网工作。

Documentation for NetworkLoadBalancedFargateService

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    相关资源
    最近更新 更多