AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template for Step Functions State Machine using Amazon Bedrock'
Resources:
  StepFunctionsStateMachine:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      StateMachineName: !Sub '${AWS::StackName}-BedrockRFP'
      StateMachineType: "STANDARD"
      DefinitionString: !Sub |
        {
          "Comment": "A state machine to orchestrate iterating through an array, sending each element to Amazon Bedrock.",
          "StartAt": "Map",
          "States": {
            "Map": {
              "Type": "Map",
              "ItemProcessor": {
                "ProcessorConfig": {
                  "Mode": "INLINE"
                },
                "StartAt": "Bedrock InvokeModel",
                "States": {
                  "Bedrock InvokeModel": {
                    "Type": "Task",
                    "Resource": "arn:aws:states:::bedrock:invokeModel",
                    "Parameters": {
                      "ModelId": "arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-haiku-20241022-v1:0",
                      "Body": {
                        "anthropic_version": "bedrock-2023-05-31",
                        "max_tokens": 800,
                        "messages": [
                          {
                            "role": "user",
                            "content": [
                              {
                                "type": "text",
                                "text.$": "States.Format('Answer following question thoroughly, using the following description. Do not repeat the question. Only respond with the answer to the question. Question: {} Description: {}', $.questions.question, $.description)"
                              }
                            ]
                          }
                        ]
                      }
                    },
                    "End": true,
                    "ResultSelector": {
                      "completion.$": "$.Body.content[0].text"
                    },
                    "ResultPath": "$.questions.response",
                    "OutputPath": "$.questions"
                  }
                }
              },
              "End": true,
              "ItemsPath": "$.questions",
              "ItemSelector": {
                "description.$": "$.description",
                "questions": {
                  "question.$": "$$.Map.Item.Value"
                }
              }
            }
          }
        }
      RoleArn: !GetAtt StepFunctionsExecutionRole.Arn

  StepFunctionsExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: states.amazonaws.com
            Action: 'sts:AssumeRole'
      Policies:
        - PolicyName: StepFunctionsExecutionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - 'bedrock:InvokeModel'
                Resource:
                  - 'arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-haiku-20241022-v1:*'

Outputs:
  StateMachineArn:
    Description: 'ARN of the created Step Functions State Machine'
    Value: !Ref StepFunctionsStateMachine