-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-step-function.py
More file actions
105 lines (91 loc) · 3.2 KB
/
create-step-function.py
File metadata and controls
105 lines (91 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import boto3
import json
def create_step_function():
"""Create Step Functions state machine for document processing"""
stepfunctions = boto3.client('stepfunctions', region_name='us-east-1')
iam = boto3.client('iam', region_name='us-east-1')
# Step Functions definition
definition = {
"Comment": "Document Processing Workflow",
"StartAt": "ProcessDocument",
"States": {
"ProcessDocument": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "aws-idp-system-textract-processor-dev",
"Payload.$": "$"
},
"Next": "StoreResults"
},
"StoreResults": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "aws-idp-system-data-store-dev",
"Payload.$": "$"
},
"End": True
}
}
}
# Create IAM role for Step Functions
trust_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "states.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
role_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["lambda:InvokeFunction"],
"Resource": "*"
}
]
}
try:
# Create role
role_response = iam.create_role(
RoleName='StepFunctionsExecutionRole',
AssumeRolePolicyDocument=json.dumps(trust_policy)
)
iam.put_role_policy(
RoleName='StepFunctionsExecutionRole',
PolicyName='LambdaInvokePolicy',
PolicyDocument=json.dumps(role_policy)
)
role_arn = role_response['Role']['Arn']
print(f"✅ Created IAM role: {role_arn}")
except iam.exceptions.EntityAlreadyExistsException:
role_arn = f"arn:aws:iam::774305598371:role/StepFunctionsExecutionRole"
print(f"✅ Using existing IAM role: {role_arn}")
try:
# Create state machine
response = stepfunctions.create_state_machine(
name='aws-idp-system-document-processing-dev',
definition=json.dumps(definition),
roleArn=role_arn
)
print(f"✅ Created Step Functions state machine:")
print(f" ARN: {response['stateMachineArn']}")
return response['stateMachineArn']
except Exception as e:
print(f"❌ Error creating state machine: {e}")
return None
if __name__ == "__main__":
print("🚀 Creating Step Functions state machine...")
arn = create_step_function()
if arn:
print("\n✅ Step Functions created successfully!")
print("Update your Flask app with this ARN:")
print(f"STEP_FUNCTION_ARN = '{arn}'")
else:
print("\n❌ Failed to create Step Functions")