I would like to deploy a Function App that has source control from github with terraform. I use the resource azurerm_function_app that has an argument source_control but I am facing this issue :
Error: Unsupported argument
on main.tf line 137, in resource "azurerm_function_app" "example":
137: source_control = {
An argument named "source_control" is not expected here. Did you mean to
define a block of type "source_control"?
I am using that code :
provider "azurerm" {
version = "~> 2.54"
features {}
}
resource "azurerm_app_service_plan" "example" {
name = "example"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
kind = "FunctionApp"
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_function_app" "example" {
name = "example_func"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
storage_account_name = azurerm_storage_account.example.name
storage_account_access_key = azurerm_storage_account.example.primary_access_key
source_control = {
repo_url = "https://github.com/myrepo"
branch = "master"
}
}
Accordingly to the documentation, source_control is a block which means is defined without the = sign:
source_control {
}
Instead of:
source_control = {
}
With the = sign is an argument, not a block.
Related
I was looking at a GitHub project ibm-cloud-architecture/terraform-openshift4-azure to install OpenShift using Terraform.
Using Terraform 1.3.7 this project fails on the following code
resource "azurerm_lb_backend_address_pool" "internal_lb_controlplane_pool_v4" {
count = var.use_ipv4 ? 1 : 0
resource_group_name = var.resource_group_name
loadbalancer_id = azurerm_lb.internal.id
name = var.cluster_id
}
with the message
Error: Unsupported argument
on vnet/internal-lb.tf line 40, in resource "azurerm_lb_backend_address_pool" internal_lb_controlplane_pool_v4":
40: resource_group_name = var.resource_group_name
An argument named "resource_group_name" is not expected here.
Why is this code failing? How can we specify the name of a resource group with the current version of Terraform and Azure?
If you check docs for azurerm_lb_backend_address_pool you will see that it does not take resource_group_name argument. So it should be:
resource "azurerm_lb_backend_address_pool" "internal_lb_controlplane_pool_v4" {
count = var.use_ipv4 ? 1 : 0
loadbalancer_id = azurerm_lb.internal.id
name = var.cluster_id
}
Issue was caused because of syntax error. There resource_group_name is not required.
resource "azurerm_lb_backend_address_pool" "example" {
loadbalancer_id = azurerm_lb.example.id
name = "BackEndAddressPool"
}
here is the code reference and replicated the same
main tf as follow:
data "azurerm_client_config" "current" {}
resource "azurerm_resource_group" "example" {
name = "********"
location = "West Europe"
}
resource "azurerm_public_ip" "example" {
name = "swarnaPublicIPForLB"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
allocation_method = "Static"
}
resource "azurerm_lb" "example" {
name = "swarnaTestLoadBalancer"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
frontend_ip_configuration {
name = "swanraPublicIPAddress"
public_ip_address_id = azurerm_public_ip.example.id
}
}
resource "azurerm_lb_backend_address_pool" "example" {
loadbalancer_id = azurerm_lb.example.id
name = "BackEndAddressPool"
}
upon plan and apply
From Portal
I am Following this docs page to deploy azure function with app settings https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/function_app
My terraform file looks like :
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.10.0"
}
}
}
provider "azurerm" {
}
resource "azurerm_resource_group" "example" {
name = "azure-functions-test-rg"
location = "West Europe"
}
resource "azurerm_storage_account" "example" {
name = "funcdemo123shafiq"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_app_service_plan" "example" {
name = "azure-functions-test-service-plan"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_function_app" "example" {
name = "test-azure-shafiq123"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
storage_account_name = azurerm_storage_account.example.name
storage_account_access_key = azurerm_storage_account.example.primary_access_key
os_type = "linux"
version = "~4"
app_settings {
FUNCTIONS_WORKER_RUNTIME = "python"
TESTING_KEY = "TESTING_VALUE"
}
site_config {
linux_fx_version = "python|3.9"
}
}
When try to deploy this through terraform apply command , I am getting this error.
│ Error: Unsupported block type
│
│ on main.tf line 46, in resource "azurerm_function_app" "example":
│ 46: app_settings {
│
│ Blocks of type "app_settings" are not expected here. Did you mean to define argument "app_settings"? If so, use the equals sign to assign it a value.
app_setting is supported on specific version of Terraform AzureRM provider. There is bug fixed availble for those version. I have used 3.3.0 provider version and it is working for me as expected and also you can't configure the value of site_config.Its value will be decide automatically based on the result of applying this configuration, same you can check in the updated document of Terraform
main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.3.0"
}
}
}
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "example" {
name = "v-rXXXXXree"
#location = "West Europe"
}
resource "azurerm_storage_account" "example" {
name = "funcdemo123shafiq4535"
resource_group_name = data.azurerm_resource_group.example.name
location = data.azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_service_plan" "example" {
name = "azure-functions-test-service-plan1"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
os_type = "Linux"
sku_name = "Y1"
}
resource "azurerm_linux_function_app" "example" {
name = "test-azure-shafi4353"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
service_plan_id = azurerm_service_plan.example.id
storage_account_name = azurerm_storage_account.example.name
storage_account_access_key = azurerm_storage_account.example.primary_access_key
#os_type = "linux"
#version = "~3"
app_settings={
FUNCTIONS_WORKER_RUNTIME = "python"
TESTING_KEY = "TESTING_VALUE"
}
site_config {
#linux_fx_version = "python|3.9"
}
}
i am trying to create a windows webapp stack using terraform but it creates windows container service plan here is my code
can anyone please help
**code**
provider "azurerm" {
version = "= 2.69.0"
features {}
}
resource "azurerm_resource_group" "example" {
name = "functoss11"
location = "East Asia"
}
resource "azurerm_app_service_plan" "example" {
name = "ASP-ush-9388"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku {
tier = "basic"
size = "B1"
}
}
resource "azurerm_app_service" "example" {
name = "newddshaikh"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
site_config {
dotnet_framework_version = "v5.0"
}
}
The error message is:
There are some problems with the configuration, described below. The Terraform configuration must be valid before initialization so that Terraform can determine which modules and providers need to be installed.
Here's what I'm doing:
Written a terraform script in azvm.tf to create a VM.
Defined the variables resourcegroup, location, and pub_key in the variables.tf file, and call those variables in the azvm.tf file using string interpolation syntax.
Created a VM with the following features:
a) Ubuntu 18.04 server
b) VM name : any custom name
c) Admin_username : Any custom name
d) disable password authentication
e) size : Standard DS1_v2
f) Allow traffic for ssh, http, https
g) use public ssh key generated in the above step
With help of vi command we created variables.tf file as:
variable "resourcegroup"
{
default = "user-pbtwiiiuofyu"
}
variable "location"
{
default = ["East US"]
}
variable "pub_key"
{
default = ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDH7+uZHdf3SXSfz3Z80FCahr1Bt2t2u+RBlgE/QPN5Qy/CdrpcNH/8oncuDLKW5r+Ie+J5yNVwJz96gdLAQPNMmAYEPGjNct63kQp8hZPlWFdQIEyvHZwwP4YWf5vyRjozQ1PZN/0WyBjqTYmJh3z6haFxqnmIf6a9G8JldjtTPuF2oAnHaUDpqD4qBy/+OSN7CX1Lowny83NLNFoTgmm2npU+RIS2FDYpf1z30tejZSO0V3Z+iT3Xddlzh2NwunzOPv9avzwrLbSSvdbugcXTSPURMLgXwCapH9oHt4RCB/2mgsrRcLQb14FKG9UVBwzh07v2w8fDAHLnv2g4bpnhIyZMpX3zLO9mtQ4ix9DpyqPMqLV7b1uTSyj9t3UEcfprGWRPDJgkpGXEzP0iQgrGlfpctL/6/Hy009123uzb4ugXwndPbuQnPtj45PX2D4zEOFPrlvFBofazuGp65wxdzlzZpGsgqxiZQbdFCU4eQV2Y2kBPj/uv/3DOI2x27ML6i9D/shiVz/sSpryjKRHIl9bU7rU4vClgED/1NuhhTGLk5qPFOjsvvHpIORu5zFKNmeGFHW6e0TJnJMsckszseQuyjqUMkBoFV0NIOyS/mGLrxxzOTpzQyM9duat4T3kGLjjy+ozpHRaahXO79I91pDP66enitmj861kS6G5chQ== root#6badb6ae71d1"]
}
And vi command we created azvm.tf file as
az vm create -n Myuyuuy5Vm -g var.resourcegroup --ssh-key-values var.pub_key
We have another similar task but less easy in compare to it i.e.-
Write a terraform script in azmonitor.tf to create a storage account,
storage container, storage blob to monitor and send log reports
everyday.
Define the variables resourcegroup and location in the variables.tf
file, and call those variables in the azmonitor.tf file using string
interpolation syntax
variables.tf as-
variable "resourcegroup"
{
default = "user-pbtwiiiuofyu"
}
variable "location"
{
default = ["East US"]
}
azmonitor.tf we had created as-
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = var.resourcegroup
location = var.location
}
resource "azurerm_storage_account" "example" {
name = "sa123321123"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.location
sku = "Standard_LRS"
}
resource "azurerm_storage_container" "example" {
name = "sc123321123"
resource_group_name = azurerm_resource_group.example.name
account_key = azurerm_storage_account.eample.name
public_access = "blob"
}
resource "azurerm_storage_blob" "example" {
name = "sb123321123"
resource_group_name = azurerm_resource_group.example.name
account_key = azurerm_storage_account.eample.name
source = azurerm_storage_container.name
}
So as mentioned above, you're using a mix of Terraform and Az CLI here - this is not right. You should use one or the other.
It seems you've been tasked to create a Linux VM using Terraform. You need a 'main' Terraform file for your main code/terraform objects and then a 'variable' Terraform file for your variables. Technically, Terraform will flatten every Terraform file that are in the same directory when you do a Terraform init/plan - this means you could put everything in just one single .tf file.
However, for the purpose of the tutorial, it's a good practice to split both so they can be managed easily. This is the Terraform code that you would want to use - it will help you create a Linux VM.
For simplicity, I'm putting a sample here that links back to the variables in your variables.tf
terraform {
required_version = "= 0.14.10" //change this accordingly
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.55.0"
}
}
}
resource "azurerm_resource_group" "example" {
name = var.resourcegroup
location = var.location
}
resource "azurerm_virtual_network" "example" {
name = "example-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "example" {
name = "example-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "example" {
name = "example-machine"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_DS1_v2"
admin_username = "Admin_username"
network_interface_ids = [
azurerm_network_interface.example.id,
]
admin_ssh_key {
username = "adminuser"
public_key = file("~/.ssh/id_rsa.pub") //put your public key here
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
Once you're done with validating the code above, you'll need two more objects,
The azurerm_network_security_group that will help you create the rules to allow inbound connectivity to the VM
The azurerm_subnet_network_security_group_association that will help you attach the subnet to the NSG.
Once you have the complete code, then you can run a terraform init to initialise the modules, followed by terraform plan to verify your plan and finally terraform apply to deploy the VM.
For the blob part:
variable.tf
variable "resourcegroup" {
default = "user-pbtwiiiuofyu"
}
variable "location" {
default = "East US"
}
azmonitor.tf
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = var.resourcegroup
location = var.location
}
resource "azurerm_storage_account" "example" {
name = "examplestoracc"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "example" {
name = "content"
storage_account_name = azurerm_storage_account.example.name
container_access_type = "private"
}
resource "azurerm_storage_blob" "example" {
name = "my-awesome-content.zip"
storage_account_name = azurerm_storage_account.example.name
storage_container_name = azurerm_storage_container.example.name
type = "Block"
source = "./some-local-file.zip"
}
Doing Event Grid Subscription with a EventHub endpoint
resource "azurerm_eventgrid_system_topic_event_subscription" "example" {
name = "example-event-subscription"
system_topic = azurerm_system_topic.example.name
resource_group_name = azurerm_resource_group.example.name
eventhub_endpoint {
eventhub_endpoint_id = azurerm_eventhub.example.id
}
I got the error like
Blocks of type "eventhub_endpoint" are not expected here.
Not sure what I'm missing here. Is the eventhub_endpoint is not a valid one ? How can i configure the eventhub for my event grid sub ?
Regarding the issue, please update your script as
resource "azurerm_eventgrid_system_topic_event_subscription" "example" {
name = "example-event-subscription"
system_topic = azurerm_eventgrid_system_topic.example.name
resource_group_name = azurerm_resource_group.example.name
eventhub_endpoint_id = azurerm_eventhub.example.id
}
For more details, please refer to here.
For example (I use terraform 0.15.4 on windows)
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.46.0"
}
}
}
provider "azurerm" {
subscription_id = "e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68"
client_id = "42e0d080-b1f3-40cf-8db6-c4c522d988c4"
client_secret = "Gbx2eK64iqq_g_3NCA.ClJDfQpIjoae:"
tenant_id = "e4c9ab4e-bd27-40d5-8459-230ba2a757fb"
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-rg"
location = "West Europe"
}
resource "azurerm_eventhub_namespace" "example" {
name = "testhubname0123"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
sku = "Standard"
capacity = 1
tags = {
environment = "Production"
}
}
resource "azurerm_eventhub" "example" {
name = "testhub0123"
namespace_name = azurerm_eventhub_namespace.example.name
resource_group_name = azurerm_resource_group.example.name
partition_count = 2
message_retention = 1
}
resource "azurerm_eventgrid_system_topic" "example" {
name = "example-system-topic"
location = "Global"
resource_group_name = azurerm_resource_group.example.name
source_arm_resource_id = azurerm_resource_group.example.id
topic_type = "Microsoft.Resources.ResourceGroups"
}
resource "azurerm_eventgrid_system_topic_event_subscription" "example" {
name = "example-event-subscription"
system_topic = azurerm_eventgrid_system_topic.example.name
resource_group_name = azurerm_resource_group.example.name
eventhub_endpoint_id = azurerm_eventhub.example.id
}