aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.58k stars 3.88k forks source link

Cannot Specify Subnets When Creating Elasticsearch Domain in CDK #16079

Closed kkzhao63 closed 3 years ago

kkzhao63 commented 3 years ago

:question: SubnetSelection not available to choose specific subnet in an existing VPC

The Question

When I am creating Elasticsearch domain using AWS CDK, I cannot specify vpc subnets. According to DomainProps interface document (https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticsearch.DomainProps.html ), it has vpcSubnets property in which I should be able to specify SubnetSeletion (https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ec2.SubnetSelection.html ) with subnetType and availabilityZones with something like:

   vpcSubnets: {     
     subnetType: ec2.SubnetType.PRIVATE,
     availabilityZones: ['usw2-az1','usw2-az2'],
   }

However, I always got the following error message:

===== error message begins ====== cdk synth vxCabinetElasticSearch ⨯ Unable to compile TypeScript: lib/elasticsearch-stack.ts:38:10 - error TS2322: Type '{ subnetType: ec2.SubnetType.PRIVATE; availabilityZones: string[]; }' is not assignable to type 'SubnetSelection[]'. Object literal may only specify known properties, and 'subnetType' does not exist in type 'SubnetSelection[]'.

38 subnetType: ec2.SubnetType.PRIVATE,


  node_modules/@aws-cdk/aws-elasticsearch/lib/domain.d.ts:611:14
    611     readonly vpcSubnets?: ec2.SubnetSelection[];
The expected type comes from property 'vpcSubnets' which is declared here on type 'DomainProps'

Subprocess exited with error 1 ezhao@ESDD-65284L:~/CDK/e-cabinet-app-infrastructure$ ===== error message ends ======

Environment

Other information

import as cdk from '@aws-cdk/core'; import as es from '@aws-cdk/aws-elasticsearch'; import * as ec2 from '@aws-cdk/aws-ec2'; import { IEnvironmentConfig } from '../config/environment-config'; import { EnvironmentConfig } from '../config/environment-config';

const appConfig = EnvironmentConfig.predevEnvConfig

export interface CustomStackProps extends cdk.StackProps { envConfig: IEnvironmentConfig, } export class vxCabinetElasticSearchStack extends cdk.Stack{ constructor(scope: cdk.Construct, id: string, props: CustomStackProps) { super(scope, id, props);

const vpc = ec2.Vpc.fromLookup(this, 'ImportVPC', { tags: { AppID: props.envConfig.networking.tag } });
const subSelection = vpc.selectSubnets({subnetType: ec2.SubnetType.PRIVATE});
// const securityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, 'SecurityGroup', props.envConfig.securityGroup.rds);

const domainProps: es.DomainProps = {
   version: es.ElasticsearchVersion.V7_9,  
   capacity: {
      masterNodes: 3,
      dataNodes: 2,
      masterNodeInstanceType: 't3.small.elasticsearch',
      dataNodeInstanceType:  't3.small.elasticsearch',
   },       
   enforceHttps: true,
   nodeToNodeEncryption: true,
   encryptionAtRest: {
     enabled: true,
   },       
   fineGrainedAccessControl: {
    masterUserName: 'esmasteruser',
   },
   vpc,
   vpcSubnets: {     
     subnetType: ec2.SubnetType.PRIVATE,
     availabilityZones: ['usw2-az1','usw2-az2'],
   },
   zoneAwareness:{
     enabled: true,
     availabilityZoneCount: 2,
   }
 }
 const esDomainID = new es.Domain(this, 'vxCabinetsDomain', domainProps)

} }

BenChaimberg commented 3 years ago

You need to specify SubnetSelection[], aka an array of SubnetSelection. Currently, you are just supplying one SubnetSelection

github-actions[bot] commented 3 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.