Terraform guide 5 - State

Terraform: State

Terraform formatting and remote state

  • version it using a S3 bucket.
  • Create an S3 Bucket
    1. Search for S3 in Find Services -> Create Bucket
      • Enter an unique Bucket name
      • Choose region (e.g. US East (N. Virginia)) -> Next, next…
    2. On Review page, ‘Create bucket’
      • Add the Terraform Folder to the Bucket
      • Create terraform-aws folder on the bucket and save
  • Add Backend to Scripts
    1. Setup from the Docker Swarm Manager
      1
      2
      3
      4
      5
      cd ~/terraform/AWS
      # set environment vars
      export AWS_ACCESS_KEY_ID="[ACCESS_KEY]"
      export AWS_SECRET_ACCESS_KEY="[SECRET_KEY]]"
      export AWS_DEFAULT_REGION="us-east-1"
    2. Create terraform.tf
      1
      2
      3
      4
      5
      terraform {
      backend "s3" {
      key = "terraform-aws/terraform.tfstate"
      }
      }
    3. Work with Terraform
      1
      2
      3
      4
      5
      terraform init -backend-config "bucket=[BUCKET_NAME]"
      terraform validate
      terraform plan
      terraform apply -auto-approve
      terraform destroy -auto-approve

Using Remote State with Jenkins

  • update CI/CD process to use remote state with our Jenkins Pipelines: 2 separate Pipelines: deployInfrastructure and destroyInfrastructure
  • Create S3 Bucket
    1. Search for S3 in Find Services -> ‘Create Bucket’
      • Enter an unique Bucket name
      • Choose region (e.g. US East (N. Virginia)) -> Next, next…
    2. On Review page, ‘Create bucket’
      • Add the Terraform Folder to the Bucket
      • Create terraform-aws folder on the bucket and save
    3. Create the Jenkins DeployInfrastructure job
      • Item name = “DeployDockerService”, Pipeline,
      • ‘Add Parameter’ -> String Parameter
        • Name = “access_key_id”
        • Default Value = “Access Key Id”
      • ‘Add Parameter’ -> String Parameter
        • Name = “secret_access_key”
        • Default Value = “Secret Access Key”
      • ‘Add Parameter’ -> String Parameter
        • Name = “bucket_name”
        • Default Value = “S3 Bucket”
      • ‘Add Parameter’ -> Choice Parameter
        • Name = “image_name”
        • Choices = “ghost:latest” and “ghost:alpine” (make sure they are on separate lines)
      • ‘Add Parameter’ -> String Parameter
        • Name = “ghost_ext_port”
        • Default Value = 80
      • In Pipeline section -> add to Script:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        env.AWS_ACCESS_KEY_ID = "${access_key_id}"
        env.AWS_SECRET_ACCESS_KEY = "${secret_access_key}"
        env.AWS_DEFAULT_REGION = 'us-east-1'

        node {
        git (
        url: 'https://github.com/linuxacademy/content-terraform-docker-service.git',
        branch: 'remote-state'
        )
        stage('init') {
        sh label: 'terraform init', script: "terraform init -backend-config \"bucket=${bucket_name}\""
        }
        stage('plan') {
        sh label: 'terraform plan', script: "terraform plan -out=tfplan -input=false -var image_name=${image_name} -var ghost_ext_port=${ghost_ext_port}"
        }
        stage('apply') {
        sh label: 'terraform apply', script: "terraform apply -lock=false -input=false tfplan"
        }
        }
    4. Create the Jenkins DestroyInfrastructure job
      • Item name = “DestroyDockerService”, Pipeline,
      • ‘Copy from’ = “DeployDockerService”, Ok.
      • In Pipeline section -> edit Script to this:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        env.AWS_ACCESS_KEY_ID = "${access_key_id}"
        env.AWS_SECRET_ACCESS_KEY = "${secret_access_key}"
        env.AWS_DEFAULT_REGION = 'us-east-1'

        node {
        git (
        url: 'https://github.com/linuxacademy/content-terraform-docker-service.git',
        branch: 'remote-state'
        )
        stage('init') {
        sh label: 'terraform init', script: "terraform init -backend-config \"bucket=${bucket_name}\""
        }
        stage('plan_destroy') {
        sh label: 'terraform plan', script: "terraform plan -destroy -out=tfdestroyplan -input=false -var image_name=${image_name} -var ghost_ext_port=${ghost_ext_port}"
        }
        stage('destroy') {
        sh label: 'terraform apply', script: "terraform apply -lock=false -input=false tfdestroyplan"
        }
        }
    5. Once Jenkins is running, check it
      1
      2
      docker container ls
      docker exec -it 73575a9ee4ac /bin/bash