Multiple AWS providers using CDKTF

1.4k Views Asked by At

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.

1

There are 1 best solutions below

2
On

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