I'm using cdktf with TypeScript to manage my Infrastructure as Code (IaC), and I'm trying to store my tfstate file in an Azure Storage Account. However, I'm encountering the following error during this process:
Initializing the backend...
src ╷
│ Error: Failed to get existing workspaces: Error retrieving keys for Storage Account "${azurerm_storage_account.xxxxx.name}": storage.AccountsClient#ListKeys: Invalid input: autorest/validation: validation failed: parameter=resourceGroupName constraint=Pattern value="${azurerm_resource_group.xxxxx-rg (xxxx-rg).name}" details: value doesn't match pattern ^[-\w\._\(\)]+$
│
My goal is to create the tfstate storage account along with the AzureBackend using cdktf. Currently, I need to create the storage account manually, which I'd like to avoid. Here's the relevant code that I'm using:
class MyStack extends TerraformStack {
constructor(scope: Construct, id: string, env: Environment) {
super(scope, id);
new AzurermProvider(this, 'AzureRm', {
features: {},
});
const tfStateResourceGroupName = env.CreateTfStateId('xxxx');
const tfStateStorageAccountName = env.CreateTfStateId('xxx');
const tfStateResourceGroup = new ResourceGroup(this, tfStateResourceGroupName, {
name: tfStateResourceGroupName,
location: 'xxxxx', // Replace with your desired location
});
const tfStateStorageAccount = new StorageAccount(this, tfStateStorageAccountName, {
name: tfStateStorageAccountName,
resourceGroupName: tfStateResourceGroup.name,
location: 'xxxx',
accountTier: 'Standard',
accountReplicationType: 'LRS',
accountKind: 'StorageV2',
enableHttpsTrafficOnly: true,
});
const tfStateContainerName = 'tfstate';
new AzurermBackend(this, 'TerraformBackend', {
resourceGroupName: tfStateResourceGroup.name,
storageAccountName: tfStateStorageAccount.name,
containerName: tfStateContainerName,
key: `${env.name}/infrastructure-terraform.tfstate`,
});
}
}
I appreciate any insights into resolving this issue and achieving my goal of creating the tfstate storage account along with the AzureBackend through code. Thank you!
Trying to create both the storage account and the Azure backend at the same time hit a snag. So, I took a different route—I set up the resource group and storage account manually first. Once that was done, I easily configured the Azure backend without any trouble.