dotFactory
Documentation / Access and integration

AWS role setup with Terraform

Access and integration

Create an IAM role that lets Factory write to an S3 prefix.

This example creates a role that trusts Factory and permits s3:PutObject on a prefix of an existing S3 bucket. Adjust the permissions to your workflow. It does not create or modify the bucket.

  1. Authenticate Terraform to your AWS account and save the configuration below as main.tf.
  2. Set customer_region and bucket_name. Optionally set artifact_prefix, role_name, and a customer-specific external_id. If you set an external ID, pass the same value to aws_assume_role.
  3. Run terraform init, review terraform plan, and apply the plan.
  4. Set aws.role_arn in your Factory organization configuration to the output of terraform output -raw role_arn. If you set an external ID, also set aws.external_id.

This example does not grant reads, listing, deletion or KMS operations. Bucket policies, service control policies and permissions boundaries can restrict it further. Use an SSE-S3 bucket or provide the required KMS permissions.

terraform {
  required_version = ">= 1.5.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

variable "customer_region" {
  type = string
}

provider "aws" {
  region = var.customer_region
}

variable "role_name" {
  type    = string
  default = "FactoryArtifacts"
}

variable "bucket_name" {
  description = "Existing customer-owned S3 bucket in customer_region."
  type        = string
}

variable "artifact_prefix" {
  description = "Literal object-key prefix, including its trailing slash."
  type        = string
  default     = "factory-artifacts/"
  validation {
    condition     = can(regex("^[A-Za-z0-9][A-Za-z0-9/_-]*/$", var.artifact_prefix))
    error_message = "Use a nonempty literal prefix ending in /; IAM wildcards are not allowed."
  }
}

variable "external_id" {
  description = "Optional external ID required on every AssumeRole call."
  type        = string
  default     = null
  validation {
    condition     = var.external_id == null ? true : can(regex("^[A-Za-z0-9_+=,.@:/-]{2,1224}$", var.external_id))
    error_message = "Use 2–1224 characters permitted by STS ExternalId, or null."
  }
}

data "aws_partition" "current" {}

resource "aws_iam_role" "factory" {
  name                 = var.role_name
  max_session_duration = 3600
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [merge({
      Effect    = "Allow"
      Principal = { AWS = "arn:aws:iam::860892315863:user/dotFactory-customer-role-assumer" }
      Action    = "sts:AssumeRole"
      }, var.external_id == null ? {} : {
      Condition = {
        StringEquals = { "sts:ExternalId" = var.external_id }
      }
    })]
  })
}

resource "aws_iam_role_policy" "artifacts" {
  role = aws_iam_role.factory.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = ["s3:PutObject"]
      Resource = "arn:${data.aws_partition.current.partition}:s3:::${var.bucket_name}/${var.artifact_prefix}*"
    }]
  })
}

output "role_arn" {
  value = aws_iam_role.factory.arn
}

output "external_id" {
  value = var.external_id
}

To remove the example resources, run terraform destroy. Already-issued sessions can survive trust removal until expiration. See AWS role assumption for how to use the role in a factory.

Your factories

Your factories will appear here. Create one and open it beside this guide.