AWS role setup with Terraform
Access and integrationCreate 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.
- Authenticate Terraform to your AWS account and save the configuration below
as
main.tf. - Set
customer_regionandbucket_name. Optionally setartifact_prefix,role_name, and a customer-specificexternal_id. If you set an external ID, pass the same value toaws_assume_role. - Run
terraform init, reviewterraform plan, and apply the plan. - Set
aws.role_arnin your Factory organization configuration to the output ofterraform output -raw role_arn. If you set an external ID, also setaws.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 will appear here. Create one and open it beside this guide.