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

aws_ec2.Vpc.from_lookup environment error #14405

Closed
jbremson opened this issue Apr 27, 2021 · 3 comments
Closed

aws_ec2.Vpc.from_lookup environment error #14405

jbremson opened this issue Apr 27, 2021 · 3 comments
Assignees
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@jbremson
Copy link

jbremson commented Apr 27, 2021

On OS X 10.15.7, I cannot get a result from from_lookup.

        vpc = aws_ec2.Vpc.from_lookup(self, "random-id-string",
                                        vpc_id='<vpc_id')

This error is returned.

jsii.errors.JSIIError: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)

However the requested env vars are set correctly.

Reproduction Steps

ecs-app.py


from aws_cdk import core
from stacks.inspector_stack import InspectorLambdaStack

env = {
    'account_name': os.getenv('CDK_DEPLOY_TO_ENV', 'stg'),
}

class Settings():

    def __init__(self, env):
        self.env = env['account_name']

settings = Settings(env)

app = core.App()

stack_env = core.Environment(account=core.Aws.ACCOUNT_ID, region=core.Aws.REGION)

InspectorLambdaStack(scope=app,
                     id='inspector-stack',
                     description='Daily cron for internal report',
                     settings=settings,
                     env=stack_env)



app.synth()```

inspector_stack.py
===============================

from aws_cdk import (
    core,
    aws_ec2
)


class InspectorLambdaStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str,
                 settings, env, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)


        vpc = aws_ec2.Vpc.from_lookup(self, "random-id-string",
                                        vpc_id='<vpc_id>')


### What did you expect to happen?

I expect an IVpc reference.

### What actually happened?

Running `cdk deploy inspector-stack -v`

```CDK toolkit version: 1.90.1 (build 0aee440)
Command line arguments: {
  _: [ 'deploy' ],
  v: 1,
  verbose: 1,
  lookups: true,
  'ignore-errors': false,
  ignoreErrors: false,
  json: false,
  j: false,
  debug: false,
  ec2creds: undefined,
  i: undefined,
  'version-reporting': undefined,
  versionReporting: undefined,
  'path-metadata': true,
  pathMetadata: true,
  'asset-metadata': true,
  assetMetadata: true,
  'role-arn': undefined,
  r: undefined,
  roleArn: undefined,
  staging: true,
  'no-color': false,
  noColor: false,
  fail: false,
  all: false,
  'build-exclude': [],
  E: [],
  buildExclude: [],
  ci: false,
  execute: true,
  force: false,
  f: false,
  parameters: [ {} ],
  'previous-parameters': true,
  previousParameters: true,
  '$0': '/usr/local/bin/cdk',
  STACKS: [ 'inspector-stack' ],
  'S-t-a-c-k-s': [ 'inspector-stack' ]
}
cdk.json: {
  "app": "python3 ecs-app.py"
}
merged settings: {
  versionReporting: true,
  pathMetadata: true,
  output: 'cdk.out',
  app: 'python3 ecs-app.py',
  context: {},
  debug: false,
  assetMetadata: true,
  toolkitBucket: {},
  staging: true,
  bundlingStacks: [ '*' ],
  lookups: true
}
Determining if we're on an EC2 instance.
Does not look like an EC2 instance.
Toolkit stack: CDKToolkit
Setting "CDK_DEFAULT_REGION" environment variable to us-west-1
Resolving default credentials
Looking up default account ID from STS
Default account ID: xxxxxxxxxxxxxxxx
Setting "CDK_DEFAULT_ACCOUNT" environment variable to xxxxxxxxxxxx
context: {
  'aws:cdk:enable-path-metadata': true,
  'aws:cdk:enable-asset-metadata': true,
  'aws:cdk:version-reporting': true,
  'aws:cdk:bundling-stacks': [ '*' ]
}
outdir: cdk.out
env: {
  CDK_DEFAULT_REGION: 'us-west-1',
  CDK_DEFAULT_ACCOUNT: 'xxxxxx',
  CDK_CONTEXT_JSON: '{"aws:cdk:enable-path-metadata":true,"aws:cdk:enable-asset-metadata":true,"aws:cdk:version-reporting":true,"aws:cdk:bundling-stacks":["*"]}',
  CDK_OUTDIR: 'cdk.out',
  CDK_CLI_ASM_VERSION: '9.0.0',
  CDK_CLI_VERSION: '1.90.1'
}
jsii.errors.JavaScriptError: 
  Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)
      at Function.getValue (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/jsii-kernel-EnLHbJ/node_modules/@aws-cdk/core/lib/context-provider.js:51:19)
      at Function.fromLookup (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/jsii-kernel-EnLHbJ/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:483:51)
      at /private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:7991:114
      at Kernel._wrapSandboxCode (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:8582:24)
      at /private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:7991:87
      at Kernel._ensureSync (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:8563:28)
      at Kernel.sinvoke (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:7991:34)
      at KernelHost.processRequest (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:9479:36)
      at KernelHost.run (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:9442:22)
      at Immediate._onImmediate (/private/var/folders/kf/031yw5wx3hnfj7by8xsjz4jc0000gn/T/tmpaymoh_4s/lib/program.js:9443:46)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "ecs-app.py", line 27, in <module>
    env=stack_env)
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/jsii/_runtime.py", line 83, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Users/joel/PycharmProjects/apto-settlements-inspector/cfn/stacks/inspector_stack.py", line 18, in __init__
    vpc_id='vpc-07460c910e714eda7')
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/aws_cdk/aws_ec2/__init__.py", line 42251, in from_lookup
    return typing.cast(IVpc, jsii.sinvoke(cls, "fromLookup", [scope, id, options]))
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/jsii/_kernel/__init__.py", line 128, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/jsii/_kernel/__init__.py", line 363, in sinvoke
    args=_make_reference_for_native(self, args),
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/jsii/_kernel/providers/process.py", line 362, in sinvoke
    return self._process.send(request, InvokeResponse)
  File "/Users/joel/PycharmProjects/venvs/asi/lib/python3.6/site-packages/jsii/_kernel/providers/process.py", line 326, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)
Subprocess exited with error 1
Error: Subprocess exited with error 1
    at ChildProcess.<anonymous> (/usr/local/Cellar/aws-cdk/1.90.1/libexec/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:122:23)
    at ChildProcess.emit (node:events:378:20)
    at ChildProcess.EventEmitter.emit (node:domain:470:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)

Environment

  • **CDK CLI Version : 1.90.1
  • **Framework Version:1.100.0
  • **Node.js Version:v15.9.0
  • OS :
  • **Language (Version): Python 3.6.8

Other

I am not using STS to access Admin privs, I using it directly.


This is 🐛 Bug Report

@jbremson jbremson added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 27, 2021
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Apr 27, 2021
@rix0rrr
Copy link
Contributor

rix0rrr commented May 10, 2021

This line is not correct:

stack_env = core.Environment(account=core.Aws.ACCOUNT_ID, region=core.Aws.REGION)

Read this page: https://docs.aws.amazon.com/cdk/latest/guide/environments.html

And specifically look at the second code example in Python.

@rix0rrr rix0rrr closed this as completed May 10, 2021
@github-actions
Copy link

⚠️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.

@teChanACN
Copy link

Doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants