Saturday 31 October 2020

How to configure AWS CDK Account and Region to look up a VPC

I am learning the AWS CDK, and this is a problem I can't seem to figure out. JS/Node are not languages I use often, so if there is some obvious native thing that I am missing, please don't be too harsh. I'm trying to deploy a container to an existing VPC / new ECS Cluster. The following code isn't my whole script but is an important part. Hopefully, it gives the idea of what I'm trying to do.

//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();

When I run CDK synth, it spits out:

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)

But I don't know why. My usage here fits several other Stackoverflow answers to similar questions, it loos like the examples in the AWS docs, and when I console.log(process.env), it spits out the correct/expected values of CDK_DEFAULT_REGION and CDK_DEFAULT_ACCOUNT. When I log "env" it spits out the expected values as well.

So my question is, how do I configure my environment so ec2.Vpc.fromLookup knows my account info, or how do I pass the values properly to "env"?



from How to configure AWS CDK Account and Region to look up a VPC

No comments:

Post a Comment