Integrate terraform and az-cli

Aim: install az-cli and terraform to deploy resources

I tried az-cli install to my rhel system with below command but it was giving hell out of error so tried other way

run this command on terminal  with sudo acccess
       curl -L https://aka.ms/InstallAzureCli | bash

it start installing some of az modules but in last gives below error

ERROR: Could not find a version that satisfies the requirement mock~=4.0 (from azure-cli) (from versions: 0.5.0, 0.6.0, 0.7.0b1, 0.7.0b2, 0.7.0b3, 0.7.0b4, 0.7.0rc1, 0.7.0, 0.7.1, 0.7.2, 0.8.0, 1.0b1, 1.0.0, 1.0.1, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.2.0, 1.3.0, 2.0.0, 3.0.0, 3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.5)
ERROR: No matching distribution found for mock~=4.0 (from azure-cli)
Traceback (most recent call last):
  File "/tmp/azure_cli_install_tmp_7nrBN8", line 420, in <module>
    main()
  File "/tmp/azure_cli_install_tmp_7nrBN8", line 405, in main
    install_cli(install_dir, tmp_dir)
  File "/tmp/azure_cli_install_tmp_7nrBN8", line 151, in install_cli
    exec_command(cmd)
  File "/tmp/azure_cli_install_tmp_7nrBN8", line 106, in exec_command
    subprocess.check_call(command_list, cwd=cwd, env=env)
  File "/usr/lib64/python2.7/subprocess.py", line 542, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/home/userprofile/folder/azure-cli/bin/pip', 'install', '--cache-dir', '/tmp/tmpAlRiJK', 'azure-cli', '--upgrade']' returned non-zero exit status 1

Then i switched to other method, which i felt quite easy and below is the steps

run command from terminal
    sudo yum install azure-cli
output on screen will be
Loaded plugins: downloadkvmonly-background, ibm-check-lotus-updates, ibm-check-upgrade, ibm-check-xorg-updates, ibm-repository, langpacks, post-transaction-actions, refresh-packagekit, versionlock
Cannot reach IBM Intranet network. Please ensure you have an active IBM connection.
azure-cli                                                                                                                                                                               | 3.0 kB  00:00:00    
code                                                                                                                                                                                    | 3.0 kB  00:00:00    
teams                                                                                                                                                                                   | 3.0 kB  00:00:00    
(1/2): azure-cli/primary_db                                                                                                                                                             |  53 kB  00:00:00    
(2/2): code/primary_db                                                                                                                                                                  | 282 kB  00:00:00    
Resolving Dependencies
--> Running transaction check
---> Package azure-cli.x86_64 0:2.5.1-1.el7 will be installed
--> Processing Dependency: python3 for package: azure-cli-2.5.1-1.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: azure-cli-2.5.1-1.el7.x86_64
--> Finished Dependency Resolution
Error: Package: azure-cli-2.5.1-1.el7.x86_64 (azure-cli)
           Requires: python3
Error: Package: azure-cli-2.5.1-1.el7.x86_64 (azure-cli)
           Requires: libpython3.6m.so.1.0()(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
[piyushsr@oc1410205881 ~]$ sudo rpm -ivh --nodeps azure-cli-*.rpm
error: File not found by glob: azure-cli-*.rpm

Then i saw one of dependecy is failed so installed manully like
     sudo yum install libpython3.6m.so.1.0

and this will install most of dependency and after all that you run
  sudo yum install azure-cli
 it will get install az-cli sucesfully. You can test by running az login command there.

Pythos is playing important role in az cli module, so if any warning to python you need to fix that.


Now will install terraform
The best part of terraform that everything is available on there site it self so you dont need to do much troubleshooting until you destroyed everything.
Official Site:  https://www.terraform.io/docs

I found very helpful there site and installation. Package is easily available on download link and better to review requirement also from Link
After downloading package extract to one path and you will see there only one fexecutable file with name of terraform.
Now to integrate terraform with azure you have to collect some of information from your azure account
1. subscribtion ID
2. tenant ID
3. user name

and save that file with name of provider.tf in path where terraform executable you extratcted
provider "azurerm" {
  # Whilst version is optional, we /strongly recommend/ using it to pin the version of the Provider being used
  version = "=1.44.0"

  subscription_id = "123456789012345656788"
  tenant_id       = "abcdefg123298q73r-e8131371373"
}

after that open terminal and cd to terraform folder and see the output running by terraform command available
===================================================
[piyushsr@Terraform]$ terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management
=====================================================
There are bunch of scripts format already available on terraform site
which will give you better idea to plan deploy and destroy resource in azure subscription. I prefer to have resources with main.tf you can do another way also but make a standard helps to understand in break fix time.

[piyushsr@erraform]$ terraform init

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Then

[piyushsr@Terraform]$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.rg will be created
  + resource "azurerm_resource_group" "rg" {
      + id       = (known after apply)
      + location = "northeurope"
      + name     = "my-first-terraform-rg"
      + tags     = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

and once you verified all the configuration parameter you can say terraform apply and in duration of 2-3 min resource will be created on azure.




No comments:

Post a Comment