2

I am using CDKTF and python for a project where I am generating JSON output that will be interpreted by Terraform.

I have a use case where I need to send in multiple aliased AWS providers. I am able to specify a single provider to the stack by using the add_provider method but I cannot add a secondary aliased provider without using add_override.

Is there a way for me to do this without getting name conflicts in the keys where CDKTF gives an error that I am specifying the aws key twice.

Basically I am asking if there is a way for me to specify the key I use when specifying the keys in providers so that I get something like:

"providers": {
   "aws": "aws.account-one",
   "aws.two": "aws.account-two"
}

Kindly let me know if I am doing this wrong.

Thanks in advance.

nandac
  • 335
  • 3
  • 16

1 Answers1

3

With CDKTF you can specify multiple providers like this:

class MyStack extends TerraformStack {
    constructor(scope: Construct, ns: string) {
        super(scope, ns);

        new AwsProvider(this, "aws", {
            region: "eu-central-1",
        });

        const provider = new AwsProvider(this, "aws.two", {
            region: "us-east-1",
            alias: "aws.two",
        });

        // this cert is created in the us-central-1 region
        // it defaults to the provider without an alias
        new acm.AcmCertificate(this, "cert", {
            domainName: "cdktf.com",
            validationMethod: "DNS",
        });

        // this cert is created in the us-east-1 region
        const cert = new acm.AcmCertificate(this, "cert", {
            domainName: "example.com",
            validationMethod: "DNS",
            provider,
        });
    }
}

This is documented in the examples: https://github.com/hashicorp/terraform-cdk/blob/main/examples/typescript/aws-cloudfront-proxy/main.ts

Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
  • Thank you for your reply. However, what I am trying to do is specify a list of providers all of which are aliased. I do not have a default provider per se as I am targeting multiple accounts in one go. All I am doing is generating the JSON terraform with a providers block in the config that references both aws providers without the keys clashing. – nandac Dec 07 '21 at 05:08
  • That means you'd need to specify the provider you want to use all the time – Daniel Schmidt Dec 10 '21 at 14:59