# Logtail Provider

> Manage Logtail log management resources with Pulumi

The Logtail provider enables you to manage log sources, metrics, and analytics resources in Logtail using Pulumi. This provider is dynamically bridged from the [Terraform Logtail Provider](https://registry.terraform.io/providers/BetterStackHQ/logtail).

## Installation

Install the Logtail provider package using your preferred package manager:

<Tabs>
  <Tab title="bun">
    ```bash
    bun add pulumi-logtail
    ```
  </Tab>
  <Tab title="pnpm">
    ```bash
    pnpm add pulumi-logtail
    ```
  </Tab>
  <Tab title="yarn">
    ```bash
    yarn add pulumi-logtail
    ```
  </Tab>
  <Tab title="npm">
    ```bash
    npm install pulumi-logtail
    ```
  </Tab>
</Tabs>

## Configuration

### Getting API Token

1. Log in to Logtail at [logs.betterstack.com](https://logs.betterstack.com)
2. Navigate to Settings → API Tokens
3. Create a new API token
4. Copy the token value

### Provider Setup

```bash
pulumi config set logtail:apiToken YOUR_API_TOKEN --secret
```

Or using environment variables:

```bash
export LOGTAIL_API_TOKEN="your-api-token"
```

## Quick Start

```typescript
import * as pulumi from "@pulumi/pulumi";
import * as logtail from "pulumi-logtail";

// Create a log source
const source = new logtail.Source("app-logs", {
    name: "Application Logs",
    platform: "docker",
});

export const sourceToken = source.token;
```

## Key Features

### Log Sources

```typescript
const dockerSource = new logtail.Source("docker", {
    name: "Docker Containers",
    platform: "docker",
});

const syslogSource = new logtail.Source("syslog", {
    name: "System Logs",
    platform: "syslog",
});
```

### Linking an AWS Account

For `aws` platform sources, attach the AWS account that Better Stack pulls logs
from with `logtail.SourceAwsAccount` (added in 10.14.0). Connect a new account
using the Better Stack CloudFormation stack outputs, or reuse one you've already
connected via `awsAccountId`:

```typescript
const awsSource = new logtail.Source("aws-logs", {
    name: "AWS Logs",
    platform: "aws",
});

new logtail.SourceAwsAccount("aws-account", {
    sourceId: awsSource.id,
    awsRoleArn: "arn:aws:iam::123456789012:role/BetterStackIntegration",
    awsExternalId: "cf-stack-external-id",
});
```

### Collectors and Targets

Collectors are agents that run inside your infrastructure to collect metrics from databases and processes. Use `logtail.CollectorTarget` (added in 10.11.3) to attach scrape targets to a collector:

```typescript
const collector = new logtail.Collector("infra-collector", {
    name: "Infra Collector",
    dataRegion: "eu",
});

new logtail.CollectorTarget("postgres-primary", {
    collectorId: collector.id,
    kind: "postgres",
    host: "db.internal",
    port: 5432,
    username: "metrics",
    password: "secret",
});

new logtail.CollectorTarget("nginx-edge", {
    collectorId: collector.id,
    kind: "nginx",
    collectorHost: "edge-01.internal",
    endpoint: "http://127.0.0.1/nginx_status",
});
```

### Dashboards

```typescript
const dashboard = new logtail.Dashboard("errors-overview", {
    name: "Errors Overview",
});
```
