Reputation: 97
It has not been too long since I started working with C# and Terraform. Some things are not entirely clear and it reflects as Terraform refuses to plan my generated cloud environment stacks.
The issue seems to be rooted in cross referencing resources from different stacks and I am not sure how I can fix it.
Example Storage account stack:
internal class StorageAccounts : TerraformStack
{
internal readonly StorageAccount _storageAccount;
internal readonly StorageShare _storageShare;
/// <summary>
/// The StorageAccounts class contains all the definitions of the Azure resources which are directly related to StorageAccounts
/// </summary>
/// <param name="scope">an object of class Construct</param>
/// <param name="id">a string identifier</param>
/// <param name="nameGenerator">an object of class NameGenerator</param>
/// <param name="networking">an object of class Networking</param>
/// <param name="resourceGroup">an object of class ResourceGroups</param>
internal StorageAccounts(Construct scope, string id, NameGenerator nameGenerator, ResourceGroups resourceGroup ) : base(scope, id)
{
string[] _ips = new string[] { "111.111.111.000", "111.111.111.001" };
AzurermProvider azurermProvider = new(this, "AzureRm", new AzurermProviderConfig
{
Features = new AzurermProviderFeatures(),
});
StorageAccount storageAccount = new StorageAccount(this, "azurerm_storage_account", new StorageAccountConfig
{
Name = nameGenerator.GetResNames()["Storage"],
ResourceGroupName = resourceGroup._resourceGroup.Name,
Location = nameGenerator._region[1],
AccountTier = "Standard",
AccountKind = "StorageV2",
AccountReplicationType = "RAGRS",
AllowBlobPublicAccess = true,
NetworkRules = new StorageAccountNetworkRules
{
DefaultAction = "Deny",
VirtualNetworkSubnetIds = new string[] { "test" },
IpRules = _ips
}
});
StorageShare storageShare = new StorageShare(this, "azurerm_storage_share", new StorageShareConfig
{
Name = "authtickets",
StorageAccountName = storageAccount.Name,
Quota = 5
});
_storageAccount = storageAccount;
_storageShare = storageShare;
}
}
As per my definition of the StorageAccounts class, it's constructor takes two more objects than usual, one which is of importance and that is the ResourceGroups object.
The ResourceGroups class is at it sounds, a class dedicated to ResourceGroups and I pass it with the constructor in order to get the necessary name.
Once I synthesise the stacks through the following (which I thought was the way to go as passing the objects with their own inherent attributes would make for the right referencing of values):
internal static void Synthesise(NameGenerator nameGen)
{
HashiCorp.Cdktf.App app = new();
ResourceGroups resourceGroup = new(app, "ResourceGroup", nameGen);
StorageAccounts storageAccounts = new(app, "Storage", nameGen, resourceGroup);
Networking networking = new(app, "Network", nameGen, resourceGroup, storageAccounts);
Kubernetes kubernetes = new(app, "Kubernetes", nameGen, networking, resourceGroup);
app.Synth();
}
I get the relevant JSON files with the correct output and expected names values etc. If I initialise and plan the ResourceGroups using terraform.exe, it will complete both processes successfully.
However, once I try to initialise and plan the StorageAccounts stack, I get the following error:
data.terraform_remote_state.Storage_crossstackreferenceinputResourceGroup_C0FA14C2: Reading... data.terraform_remote_state.Storage_crossstackreferenceinputResourceGroup_C0FA14C2: Still reading... [10s elapsed] ╷ │ Error: Unable to find remote state │ │ with data.terraform_remote_state.Storage_crossstackreferenceinputResourceGroup_C0FA14C2, │ on cdk.tf.json line 21, in data.terraform_remote_state.Storage_crossstackreferenceinputResourceGroup_C0FA14C2: │ 21: "workspace": "${terraform.workspace}" │ │ No stored state was found for the given workspace in the given backend.
I have been searching around on the Terraform documentation regarding possible fixes, but they seem to vary a lot due to the examples given with AWS, HCL and TS, which I find hard to convert back in to my own context.
I've been looking at Remote Backends but it is hard to grasp as there is minimal information available to work with in C#, same goes for Remote state.
Source thread in HashiCorp's discuss: https://discuss.hashicorp.com/t/cdktf-c-terraform-plan-gives-unable-to-find-remote-state/41352/3
Any help is appreciated!
Upvotes: 0
Views: 1389
Reputation: 11921
It looks like the problem stems from a cross stack reference, so the reference of a value origin from one stack to another one. It seems like the remote state data source can not find the value in the stack the value origins from. A common reason for this is that you did not apply the source stack yet before trying to plan one that depends on it or that the execution environment has no access to the backend.
Upvotes: 2