Blueprints

Getting started with Kestra — an Infrastructure Automation workflow example

About this blueprint

API Docker Getting Started

This flow is a simple example of an infrastructure automation use case. It builds a Docker image, runs a container, and uses Terraform to create a file with details about Pokémon. The flow has four tasks:

  1. The first task builds a Docker image.
  2. The second task runs a container using the image.
  3. The third task uses Terraform to create a file with details about Pokémon.
  4. The fourth task logs the details about Pokémon.
yaml
id: infrastructure_automation
namespace: tutorial

inputs:
  - id: docker_image
    type: STRING
    defaults: kestra/myimage:latest

tasks:
  - id: build_image
    type: io.kestra.plugin.docker.Build
    dockerfile: |
      FROM python:3.11-alpine
      RUN pip install --no-cache-dir kestra
    tags:
      - "{{ inputs.docker_image }}"
    push: false # you can change this to true after adding credentials
    credentials:
      registry: https://index.docker.io/v1/
      username: "{{ secret('DOCKERHUB_USERNAME') }}"
      password: "{{ secret('DOCKERHUB_PASSWORD') }}"

  - id: run_container
    type: io.kestra.plugin.docker.Run
    pullPolicy: NEVER # to use the local image we've just built
    containerImage: "{{ inputs.docker_image }}"
    commands:
      - pip
      - show
      - kestra

  - id: run_terraform
    type: io.kestra.plugin.terraform.cli.TerraformCLI
    beforeCommands:
      - terraform init
    commands:
      - terraform plan 2>&1 | tee plan_output.txt
      - terraform apply -auto-approve 2>&1 | tee apply_output.txt
    outputFiles:
      - "*.txt"
    inputFiles:
      main.tf: |
        terraform {
          required_providers {
            http = {
              source = "hashicorp/http"
            }
            local = {
              source = "hashicorp/local"
            }
          }
        }

        provider "http" {}
        provider "local" {}

        variable "pokemon_names" {
          type    = list(string)
          default = ["pikachu", "psyduck", "charmander", "bulbasaur"]
        }

        data "http" "pokemon" {
          count = length(var.pokemon_names)
          url   = "https://pokeapi.co/api/v2/pokemon/${var.pokemon_names[count.index]}"
        }

        locals {
          pokemon_details = [for i in range(length(var.pokemon_names)) : {
            name  = jsondecode(data.http.pokemon[i].response_body)["name"]
            types = join(", ", [for type in jsondecode(data.http.pokemon[i].response_body)["types"] : type["type"]["name"]])
          }]

          file_content = join("\n\n", [for detail in local.pokemon_details : "Name: ${detail.name}\nTypes: ${detail.types}"])
        }

        resource "local_file" "pokemon_details_file" {
          filename = "${path.module}/pokemon.txt"
          content  = local.file_content
        }

        output "file_path" {
          value = local_file.pokemon_details_file.filename
        }

  - id: log_pokemon
    type: io.kestra.core.tasks.log.Log
    message: "{{ read(outputs.run_terraform.outputFiles['pokemon.txt']) }}"

Build

Run

Terraform CLI

Log

More Related Blueprints

New to Kestra?

Use blueprints to kickstart your first workflows.

Get started with Kestra