npx skills add ...
npx skills add aws/agent-toolkit-for-aws --skill processing-s3-uploads-with-step-functions
npx skills add aws/agent-toolkit-for-aws --skill processing-s3-uploads-with-step-functions
Deploy an event-driven workflow that routes S3 uploads to either Lambda or Fargate via Step Functions based on file size. Uses EventBridge to trigger a Step Functions state machine when objects are uploaded to S3. Small files are processed by Lambda, large files by a Fargate task. Includes VPC, ECR repository, ECS cluster, and scoped IAM roles. Trigger keywords: Step Functions, Fargate, Lambda, S3 event, EventBridge, ECS, ECR, file processing, workflow orchestration, serverless.
This skill deploys an event-driven workflow using AWS CLI. When a file is uploaded to an S3 bucket, EventBridge triggers a Step Functions state machine. The state machine checks the file size and routes processing to either a Lambda function (files ≤ 6 MB) or a Fargate task (files > 6 MB).
The architecture includes:
Use this skill when:
Do not use this skill when:
aws sts get-caller-identity.aws kms create-key --description "Key for CloudWatch Logs encryption" --region {region}Constraints for parameter acquisition:
Constraints:
Constraints:
aws sts get-caller-identity --query 'Account' --output textConstraints:
aws ec2 describe-vpcs --filters Name=isDefault,Values=true --query 'Vpcs[0].VpcId' --output text --region {region}aws ec2 create-default-vpc --region {region} or provide a VPC ID manuallyaws ec2 describe-subnets --filters Name=vpc-id,Values={vpc_id} --query 'Subnets[0:2].SubnetId' --output text --region {region}aws ec2 create-security-group --group-name fargate-sg --description "Security group for Fargate tasks" --vpc-id {vpc_id} --region {region}aws ec2 revoke-security-group-egress --group-id {sg_id} --ip-permissions IpProtocol=-1,IpRanges='[{CidrIp=0.0.0.0/0}]' --region {region}
Then add scoped rules:
aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol tcp --port 443 --cidr 0.0.0.0/0 --region {region} and
aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol udp --port 53 --cidr 0.0.0.0/0 --region {region}Constraints:
aws ecr create-repository --repository-name {ecr_repo_name} --region {region}Constraints:
You MUST verify Docker is installed by running docker --version. If Docker is not installed, instruct the user to install it from https://docs.docker.com/get-docker/ and abort until it is available
You MUST authenticate Docker with ECR:
aws ecr get-login-password --region {region} | docker login --username AWS --password-stdin {account_id}.dkr.ecr.{region}.amazonaws.com
The Dockerfile and processor code are in scripts/Dockerfile and scripts/fargate_processor.py
You MUST build and push the image from the scripts directory:
Follow the detailed instructions in references/iam-roles.md to create all IAM roles (Lambda, ECS task execution, ECS task, Step Functions, and EventBridge roles).
Constraints:
The function code is in scripts/lambda_function.py
You MUST be in the skill root directory before packaging and creating the function
You MUST package it with: python3 -c "import zipfile,io; z=io.BytesIO(); f=zipfile.ZipFile(z,'w'); f.writestr('lambda_function.py', open('scripts/lambda_function.py').read()); f.close(); open('/tmp/lambda_function.zip','wb').write(z.getvalue())"
You MUST create the function with:
You MUST verify the function was created with:
aws lambda get-function --function-name sfn-file-processor --region {region}
Constraints:
aws logs create-log-group --log-group-name /StepFunctionFargateTask --region {region}aws logs associate-kms-key --log-group-name /StepFunctionFargateTask --kms-key-arn {kms_key_arn} --region {region}Follow the detailed instructions in references/ecs-task-definition.md to create the ECS cluster and register the Fargate task definition.
Constraints:
aws s3api create-bucket --bucket {bucket_name} --region {region} --create-bucket-configuration LocationConstraint={region}--create-bucket-configuration if region is us-east-1aws s3api put-bucket-notification-configuration --bucket {bucket_name} --notification-configuration '{"EventBridgeConfiguration": {}}' --region {region}aws s3api put-bucket-encryption --bucket {bucket_name} --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms"}}]}' --region {region}Constraints:
The state machine definition is in scripts/statemachine.asl.json
You MUST create a working copy and replace all placeholders:
You MUST create the state machine with:
You MUST capture the stateMachineArn from the response
Constraints:
You MUST create the EventBridge rule to trigger on S3 object creation:
You MUST add the state machine as a target:
Constraints:
You MUST create a Dead Letter Queue for failed EventBridge invocations:
aws sqs create-queue --queue-name s3-to-stepfunctions-dlq --region {region}
You MUST update the EventBridge target to attach the DLQ:
You MUST create a CloudWatch alarm for Step Functions execution failures:
aws cloudwatch put-metric-alarm --alarm-name sfn-execution-failures --metric-name ExecutionsFailed --namespace AWS/States --statistic Sum --period 300 --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold --evaluation-periods 1 --dimensions Name=StateMachineArn,Value={state_machine_arn} --region {region}
Constraints:
You MUST test with a small file (< 6 MB) to verify Lambda processing:
You MUST wait 15 seconds then check the Step Functions execution:
aws stepfunctions list-executions --state-machine-arn {state_machine_arn} --region {region}
You MUST verify the execution succeeded and routed to Lambda
You MUST provide a summary of all created resources including: VPC ID, subnet IDs, security group ID, ECR repo URI, ECS cluster ARN, task definition ARN, Lambda function ARN, state machine ARN, bucket name, and EventBridge rule name
aws s3api get-bucket-notification-configuration --bucket {bucket_name}aws events describe-rule --name s3-to-stepfunctions --region {region}aws ecr describe-images --repository-name {ecr_repo_name} --region {region}/StepFunctionFargateTaskaws logs tail /aws/lambda/sfn-file-processor --region {region}lambda:InvokeFunction permissioniam:PassRole for both the ECS execution role and task role ARNsaws ec2 revoke-security-group-egress --group-id {sg_id} --ip-permissions IpProtocol=-1,IpRanges='[{CidrIp=0.0.0.0/0}]' then add aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol tcp --port 443 --cidr 0.0.0.0/0 and aws ec2 authorize-security-group-egress --group-id {sg_id} --protocol udp --port 53 --cidr 0.0.0.0/0. For production, consider using VPC endpoints for S3 and CloudWatch Logs instead of internet-routed traffic.aws ecr put-image-scanning-configuration --repository-name {ecr_repo_name} --image-scanning-configuration scanOnPush=true --region {region}aws s3api put-bucket-encryption --bucket {bucket_name} --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"aws:kms"}}]}'aws logs associate-kms-key --log-group-name /StepFunctionFargateTask --kms-key-arn <KMS_KEY_ARN>