如何配置 AWS CDK 账户和区域以查找 VPC

问题描述 投票:0回答:6

我正在学习AWS CDK,这是一个我似乎无法弄清楚的问题。 JS/Node 不是我经常使用的语言,所以如果我缺少一些明显的原生东西,请不要太严厉。我正在尝试将容器部署到现有的 VPC/新的 ECS 集群。下面的代码不是我的整个脚本,但它是一个重要的部分。希望它能让我明白我正在尝试做什么。

//import everything first

stack_name = "frontend";

class Frontend extends core.Stack {
constructor(scope, id, props = {}) {
    super(scope, id);

    console.log("env variable " + JSON.stringify(props));
    
    const base_platform = new BasePlatform(this, id, props);

    //this bit doesn't matter, I'm just showing the functions I'm calling to set everything up

    const fargate_load_balanced_service = ecs_patterns.ApplicationLoadBalancedFargateService();
    this.fargate_load_balanced_service.taskDefinition.addToTaskRolePolicy();
    this.fargate_load_balanced_service.service.connections.allowTo();
    const autoscale = this.fargate_load_balanced_service.service.autoScaleTaskCount({});
    this.autoscale.scale_on_cpu_utilization();
    }
}

class BasePlatform extends core.Construct {
constructor(scope, id, props = {}) {
    super(scope, id);
    this.environment_name="frontend";
    
    console.log("environment variables " + JSON.stringify(process.env));

    //This bit is my problem child

    const vpc = ec2.Vpc.fromLookup(
        this, "VPC",{
        vpcId: 'vpc-##########'
    });

    //this bit doesn't matter, I'm just showing the functions I'm calling to set everything up

    const sd_namespace = service_discovery.PrivateDnsNamespace.from_private_dns_namespace_attributes();
    const ecs_cluster = ecs.Cluster.from_cluster_attributes();
    const services_sec_grp = ec2.SecurityGroup.from_security_group_id();
    }
}

const app = new core.App();

_env = {account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION };

new Frontend(app, stack_name, {env: _env});

app.synth();

当我运行 CDK 合成器时,它会输出:

错误:无法从上下文提供程序 vpc-provider 检索值,因为未在堆栈级别指定帐户/区域。在定义堆栈时使用显式帐户和区域配置“env”,或使用环境变量“CDK_DEFAULT_ACCOUNT”和“CDK_DEFAULT_REGION”从 CLI 继承环境信息(不建议用于生产堆栈)

但我不知道为什么。我在这里的用法符合其他几个 Stackoverflow 对类似问题的答案,它看起来像 AWS 文档中的示例,当我 console.log(process.env) 时,它会输出 CDK_DEFAULT_REGION 和 CDK_DEFAULT_ACCOUNT 的正确/预期值。当我记录“env”时,它也会输出预期值。

所以我的问题是,如何配置我的环境,以便 ec2.Vpc.fromLookup 知道我的帐户信息,或者如何将值正确传递给“env”?

javascript node.js amazon-web-services aws-cdk
6个回答
6
投票

据我了解,如果您想在合成时使用环境细节,您必须明确指定环境

AWS CDK 区分根本不指定 env 属性和使用 CDK_DEFAULT_ACCOUNT 和 CDK_DEFAULT_REGION 指定它。前者意味着堆栈应该合成一个与环境无关的模板。在此类堆栈中定义的构造不能使用有关其环境的任何信息。例如,您不能编写 if (stack.region === 'us-east-1') 等代码或使用 Vpc.fromLookup (Python: from_lookup) 等框架工具,这些工具需要查询您的 AWS 账户。如果没有指定明确的环境,这些功能根本不起作用;要使用它们,您必须指定 env。

如果你想与 cli 共享环境变量,你可以这样做:

new MyDevStack(app, 'dev', { 
  env: { 
    account: process.env.CDK_DEFAULT_ACCOUNT, 
    region: process.env.CDK_DEFAULT_REGION 
}});

1
投票

由于我无法发表评论,所以我在这里发布我的疑问。

从外观上看,只有一个堆栈

frontend
。所以我相信你也可以尝试在代码中硬编码account-id和region,看看它是否有效。 我也很好奇

的输出是什么
console.log("environment variables " + JSON.stringify(process.env));

1
投票

将带有

env
的 props 显式传递给父构造函数,如 Nick Cox

所述

class BasePlatform extends core.Construct {
        constructor(scope, id, props = {}) {
            super(scope, id, props);


0
投票

super(scope, id)
替换为
super(scope, id, props);

需要将 props 传递给 super 以便 vpc-provider 才能使用它。


0
投票

我去寻找这个,虽然“道具”有所帮助,但这并不是解决办法。

对于 cdk 2.114.1,这是正确的代码:

我有什么:

class ChatUIStack(Stack):
    def __init__(self, scope: Construct, construct_id: str):
        super().__init__(scope, construct_id)

什么是正确且有效的:

class ChatUIStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs):
        super().__init__(scope, construct_id, **kwargs)

-1
投票

最简单的方法是使用 aws cli(

aws configure
)。

您将需要为您的用户提供编程访问权限并从 aws 控制台生成访问密钥。 AWS CDK 文档

© www.soinside.com 2019 - 2024. All rights reserved.