Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(managed-migrations): IAM role auth option in the migration form (#69077)

Nick Best246d25758e95
diff --git a/products/managed_migrations/frontend/ManagedMigration.tsx b/products/managed_migrations/frontend/ManagedMigration.tsx
index 6ac4b6fc6b82..ebe1d4fe262c 100644
--- a/products/managed_migrations/frontend/ManagedMigration.tsx
+++ b/products/managed_migrations/frontend/ManagedMigration.tsx
@@ -4,6 +4,7 @@ import { Form } from 'kea-forms'
import { IconSort } from '@posthog/icons'
import { LemonButton, LemonTable, LemonTag, Link } from '@posthog/lemon-ui'
+import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
import { FlaggedFeature } from 'lib/components/FlaggedFeature'
import { TZLabel } from 'lib/components/TZLabel'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -13,6 +14,7 @@ import { LemonCheckbox } from 'lib/lemon-ui/LemonCheckbox'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { LemonProgress } from 'lib/lemon-ui/LemonProgress'
+import { LemonSegmentedButton } from 'lib/lemon-ui/LemonSegmentedButton'
import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
import { ProfilePicture } from 'lib/lemon-ui/ProfilePicture'
import { sceneConfigurations } from 'scenes/scenes'
@@ -78,10 +80,70 @@ function AmplitudeImportOptions({
)
}
+function IamRoleSetupInstructions({
+ managedMigration,
+}: {
+ managedMigration: ManagedMigrationForm
+}): JSX.Element | null {
+ const { awsIamSetup } = useValues(managedMigrationLogic)
+
+ if (!awsIamSetup?.available) {
+ return null
+ }
+
+ const bucket = managedMigration.s3_bucket
+ const prefix = managedMigration.s3_prefix
+ const permissionPolicy = awsIamSetup.permission_policy_template
+ .replaceAll('YOUR_BUCKET', bucket || '<YOUR_BUCKET>')
+ .replaceAll('YOUR_PREFIX', prefix || '')
+
+ return (
+ <div className="border rounded p-4 space-y-3 bg-surface-secondary">
+ <div className="font-semibold">Set up an IAM role for PostHog</div>
+ <ol className="list-decimal list-inside space-y-3 text-sm">
+ <li>
+ In the AWS console, create an IAM role with the following trust policy (it lets PostHog's import
+ role read from your bucket, and no one else):
+ <CodeSnippet language={Language.JSON} compact>
+ {awsIamSetup.trust_policy}
+ </CodeSnippet>
+ <div className="text-muted mt-1">
+ The external ID <code>{awsIamSetup.external_id}</code> is unique to this project and must appear
+ in the trust policy exactly as shown.
+ </div>
+ </li>
+ <li>
+ Attach this permission policy to the role. It fills in automatically from the bucket and prefix
+ entered above:
+ <CodeSnippet language={Language.JSON} compact>
+ {permissionPolicy}
+ </CodeSnippet>
+ <div className="text-muted mt-1">
+ {!bucket
+ ? 'Enter your bucket above to replace <YOUR_BUCKET> in the policy.'
+ : prefix
+ ? `This grants read access to objects under ${prefix} only - nothing else in the bucket.`
+ : 'No prefix is set, so this grants read access to the whole bucket. Set a prefix above to narrow what PostHog can read.'}
+ </div>
+ </li>
+ <li>
+ Paste the new role's ARN:
+ <LemonField name="role_arn" className="mt-1">
+ <LemonInput placeholder="arn:aws:iam::123456789012:role/posthog-import" />
+ </LemonField>
+ </li>
+ </ol>
+ </div>
+ )
+}
+
export function ManagedMigration(): JSX.Element {
- const { managedMigration, isManagedMigrationSubmitting } = useValues(managedMigrationLogic)
+ const { managedMigration, isManagedMigrationSubmitting, awsIamSetup } = useValues(managedMigrationLogic)
const { setManagedMigrationValue } = useActions(managedMigrationLogic)
+ const isS3Source = managedMigration.source_type === 's3' || managedMigration.source_type === 's3_gzip'
+ const usesIamRole = isS3Source && managedMigration.s3_auth_method === 'iam_role' && !!awsIamSetup?.available
+
return (
<Form logic={managedMigrationLogic} formKey="managedMigration" enableFormOnSubmit className="space-y-4">
<SceneContent>
@@ -171,23 +233,57 @@ export function ManagedMigration(): JSX.Element {
</LemonField>
</div>
- <LemonField name="s3_prefix" label="S3 Prefix (optional)">
- <LemonInput placeholder="path/to/files/" />
- </LemonField>
-
<LemonField
- name="endpoint_url"
- label="Endpoint URL"
- showOptional
- info={
+ name="s3_prefix"
+ label="S3 Prefix (optional)"
+ help={
<>
- Only required for S3-compatible storage like Cloudflare R2 or MinIO. For R2, use
- https://ACCOUNT_ID.r2.cloudflarestorage.com and set region to "auto".
+ Matched as a plain string prefix: <code>exports</code> also matches keys under{' '}
+ <code>exports-old/</code>. End with <code>/</code> to match a single folder.
+ {usesIamRole && (
+ <>
+ {' '}
+ Must exactly match the prefix in your IAM role's permission policy,
+ including any trailing slash.
+ </>
+ )}
</>
}
>
- <LemonInput placeholder="https://ACCOUNT_ID.r2.cloudflarestorage.com" />
+ <LemonInput placeholder="path/to/files/" />
</LemonField>
+
+ {awsIamSetup?.available && (
+ <LemonField name="s3_auth_method" label="Authentication">
+ <LemonSegmentedButton
+ value={managedMigration.s3_auth_method}
+ onChange={(value) => setManagedMigrationValue('s3_auth_method', value)}
+ options={[
+ { value: 'iam_role', label: 'IAM role (recommended)' },
+ { value: 'access_keys', label: 'Access keys' },
+ ]}
+ size="small"
+ />
+ </LemonField>
+ )}
+
+ {usesIamRole ? (
+ <IamRoleSetupInstructions managedMigration={managedMigration} />
+ ) : (
+ <LemonField
+ name="endpoint_url"
+ label="Endpoint URL"
+ showOptional
+ info={
+ <>
+ Only required for S3-compatible storage like Cloudflare R2 or MinIO. For R2, use
+ https://ACCOUNT_ID.r2.cloudflarestorage.com and set region to "auto".
+ </>
+ }
+ >
+ <LemonInput placeholder="https://ACCOUNT_ID.r2.cloudflarestorage.com" />
+ </LemonField>
+ )}
</>
)}
{(managedMigration.source_type === 'mixpanel' || managedMigration.source_type === 'amplitude') && (
@@ -239,7 +335,7 @@ export function ManagedMigration(): JSX.Element {
/>
)}
- {managedMigration.source_type === 'mixpanel' ? (
+ {usesIamRole ? null : managedMigration.source_type === 'mixpanel' ? (
<LemonField
name="secret_key"
label="Project secret"
diff --git a/products/managed_migrations/frontend/managedMigrationLogic.ts b/products/managed_migrations/frontend/managedMigrationLogic.ts
index 396a540a7a01..090a85ad7209 100644
--- a/products/managed_migrations/frontend/managedMigrationLogic.ts
+++ b/products/managed_migrations/frontend/managedMigrationLogic.ts
@@ -24,16 +24,24 @@ import { urls } from 'scenes/urls'
import { FileSystemIconType } from '~/queries/schema/schema-general'
import { Breadcrumb, ProjectTreeRef } from '~/types'
-import { managedMigrationsPromoteCreate, managedMigrationsTrialRecordsRetrieve } from './generated/api'
-import type { TrialRecordsResponseApi } from './generated/api.schemas'
+import {
+ managedMigrationsAwsIamSetupRetrieve,
+ managedMigrationsPromoteCreate,
+ managedMigrationsTrialRecordsRetrieve,
+} from './generated/api'
+import type { BatchImportAWSIAMSetupApi, TrialRecordsResponseApi } from './generated/api.schemas'
import { ManagedMigration } from './types'
+export const S3_ROLE_ARN_REGEX = /^arn:aws:iam::\d{12}:role\/.+$/
+
export interface ManagedMigrationForm {
source_type: 's3' | 's3_gzip' | 'mixpanel' | 'amplitude'
- access_key: string
- secret_key: string
+ access_key?: string
+ secret_key?: string
content_type: 'captured' | 'mixpanel' | 'amplitude'
// s3 specific fields
+ s3_auth_method?: 'iam_role' | 'access_keys'
+ role_arn?: string
s3_region?: string
s3_bucket?: string
s3_prefix?: string
@@ -59,6 +67,10 @@ const NEW_MANAGED_MIGRATION: ManagedMigrationForm = {
source_type: 's3',
access_key: '',
secret_key: '',
+ // Promoted to 'iam_role' once aws_iam_setup confirms role auth is available, so the
+ // validator (which only sees form values) can never demand a role_arn the UI is hiding
+ s3_auth_method: 'access_keys',
+ role_arn: '',
s3_region: '',
s3_bucket: '',
s3_prefix: '',
@@ -76,6 +88,8 @@ const NEW_MANAGED_MIGRATION: ManagedMigrationForm = {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface managedMigrationLogicValues {
+ awsIamSetup: BatchImportAWSIAMSetupApi | null
+ awsIamSetupLoading: boolean
breadcrumbs: Breadcrumb[]
isManagedMigrationSubmitting: boolean
isManagedMigrationValid: boolean
@@ -129,6 +143,21 @@ export interface managedMigrationLogicActions {
editManagedMigration: (id: string | null) => {
id: string | null
}
+ loadAwsIamSetup: () => any
+ loadAwsIamSetupFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadAwsIamSetupSuccess: (
+ awsIamSetup: BatchImportAWSIAMSetupApi,
+ payload?: any
+ ) => {
+ awsIamSetup: BatchImportAWSIAMSetupApi
+ payload?: any
+ }
loadMigrations: () => any
loadMigrationsFailure: (
error: string,
@@ -300,6 +329,15 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
closeTrialResults: () => null,
},
],
+ awsIamSetup: [
+ null as BatchImportAWSIAMSetupApi | null,
+ {
+ loadAwsIamSetup: async () => {
+ const projectId = ApiConfig.getCurrentProjectId()
+ return await managedMigrationsAwsIamSetupRetrieve(String(projectId))
+ },
+ },
+ ],
})),
forms({
managedMigration: {
@@ -309,6 +347,8 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
content_type,
access_key,
secret_key,
+ s3_auth_method,
+ role_arn,
s3_region,
s3_bucket,
start_date,
@@ -319,12 +359,15 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
is_trial,
trial_record_limit,
}: ManagedMigrationForm) => {
+ const usesIamRole = (source_type === 's3' || source_type === 's3_gzip') && s3_auth_method === 'iam_role'
+
const errors: Record<string, string | null> = {
- secret_key: !secret_key
- ? source_type === 'mixpanel'
- ? 'Project secret is required'
- : 'Secret key is required'
- : null,
+ secret_key:
+ !secret_key && !usesIamRole
+ ? source_type === 'mixpanel'
+ ? 'Project secret is required'
+ : 'Secret key is required'
+ : null,
}
if (is_trial) {
@@ -335,10 +378,18 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
}
// Mixpanel authenticates with the project secret alone — no access key.
- if (source_type !== 'mixpanel') {
+ if (source_type !== 'mixpanel' && !usesIamRole) {
errors.access_key = !access_key ? 'Access key is required' : null
}
+ if (usesIamRole) {
+ errors.role_arn = !role_arn
+ ? 'IAM role ARN is required'
+ : !S3_ROLE_ARN_REGEX.test(role_arn)
+ ? 'Enter a valid IAM role ARN, e.g. arn:aws:iam::123456789012:role/posthog-import'
+ : null
+ }
+
if (source_type === 's3' || source_type === 's3_gzip') {
errors.s3_region = !s3_region ? 'S3 region is required' : null
errors.s3_bucket = !s3_bucket ? 'S3 bucket is required' : null
@@ -382,12 +433,16 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
},
submit: async (values: ManagedMigrationForm) => {
const projectId = ApiConfig.getCurrentProjectId()
+ const usesIamRole =
+ (values.source_type === 's3' || values.source_type === 's3_gzip') &&
+ values.s3_auth_method === 'iam_role'
let payload: ManagedMigrationForm = {
source_type: values.source_type,
- access_key: values.access_key,
- secret_key: values.secret_key,
content_type: values.content_type,
...(values.is_trial ? { is_trial: true, trial_record_limit: values.trial_record_limit } : {}),
+ ...(usesIamRole
+ ? { role_arn: values.role_arn }
+ : { access_key: values.access_key, secret_key: values.secret_key }),
}
if (values.source_type === 's3' || values.source_type === 's3_gzip') {
payload = {
@@ -395,7 +450,7 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
s3_region: values.s3_region,
s3_bucket: values.s3_bucket,
s3_prefix: values.s3_prefix,
- ...(values.endpoint_url ? { endpoint_url: values.endpoint_url } : {}),
+ ...(values.endpoint_url && !usesIamRole ? { endpoint_url: values.endpoint_url } : {}),
}
if (values.content_type === 'amplitude') {
@@ -432,6 +487,13 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
},
}),
listeners(({ actions, values, cache }) => ({
+ loadAwsIamSetupSuccess: ({ awsIamSetup }) => {
+ // Default to the recommended role auth once we know it's available, unless the
+ // user already started entering access keys while the setup call was in flight
+ if (awsIamSetup?.available && !values.managedMigration.access_key && !values.managedMigration.secret_key) {
+ actions.setManagedMigrationValue('s3_auth_method', 'iam_role')
+ }
+ },
submitManagedMigrationSuccess: async ({ managedMigration }) => {
if (managedMigration?.is_trial) {
lemonToast.success('Trial run started — results will appear here when it completes')
@@ -580,6 +642,7 @@ export const managedMigrationLogic = kea<managedMigrationLogicType>([
})),
afterMount(({ actions }) => {
actions.loadMigrations()
+ actions.loadAwsIamSetup()
}),
beforeUnmount(({ actions }) => {
actions.stopPolling()
diff --git a/products/managed_migrations/frontend/types.ts b/products/managed_migrations/frontend/types.ts
index bf38806d8210..621811f6e4f3 100644
--- a/products/managed_migrations/frontend/types.ts
+++ b/products/managed_migrations/frontend/types.ts
@@ -66,6 +66,7 @@ export interface S3ManagedMigration extends BaseManagedMigration {
s3_region: string
s3_bucket: string
s3_prefix: string
+ role_arn?: string
endpoint_url?: string
}
@@ -74,6 +75,7 @@ export interface S3GzipManagedMigration extends BaseManagedMigration {
s3_region: string
s3_bucket: string
s3_prefix: string
+ role_arn?: string
endpoint_url?: string
}