Aws-cdk: Setting stack env has no affect when using assets

Created on 10 May 2020  路  3Comments  路  Source: aws/aws-cdk

I think this is related to assets. I have a stack that absolutely refuses to deploy unless I specify a region and account on the stack manually. On top of this, it completely ignores the region I set, and uses the one from my AWS profile.

Reproduction Steps

Prerequisites

  • You need a hosted zone to look up (global)
  • aws profile's default region is us-west-2.
class DNS extends cdk.Stack {
  constructor(parent, name, props = {}) {
    super(parent, name);
    // Uncomment these to work
    // this.account = props.env.account;
    // this.region = props.env.region;
    const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domain });
      const certificateArn = new acm.DnsValidatedCertificate(this, `SiteCertificateTest`, {
        domainName: props.domain,
        hostedZone: zone,
      }).certificateArn;
      new cdk.CfnOutput(this, `Certificate`, {
        value: certificateArn,
        exportName: `test-export:Cert`
    });
  }
}
const app = new cdk.App();
new DNS(app, `test-dns`, {
  domain: '<your_domain>',
  // add your account and uncomment the rest, it'll work
  env: {region: 'us-east-1'/*, account: '111111111111'*/},
});

When you try to deploy this you get an error:

Uncomment the parts of the example I provided and set your account id. Deploy the stack again. It will deploy to us-west-2. But the env specifically says us-east-1.

Additionally, just passing env does not work. You must explicitly set this.region and this.account on the stack.

Error Log

Cannot retrieve value from context provider hosted-zone 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)

Environment

  • *CLI Version :1.33.0*
  • *Framework Version:1.33.0*
  • *OS :vscode dev container mcr.microsoft.com/vscode/devcontainers/javascript-node:0.43-12*
  • *Language :javascript*

Other

Things I tried.

  • Setting the env on the stack to the region I want (us-east-1)
  • Setting AWS_DEFAULT_REGION and CDK_DEFAULT_REGION environment
  • Setting the same ^ env variables at execution time
  • Setting those same ^ variables in nodes process.env

This is :bug: Bug Report

@aws-cdassets @aws-cdaws-route53 bug needs-triage

Most helpful comment

@mneil I suspect that maybe you need to propagate props to the super call:

super(parent, name, props);

All 3 comments

I have a bit more clarity here now as I got it working. 2 things blocked me.

Codebuild - sets AWS_REGION and AWS_DEFAULT_REGION. So I had to set the region when using it there.

aws-vault - I use 99 designs aws-vault to help manage my credentials and MFA. The subshell wasn't passing the environment so the profile's default was always used.

However, there is still potentially a valid issue here and that is that simply passing {env: {region: 'us-east-1'}} to my stack has no effect. But, the stack error without it. Plus, I also have to take that env and set this.region and this.account on the stack itself.

Furthermore, account is required (seems like it shouldn't be); when region really should be enough.

@mneil I suspect that maybe you need to propagate props to the super call:

super(parent, name, props);

You are right, I did not pass the props down. I completely missed that and it makes sense thinking about it now. In the heat of debugging though I failed to see it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kent1 picture Kent1  路  3Comments

eladb picture eladb  路  3Comments

eladb picture eladb  路  3Comments

pepastach picture pepastach  路  3Comments

nzspambot picture nzspambot  路  3Comments