aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

VpcNetwork.importFromContext throws Cannot read property 'VpnGatewayId' of undefined

Open PaulMaddox opened this issue 7 years ago • 5 comments

Versions

❯  cdk --version
0.25.2 (build 558d81f)

❯  cat package.json | grep aws-cdk     
    "aws-cdk": "^0.25.2"
    "@aws-cdk/aws-ec2": "^0.25.2",
    "@aws-cdk/aws-ecs": "^0.25.2",
    "@aws-cdk/cdk": "^0.25.2",

Problem Description

I have a very standard public/private VPC deployed (it was previously deployed with CDK).

I'm trying to import that VPC from a new CDK project, like so:

const vpc = ec2.VpcNetwork.importFromContext(this, 'VPC', {
    vpcName: 'Production/VPC'
})

When running cdk synth or cdk diff, an error is shown:

❯  cdk synth
Cannot read property 'VpnGatewayId' of undefined

I suspect this is related to #1899 that was merged 8 days ago. Related: #1523 (see comment by @brad-jones)

PaulMaddox avatar Mar 12 '19 04:03 PaulMaddox

Full cdk deploy -v output (with account number redacted)

CDK toolkit version: 0.25.2 (build 558d81f)
Command line arguments: { _: [ 'deploy' ],
  trace: false,
  strict: false,
  'ignore-errors': false,
  ignoreErrors: false,
  json: false,
  j: false,
  verbose: true,
  v: true,
  ec2creds: undefined,
  i: undefined,
  'version-reporting': undefined,
  versionReporting: undefined,
  'path-metadata': true,
  pathMetadata: true,
  'asset-metadata': true,
  assetMetadata: true,
  ci: false,
  version: false,
  help: false,
  h: false,
  exclusively: false,
  e: false,
  'role-arn': undefined,
  r: undefined,
  roleArn: undefined,
  'build-exclude': [],
  E: [],
  buildExclude: [],
  '$0': '/usr/local/bin/cdk',
  app: undefined,
  context: undefined,
  plugin: undefined,
  rename: undefined,
  profile: undefined,
  proxy: undefined,
  'toolkit-stack-name': undefined,
  'require-approval': undefined,
  STACKS: [] }
Determining whether we're on an EC2 instance.
Does not look like EC2 instance.
cdk.json: {
  "app": "node bin/ecs.js"
}
Setting "aws:cdk:toolkit:default-region" context to eu-west-1
Resolving default credentials
Retrieved account ID 123456789012 from disk cache
Setting "aws:cdk:toolkit:default-account" context to 123456789012
context: { 'aws:cdk:toolkit:default-region': 'eu-west-1',
  'aws:cdk:toolkit:default-account': '123456789012',
  'aws:cdk:enable-path-metadata': true,
  'aws:cdk:enable-asset-metadata': true }
outdir: /var/folders/l1/s8wdtvds75n6x4tdkk0ryhf0c753jq/T/cdkxPrJrO
outfile: /var/folders/l1/s8wdtvds75n6x4tdkk0ryhf0c753jq/T/cdkxPrJrO/cdk.out
{ version: '0.19.0',
  artifacts:
   { EcsStack:
      { type: 'aws:cloudformation:stack',
        environment: 'aws://123456789012/eu-west-1',
        properties: [Object],
        metadata: [Object],
        missing: [Object] } },
  runtime:
   { libraries:
      { '@aws-cdk/cdk': '0.25.2',
        '@aws-cdk/cx-api': '0.25.2',
        '@aws-cdk/aws-ec2': '0.25.2',
        '@aws-cdk/aws-ecs': '0.25.2',
        '@aws-cdk/aws-applicationautoscaling': '0.25.2',
        '@aws-cdk/aws-iam': '0.25.2',
        '@aws-cdk/aws-autoscaling-common': '0.25.2',
        '@aws-cdk/aws-cloudwatch': '0.25.2',
        '@aws-cdk/aws-elasticloadbalancingv2': '0.25.2',
        '@aws-cdk/aws-codedeploy-api': '0.25.2',
        '@aws-cdk/assets-docker': '0.25.2',
        '@aws-cdk/aws-cloudformation': '0.25.2',
        '@aws-cdk/aws-codepipeline-api': '0.25.2',
        '@aws-cdk/aws-events': '0.25.2',
        '@aws-cdk/aws-ecr': '0.25.2',
        '@aws-cdk/aws-lambda': '0.25.2',
        '@aws-cdk/aws-s3-notifications': '0.25.2',
        '@aws-cdk/aws-sqs': '0.25.2',
        '@aws-cdk/aws-kms': '0.25.2',
        '@aws-cdk/assets': '0.25.2',
        '@aws-cdk/aws-s3': '0.25.2',
        '@aws-cdk/aws-autoscaling': '0.25.2',
        '@aws-cdk/aws-sns': '0.25.2',
        '@aws-cdk/aws-route53': '0.25.2',
        '@aws-cdk/aws-logs': '0.25.2',
        '@aws-cdk/aws-certificatemanager': '0.25.2',
        'jsii-runtime': 'node.js/v10.11.0' } },
  stacks:
   [ { name: 'EcsStack',
       environment: [Object],
       template: [Object],
       metadata: [Object],
       missing: [Object] } ] }
Removing outdir /var/folders/l1/s8wdtvds75n6x4tdkk0ryhf0c753jq/T/cdkxPrJrO
Some context information is missing. Fetching...
Using default AWS SDK credentials for account 123456789012
Listing VPCs in 123456789012:eu-west-1
Describing VPC vpc-0dcb4c502e5d6b443
Cannot read property 'VpnGatewayId' of undefined
TypeError: Cannot read property 'VpnGatewayId' of undefined
    at VpcNetworkContextProviderPlugin.readVpcProps (/usr/local/lib/node_modules/aws-cdk/lib/context-providers/vpcs.ts:88:93)
    at process._tickCallback (internal/process/next_tick.js:68:7)

PaulMaddox avatar Mar 12 '19 05:03 PaulMaddox

Problem lies here: packages/aws-cdk/lib/context-providers/vpcs.ts#L88

// Find attached+available VPN gateway for this VPC
const vpnGatewayResponse = await ec2.describeVpnGateways({
    Filters: [
    {
        Name: 'attachment.vpc-id',
        Values: [vpcId]
    },
    {
        Name: 'attachment.state',
        Values: ['attached']
    },
    {
        Name: 'state',
        Values: ['available']
    }
    ]
}).promise();
const vpnGatewayId = vpnGatewayResponse.VpnGateways ? vpnGatewayResponse.VpnGateways[0].VpnGatewayId : undefined;

If there are no VpnGateways, the DescribeVpnGateways SDK call will return an empty array like so:

❯  aws ec2 describe-vpn-gateways                                                                                                                  │
{                                                                                                                                                 │
    "VpnGateways": []                                                                                                                             │
}                                                                                                                                                 │

The ternary operator above should check array size, rather than whether it's defined/null.

PaulMaddox avatar Mar 12 '19 05:03 PaulMaddox

A workaround I found here is using ec2.VpcNetwork.import and not importFromContext

amirsch avatar Mar 12 '19 12:03 amirsch

Now that this is fixed in master, how can I update to that? This totally breaks me...

automagic avatar Mar 13 '19 23:03 automagic

We will do a release next week. If you don't want to wait for that, you can check out the repository and do a local build. Be aware, you will need a Linux or mac otherwise the build is going to give you a lot of trouble.

rix0rrr avatar Mar 15 '19 09:03 rix0rrr