Coder Social home page Coder Social logo

gnsaddy / terraform_iac Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 26 KB

Infrastructure management with Terraform

License: MIT License

HCL 88.72% Shell 11.28%
aws azure iac iac-terraform infrastructure-as-code infrastructure-management infrastructure-services terraform tfsec

terraform_iac's Introduction

Infrastructure management with Terraform

This repository contains code for AWS and Azure cloud.

AWS


Prerequisites for AWS

  • AWS account
  • AWS CLI
  • Terraform
  • IAM user with programmatic access
  • AWS credentials and region

Steps for configuration of AWS CLI

  1. Create a new IAM user with programmatic access and attach the following policies to the user:

    1. AmazonEC2FullAccess
    2. AmazonS3FullAccess (For S3 bucket creation)
  2. Generate the access key and secret key for the user and save it in a secure location.

  3. Configure the AWS CLI with the access key and secret key.

  4. Create a new S3 bucket and enable versioning. S3 bucket can be used to store the terraform state file.

  5. Backup the terraform state file in the S3 bucket.

    1. Create a new file named backend.tf and add the following code:

      terraform {
          backend "s3" {
              bucket = "s3-bucket-name"
              key    = "s3-bucket-key"
              region = "s3-bucket-region"
          }
      }
    2. This will create a new file named terraform.tfstate in the S3 bucket. This file will be used to store the terraform state file.

Steps for configuration of Terraform for AWS

  1. Create a new file named terraform.tfvars and add the following variables:
    1. aws_access_key - AWS access key
    2. aws_secret_key - AWS secret key
    3. aws_region - AWS region
    4. s3_bucket_name - S3 bucket name
    5. s3_bucket_key - S3 bucket key
    6. s3_bucket_region - S3 bucket region
  2. Run the following commands:
    1. terraform init - To initialize the working directory
    2. terraform refresh - To update the state file against real resources
    3. terraform fmt - To rewrite Terraform configuration files to a canonical format and style
    4. terraform validate - To validate the configuration files
    5. terraform plan - To create an execution plan
    6. terraform apply - To apply the changes required to reach the desired state of the configuration
    7. terraform destroy - To destroy the Terraform-managed infrastructure

Azure

Prerequisites for Azure

  • Azure account
  • Azure CLI
  • Terraform
  • Azure credentials and region
  • Azure subscription ID
  • Azure tenant ID
  • Azure client ID
  • Azure client secret

Steps for configuration of Azure CLI

  1. Create a new service principal with the following command:

    az ad sp create-for-rbac --name "terraform" --role="Contributor" --scopes="/subscriptions/<subscription_id>"
  2. Copy the appId, password, tenant and save it in a secure location.

  3. Configure the Azure CLI with the appId, password, tenant and subscriptionId.

  4. Create a new resource group and storage account. Storage account can be used to store the terraform state file.

  5. Backup the terraform state file in the storage account.

    1. Create a new file named backend.tf and add the following code:

      terraform {
          backend "azurerm" {
              resource_group_name  = "resource-group-name"
              storage_account_name = "storage-account-name"
              container_name       = "container-name"
              key                  = "storage-account-key"
          }
      }
    2. This will create a new file named terraform.tfstate in the storage account. This file will be used to store the terraform state file.

    3. Enable versioning for the storage account.

Steps for configuration of Terraform for Azure

  1. Create a new file named terraform.tfvars and add the following variables:
    1. azure_subscription_id - Azure subscription ID
    2. azure_tenant_id - Azure tenant ID
    3. azure_client_id - Azure client ID
    4. azure_client_secret - Azure client secret
    5. azure_region - Azure region
    6. storage_account_name - Storage account name
    7. storage_account_key - Storage account key
    8. resource_group_name - Resource group name
    9. container_name - Container name
  2. Run the following commands:
    1. terraform init - To initialize the working directory
    2. terraform refresh - To update the state file against real resources
    3. terraform fmt - To rewrite Terraform configuration files to a canonical format and style
    4. terraform validate - To validate the configuration files
    5. terraform plan - To create an execution plan
    6. terraform apply - To apply the changes required to reach the desired state of the configuration
    7. terraform destroy - To destroy the Terraform-managed infrastructure

Other useful commands

  1. terraform show - To show the current state of the infrastructure
  2. terraform import - To import existing infrastructure into Terraform
  3. terraform output - To show the output values from the root module
  4. terraform console - To open an interactive console for Terraform interpolations
  5. terraform graph - To create a visual graph of Terraform resources

References

  1. Terraform
  2. Terraform - Getting Started
  3. Terraform - AWS Provider
  4. Terraform - Azure Provider
  5. Terraform - Azure CLI
  6. Terraform - Azure CLI - Create Service Principal
  7. Terraform - Azure CLI - Configure
  8. Terraform - AWS CLI
  9. Terraform - AWS CLI - Configure
  10. Youtube- Terraform Tutorial for Beginners by FreeCodeCamp.org on YouTube (Video)
  11. YouTube - Terraform AWS by freeCodeCamp.org on YouTube (Video)
  12. YouTube - Terraform Azure Playlist by Bee a Learner on YouTube (Video)

Author

Aditya RajLinkedIn

License

MIT License: MIT

How to contribute

  • Fork the repository
  • Clone the forked repository to your local machine
  • Create a new branch using git checkout -b
  • Make changes to the code
  • Commit the changes using git commit -m ""
  • Push the changes to your forked repository using git push origin
  • Create a pull request
  • Wait for your pull request to be reviewed and merged
  • Star this repository
  • Celebrate - you've just contributed to open source!

terraform_iac's People

Contributors

gnsaddy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.