GithubHelp home page GithubHelp logo

ehrnjic / az_tf_autom_task Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 4 KB

Example how to use Terraform for IaC on Azure and Azure Pipeline to automate app integration and deployment.

License: MIT License

HCL 100.00%

az_tf_autom_task's Introduction

Teraforming AzureCloud Infrastructure

This code is "terraforming" infrastructure on Azure Cloud service to run simple dotnet web application with database (https://github.com/ehrnjic/dotnetcore-sqldb-web-app). I wrote the whole terraform code in one main.tf file, but it can be divided into separate files (variables, resources, outputs, etc.) when we have more complex implementations.

Prerequisites:

  • Azure Subscription (free account)

  • Azure DevOps Organization (basic plan)

  • Terraform v0.13.5

  • Azure CLI v2.14.0

  • .NET

RUN Terraform Code

To run this code clone this repo into your local. First run terraform init to initialize terraform. If you want to see the changes that terraform will make, run the terraform plan command. And finally, if you are satisfied with the terraform plan, run the terraform apply command that will start creating the infrastructure on your azure subscription. Confirm the execution by typing yes after the question "Do you want to perform these actions?"

After executing this code, the following new resources will be created on your Azure subscription:

  • Resource Group = rg-ehr-test
  • SQL Server = sql-ehr-test-01 and firewall rule to allow access to server
  • Database = db-ehr-test-01
  • Azure Container registry = crehrtest
  • App Service plan = asp-ehr-test
  • WebApp Service for containers = appehrtest

Data migrations

In order to auto apply migrations on startup, I've modified Startup.cs class to initiate process of auto migrations. One thing to note is that migrations on repo seem to target some other sql engine and produce error when ran against MS SQL server. To fix the issue I deleted existing Migrations folder and generated new one. After this, migrations started passing against MS SQL server.

CI/CD Pipeline

In my organization on Azure DevOps, I created public pipeline https://dev.azure.com/ehrnjic-org/devops-task to automate the build and deployment process. Build pipeline is configured to use my github repo https://github.com/ehrnjic/dotnetcore-sqldb-web-app as SCM, Build Docker container with app, tag container with release number and tag "latest", and push that container to Azure Container Registry crehrtest. Build pipeline will be triggered when commit a new release in the master branch or manually through the Azure DevOps portal. Application deployment is triggered by webhook.

Pipeline YAML configuration file (azure-pipelines.yml):

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'dd01c30b-08f3-469b-af18-39f72fdd8e72'
  imageRepository: 'ehrnjicdotnetcoresqldbwebapp'
  containerRegistry: 'crehrtest.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'
  
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
          latest

Access to application

Few minutes after triggering pipeline you can check if the application is up&running on this URL https://appehrtest.azurewebsites.net/. Be patient, this is free tier :-)

az_tf_autom_task's People

Contributors

ehrnjic avatar

Stargazers

 avatar

Watchers

 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.