Pulumi import existing Azure ResourceGroup to Azure Core - azure

We have a Resource Group in Azure. We created this resource using the following code:
private Pulumi.Azure.Core.ResourceGroup CreateResourceGroup(string resourceGroupName)
{
return new Pulumi.Azure.Core.ResourceGroup(resourceGroupName,
new Pulumi.Azure.Core.ResourceGroupArgs
{
Name = resourceGroupName,
Location = "westeurope"
});
}
CreateResourceGroup("rg-magic-apps");
Now we need to import this structure to a completely new stack (the old stack is NOT available anymore) so our only way is to import that resourcegroup.
Following the pulumi documentation we tried
pulumi import azure:core/resourceGroup:ResourceGroup /subscriptions/{subscriptionid}/resourceGroups/rg-magic-apps
but received
error: an inline resource must be specified if no import file is used
Is there a parameter missing?

Related

Access Azure Function App system keys in Terraform

I want to create Azure EventGrid subscription using Terraform.
resource "azurerm_eventgrid_system_topic_event_subscription" "function_app" {
name = "RunOnBlobUploaded"
system_topic = azurerm_eventgrid_system_topic.function_app.name
resource_group_name = azurerm_resource_group.rg.name
included_event_types = [
"Microsoft.Storage.BlobCreated"
]
subject_filter {
subject_begins_with = "/blobServices/default/containers/input"
}
webhook_endpoint {
url = "https://thumbnail-generator-function-app.azurewebsites.net/runtime/webhooks/blobs?functionName=Create-Thumbnail&code=<BLOB-EXTENSION-KEY>"
}
}
By following this doc, I successfully deployed it and it works. However, the webhook_endpoint URL needs <BLOB-EXTENSION-KEY> which is hardcoded right now and found from the following place in the portal:
In order to not commit a secret to GitHub, I want to get this value by reference, ideally using Terraform.
According to my research, it seems there is no way in Terraform to reference that value.
The closest one is this data source azurerm_function_app_host_keys in Terraform. However, it doesn't cover the blobs_extension key!
Is there any good way to reference blobs_extension in Terraform without a hardcoded value?
Thanks in advance!
If TF does not support it yet, you can create your own External Data Source which is going to use azure cli or sdk to get the value you want, and return it to your TF for further use.

Error when creating Azure resources with Terraform

I'm trying to create Azure resources from Azure web site CLI, but strangely it throws the error below after run "terraform apply". It wasn't like this before.
Error: A resource with the ID "/subscriptions/certdab-as441-4a670-bsda-2456437/resourceGroups/commapi" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.
on terra.tf line 14, in resource "azurerm_resource_group" "rg1":
14: resource "azurerm_resource_group" "rg1" {
Terraform code is below;
provider "azurerm" {
version = "=2.20.0"
features {}
subscription_id = "c413asdasdasdadsasda1c77"
tenant_id = "da956asdasdadsadsasd25a535"
}
resource "azurerm_resource_group" "rg" {
name = "commapi"
location = "West Europe"
}
resource "azurerm_resource_group" "rg1" {
name = "commapi"
location = "West Europe"
}
Regarding to their web site, if a resource group is alredy created, we should import it. But I don't get it how?
They said I should import like below, should I add this line to my terraform code?
Or should I run this import command for every Azure tool?
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1
So for example if I already created 10 Azure resources before and if I add 11.th tool to my Terraform code, should I run this "import" command for each that 10 resources which already created before? That's so weird.
How can I create these resources?
Edit:
If I try again, throws the error below;
Error: A resource with the ID "/subscriptions/asdd-asde1-4asd-bsda-asasd/resourceGroups/commerceapi/providers
/Microsoft.ApiManagement/service/commapi-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.
on terra.tf line 65, in resource "azurerm_api_management" "apm":
65: resource "azurerm_api_management" "apm" {
Thanks!
Sample 2 :
For example when I create the API, it throws the error below;
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
azurerm_api_management.apm: Creating...
Error: A resource with the ID "/subscriptions/c4112313-123-123-123-1c77/resourceGroups/testapi/providers/Microsoft.ApiManagement/service/api-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.
on commerceApi.tf line 67, in resource "azurerm_api_management" "apm":
67: resource "azurerm_api_management" "apm" {
After that, when I try to import command, this time it throws the error below;
PS C:\Users\set\Desktop\Terraform\Test_Commerceapi> terraform import azurerm_api_management.apm /subscriptions/c4asdb-234e1-23-234a-23424337/resourceGroups/testapi
azurerm_api_management.apm: Importing from ID "/subscriptions/c4234324-234-234-23-4347/resourceGroups/testapi"...
azurerm_api_management.apm: Import prepared!
Prepared azurerm_api_management for import
azurerm_api_management.apm: Refreshing state... [id=/subscriptions/c4234324-23-4234-234324-77/resourceGroups/testapi]
Error: making Read request on API Management Service "" (Resource Group "testapi"): apimanagement.ServiceClient#Get: Invalid input: autorest/validation: validation failed: parameter=serviceName constraint=MinLength value="" details: value length must be greater than or equal to 1
If you want to bring existing infrastructure under Terraform management, you can use Import.
For example, import an existing resource group.
declare the resource in the .tf file like this.
resource "azurerm_resource_group" "rg" {
}
Then run terraform import azurerm_resource_group.rg /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1 to import the existing resource group.
Then you can edit the Terraform file and add the name and location of the existing resource to the Resource Group. After this, you are ready to use the resource. Read how to Import an Existing Azure Resource in Terraform for more details.
Please note that Terraform import can only import resources into the state. It does not generate configuration. If you just intend to create new resources relying on existing resources, you could use data sources.
Data sources allow data to be fetched or computed for use elsewhere in
Terraform configuration. Use of data sources allows a Terraform
configuration to make use of information defined outside of Terraform,
or defined by another separate Terraform configuration.
For example, use Data Source: azurerm_resource_group to access information about an existing Resource Group. You can create new resources within the existing VNet.
data "azurerm_resource_group" "example" {
name = "existing"
}
output "id" {
value = data.azurerm_resource_group.example.id
}

Terraform : Tagging ALL google compute INSTANCES at once using Terraform

I have an existing project with instances running in the project. I have authenticated in the project using Terraform and credentials file. I am suppose to get existing resource by inputing the project ID.
provider "google" {
credentials = "${file("${var.path}/terraform-ma.json")}"
project = "terraform-ma-2020"
region = "us-central1"
zone = "us-central1-b"
}
data "google_project" "project" {
}
output "project_id" {
value = data.google_project.project.project_id
}
You can import existing GCP resources into Terraform state files using the information found in https://www.terraform.io/docs/import/index.html

Importing Existing Azure Storage Account Into Terraform Resource

I'm new to Terraform, and I'm trying to import two different existing Azure Storage Accounts into two "azurerm_storage_account" modules I'm creating in Terraform, "my_storage_account" and "my_storage_account_2".
I followed the Terraform import documentation and ran:
terraform import azurerm_storage_account.my_storage_account /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Storage/storageAccounts/myaccount
...but got the following error message:
Error: resource address "azurerm_storage_account.my_storage_account" does not exist in the configuration.
Before importing this resource, please create its configuration in the root module. For example:
resource "azurerm_storage_account" "my_storage_account" {
# (resource arguments)
}
Inside the root module, I have:
resource "azurerm_storage_account" "storage_account" {
# (resource arguments)
}
It sounds like the error message is telling me to write "storage_account" instead of "my_storage_account", but how can I then import to a specific module of that resource?
You have this resource declared:
resource "azurerm_storage_account" "storage_account" {
# (resource arguments)
}
This resource is tracked internally by terrafrom with the id azurerm_storage_account.storage_account.
If you want to import a storage account and tell terraform that you mean exactly this resource, you have to use the id terraform uses internally. You can probably now see the differences in the following lines:
terraform import azurerm_storage_account.my_storage_account /subcriptions/...
terraform import azurerm_storage_account.storage_account /subcriptions/...
If you want to have multiple storage accounts managed by terraform, each of them will need to have its own unique resource stanza.
See also: https://www.terraform.io/docs/import/usage.html

Microsoft Azure Java SDK: snapshot copy

I am looking for a way to copy azure managed disk snapshots between regions using Java SDK. Any suggestions or pointers will be helpful
Thanks in advance
Prasad
You can use Java SDK to create Azure managed disk snapshots with different resource groups and regions:
Disk osDisk = azure.disks().getById(linuxVM.osDiskId());
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName)
.withRegion(Region.US_EAST)
.withExistingResourceGroup(rgName)
.withLinuxFromDisk(osDisk)
.create();
See Java: Manage Azure Managed Disks to get more details.
Update-1
If you want to copy the snapshot from other regions, just change the withLinuxFromDisk() into withLinuxFromSnapshot().
You can get more interfaces about Azure snapshot in Java SDK from Java SDK for Azure.
Update-2
For your issue that you want to create a snapshot from a snapshot. With the code example below and it works well.
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.management.compute.Snapshot;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
"xxxxxxxxx",
"xxxxxxxxx",
"xxxxxxxxx",
AzureEnvironment.AZURE);
Azure.Authenticated azureAuth = Azure.authenticate(credentials);
Azure azure = azureAuth.withDefaultSubscription();
Snapshot osSnapshot = azure.snapshots().define("managedOSSnapshotName")
.withRegion(Region.US_EAST)
.withExistingResourceGroup("charlesJava")
.withDataFromSnapshot("/subscriptions/xxxxxxx/resourceGroups/groupName/providers/Microsoft.Compute/snapshots/snapshottest")
.create();
}
}
The parameter that the .withDataFromSnapshot() is the resource Id, in other words, it's the snapshot resource Id. But first of all, you should get the authentication of the resource group which you want to use with at least Contributor permission. For this step, you can create a service principal and add the role for your resource group which the snapshot in.

Resources