/// <summary> /// Imports the resources by calling <c>terraform import</c> on each. /// </summary> /// <param name="warnings">Global warnings list</param> /// <param name="errors">Global errors list</param> /// <returns>List of resources that were imported.</returns> public async Task <List <ResourceMapping> > ImportResources(IList <string> warnings, IList <string> errors) { var importedResources = new List <ResourceMapping>(); var totalResources = this.ResourceMappings.Count; this.Settings.Logger.LogInformation( $"\nImporting {totalResources} mapped resources from stack \"{this.Settings.StackName}\" to terraform state..."); var imported = 0; foreach (var resource in this.ResourceMappings) { var resourceToImport = resource.PhysicalId; this.Settings.Logger.LogInformation( $"\nImporting resource {++imported}/{totalResources} - {resource.ImportAddress}"); if (ResourceImporter.RequiresResourceImporter(resource.TerraformType)) { var importer = ResourceImporter.Create( new ResourceImporterSettings { Errors = errors, Logger = this.Settings.Logger, Resource = resource, ResourcesToImport = this.ResourceMappings.Where(r => r.Module == resource.Module) .ToList(), // Only resources in same stack Warnings = warnings }, this.Settings); if (importer != null) { resourceToImport = importer.GetImportId(); if (resourceToImport == null) { continue; } } } var cmdOutput = new List <string>(); var success = this.Settings.Runner.Run( "import", false, true, msg => cmdOutput.Add(msg), "-no-color", resource.ImportAddress, resourceToImport); if (success) { importedResources.Add(resource); } else { var error = cmdOutput.FirstOrDefault(o => o.StartsWith("Error: "))?.Substring(7); errors.Add( error != null ? $"ERROR: {resource.AwsAddress}: {error}" : $"ERROR: Could not import {resource.AwsAddress}"); } } this.IsImported = true; // Update ancestry foreach (var ancestor in this.Ancestors()) { await ancestor.WriteModuleBlocksAsync(); } return(importedResources); }