gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:59:29 28 4
gpt4 key购买 nike

我正在学习 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 合成器时,它会吐出:

Error: Cannot retrieve the value from context provider vpc-provider since the account/region is 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)


但我不知道为什么。我在这里的用法适合 Stackoverflow 对类似问题的其他几个答案,它不像 AWS 文档中的示例,当我使用 console.log(process.env) 时,它会吐出 CDK_DEFAULT_REGION 和 CDK_DEFAULT_ACCOUNT 的正确/预期值。当我记录“env”时,它也会吐出预期值。
所以我的问题是,如何配置我的环境以便 ec2.Vpc.fromLookup 知道我的帐户信息,或者如何将值正确传递给“env”?

最佳答案

据我了解,您必须specify an environment explicitly如果您想在合成时使用环境细节。

The AWS CDK distinguishes between not specifying the env property at all and specifying it using CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION. The former implies that the stack should synthesize an environment-agnostic template. Constructs that are defined in such a stack cannot use any information about their environment. For example, you can't write code like if (stack.region === 'us-east-1') or use framework facilities like Vpc.fromLookup (Python: from_lookup), which need to query your AWS account. These features do not work at all without an explicit environment specified; to use them, you must specify env.


如果你想与 cli 共享环境变量,你可以这样做:
new MyDevStack(app, 'dev', { 
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION
}});

关于javascript - 如何配置 AWS CDK 账户和区域以查找 VPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64507069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com