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.
Prerequisites
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.
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)
Things I tried.
This is :bug: Bug Report
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!
Most helpful comment
@mneil I suspect that maybe you need to propagate
propsto the super call: