Blueprints

Getting started with Kestra — an Infrastructure Automation workflow example

Source

yaml
id: infrastructure-automation
namespace: tutorial
labels:
  name: Infrastructure Automation

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

tasks:
  - id: build_image
    type: io.kestra.plugin.docker.Build
    description: Build a Docker image with Kestra installed.
    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
    description: Run the freshly built image and inspect the Kestra package.
    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
    description: Plan and apply Terraform to write a Pokémon details file.
    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.plugin.core.log.Log
    description: Log the Pokémon details produced by Terraform.
    message: "{{ read(outputs.run_terraform.outputFiles['pokemon.txt']) }}"

description: |
  # Flow Description
  **Use case:** Infrastructure automation that packages Kestra, validates the image, and provisions files with Terraform.
  **Highlights:**
  - Build a Docker image (tag controlled by input) with Kestra installed; registry credentials are read from secrets if pushing.
  - Run the freshly built image locally (no pull) and inspect the installed Kestra package.
  - Apply a Terraform plan that fetches Pokémon data and writes a formatted text file with names and types.
  - Log the generated Pokémon details from the Terraform output to verify the automation end-to-end.

About this blueprint

Getting Started API DevOps

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 with credentials as secrets.
  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.

Build

Run

Terraform CLI

Log

More Related Blueprints

New to Kestra?

Use blueprints to kickstart your first workflows.

Get started with Kestra