Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
PaulMaddox opened this issue Mar 12, 2019 · 5 comments · Fixed by #2008 · May be fixed by MechanicalRock/account-reaper#6
Closed

Comments

@PaulMaddox
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@amirsch
Copy link

amirsch commented Mar 12, 2019

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

jogold added a commit to jogold/aws-cdk that referenced this issue Mar 13, 2019
rix0rrr pushed a commit that referenced this issue Mar 13, 2019
Check VpnGateways length in VPC provider, which would lead to failure to import VPCs 
without VPN gateways.

Fixes #1998
@automagic
Copy link

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

@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 15, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants