问题描述
我正在学习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,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);
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”?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)