简体   繁体   中英

Question about function of AWS CDK TypeScript

I developed code to create VPC and subnets by TypeScript language of CDK.

import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';

export class VpcTestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const project = 'service';
    const environment = 'dev';
    const cidr = '10.1';

    // VPC
    const vpc = new ec2.CfnVPC(this, 'vpc', {
      cidrBlock: cidr + '.0.0/16',
      enableDnsHostnames: true,
      enableDnsSupport: true,
      instanceTenancy: 'default',
      tags: [ { key: 'Name', value: project + '-' + environment } ]
    });

    // Subnet-Public-01-A
    const subnet_public_01_a = new ec2.CfnSubnet(this, 'subnet_public_01_a', {
      availabilityZone: this.availabilityZones[0],
      cidrBlock: cidr + '.0.0/20',
      vpcId: vpc.ref,
      tags: [ { key: 'Name', value: project + '-' + environment + '-public-01-a' } ]
    });

    // Subnet-Public-01-C
    const subnet_public_01_c = new ec2.CfnSubnet(this, 'subnet_public_01_c', {
      availabilityZone: this.availabilityZones[2],
      cidrBlock: cidr + '.16.0/20',
      vpcId: vpc.ref,
      tags: [ { key: 'Name', value: project + '-' + environment + '-public-01-c' } ]
    });  

To make the code simple, I try to use function at the subnet section.

    // Subnet Creation Function
    function subnet_creation(availability_zone: string, subnet_name: string, subnet_cidr: string)
    {
      new ec2.CfnSubnet(this, 'subnet_' + subnet_name, {
        availabilityZone: availability_zone,
        cidrBlock: cidr + subnet_cidr,
        vpcId: vpc.ref,
        tags: [ { key: 'Name', value: project + '-' + environment + '-' + subnet_name } ]
      });
    }

    // Subnet Creation
    subnet_creation(this.availabilityZones[0], 'public-01-a',  '.0.0/20')
    subnet_creation(this.availabilityZones[2], 'public-01-c',  '.16.0/20')

I encounter an error as below when I execute 'cdk synth'.

C:\...\node_modules\ts-node\src\index.ts:513
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
lib/vpc-test.ts:36:25 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

36       new ec2.CfnSubnet(this, 'subnet_' + subnet_name, {
                           ~~~~

  lib/vpc-test.ts:34:14
    34     function subnet_creation(availability_zone: string, subnet_name: string, subnet_cidr: string)
                    ~~~~~~~~~~~~~~~
    An outer value of 'this' is shadowed by this container.

Could you please give me a guidance to use function?

Keep the function outside constructor within the class.

import * as cdk from "@aws-cdk/core";
import * as ec2 from "@aws-cdk/aws-ec2";
export class VpcTestStack extends cdk.Stack {
  project = "service";
  shortEnv = "dev";
  cidr = "10.1";
  vpc: ec2.CfnVPC;

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    this.vpc = new ec2.CfnVPC(this, "vpc", {
      cidrBlock: this.cidr + ".0.0/16",
      enableDnsHostnames: true,
      enableDnsSupport: true,
      instanceTenancy: "default",
      tags: [{ key: "Name", value: this.project + "-" + this.shortEnv }],
    });

    this.subnet_creation(this.availabilityZones[0], "public-01-a", ".0.0/20");
    this.subnet_creation(this.availabilityZones[2], "public-01-c", ".16.0/20");
  }

  subnet_creation(
    availability_zone: string,
    subnet_name: string,
    subnet_cidr: string
  ) {
    new ec2.CfnSubnet(this, "subnet_" + subnet_name, {
      availabilityZone: availability_zone,
      cidrBlock: this.cidr + subnet_cidr,
      vpcId: this.vpc.ref,
      tags: [
        {
          key: "Name",
          value: this.project + "-" + this.environment + "-" + subnet_name,
        },
      ],
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM