> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Lambda step functions logging enabled remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using the AWS console, follow these steps:

        1. **Login to AWS Console**: Go to the AWS Management Console and login with your credentials.

        2. **Navigate to Step Functions**: In the AWS Management Console, navigate to the Step Functions service by either typing "Step Functions" in the search bar or locating it under the "Services" dropdown menu.

        3. **Select the Step Function**: Click on the Step Function that is associated with the AWS Lambda function for which you want to enable logging.

        4. **Edit the State**: In the Step Function's graphical representation, find the state that invokes the AWS Lambda function. Click on that state to edit its configuration.

        5. **Enable Logging**: Look for an option to enable logging for the state. This option is usually found in the configuration settings of the state. Enable the logging option and configure the desired log settings such as log level, log format, and log destination.

        6. **Save the Changes**: After enabling logging and configuring the settings, save the changes to update the state configuration.

        7. **Test the Step Function**: Run a test on the Step Function to ensure that logging is now enabled for the AWS Lambda function.

        By following these steps, you will successfully remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using the AWS console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using AWS CLI, follow these steps:

        1. Open your terminal and ensure you have the AWS CLI installed and configured with the necessary permissions to make changes to Step Functions and Lambda functions.

        2. Enable logging for your Step Functions by updating the state machine using the AWS CLI command `update-state-machine`. Replace `STATE_MACHINE_ARN` with the ARN of your Step Function and `LOGGING_ARN` with the ARN of the CloudWatch Logs group where you want to store the logs.

        ```bash theme={null}
        aws stepfunctions update-state-machine --state-machine-arn STATE_MACHINE_ARN --logging-configuration "{ 'level': 'ALL', 'includeExecutionData': true, 'destinations': [{ 'cloudWatchLogsLogGroup': { 'logGroupArn': 'LOGGING_ARN' } }]}"
        ```

        3. Next, you need to update your Lambda function to ensure that it sends logs to CloudWatch Logs. Update the Lambda function configuration using the AWS CLI command `update-function-configuration`. Replace `FUNCTION_NAME` with the name of your Lambda function.

        ```bash theme={null}
        aws lambda update-function-configuration --function-name FUNCTION_NAME --cli-connect-timeout 60 --cli-read-timeout 60 --logging-config "{ 'logGroupName': 'LOGGING_ARN', 'logType': 'Tail' }"
        ```

        4. Verify that the logging configuration has been successfully updated for both the Step Function and the Lambda function by checking the respective configurations in the AWS Management Console or by using the AWS CLI commands `describe-state-machine` and `get-function-configuration`.

        By following these steps, you can remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using Python, follow these steps:

        1. Open the AWS Management Console and navigate to the Step Functions service.
        2. Click on the Step Function that needs to have logging enabled.
        3. Click on the "Edit" button to modify the state machine.
        4. In the state machine definition, add a new field called "LoggingConfiguration" with the desired logging configuration. Here is an example of how you can add logging using Python:

        ```python theme={null}
        {
          "Comment": "A simple AWS Step Functions state machine that logs execution history",
          "StartAt": "HelloWorld",
          "States": {
            "HelloWorld": {
              "Type": "Task",
              "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
              "End": true
            }
          },
          "LoggingConfiguration": {
              "Level": "ALL"
          }
        }
        ```

        Replace `REGION`, `ACCOUNT_ID`, and `FUNCTION_NAME` with your specific values.

        5. Click on the "Save" button to save the changes to the state machine.
        6. Test the state machine to ensure that logging is now enabled for the AWS Lambda function.

        By following these steps and adding the `LoggingConfiguration` field to the state machine definition, you can remediate the misconfiguration of Step Functions not having logging enabled for AWS Lambda using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "aws_cloudwatch_log_group" "STATE_MACHINE_LOG_GROUP" {
          name              = "/aws/vendedlogs/states/STATE_MACHINE_NAME-logs" # replace STATE_MACHINE_NAME
          retention_in_days = 30                                              # adjust as needed

          # Optionally add tags
          # tags = {
          #   Name = "STATE_MACHINE_NAME Step Functions logs"
          # }
        }

        resource "aws_iam_role" "STATE_MACHINE_ROLE" {
          name = "STATE_MACHINE_ROLE_NAME" # replace STATE_MACHINE_ROLE_NAME

          assume_role_policy = jsonencode({
            Version = "2012-10-17"
            Statement = [{
              Effect = "Allow"
              Principal = {
                Service = "states.amazonaws.com"
              }
              Action = "sts:AssumeRole"
            }]
          })
        }

        resource "aws_iam_role_policy" "STATE_MACHINE_LOGGING_POLICY" {
          name = "STATE_MACHINE_LOGGING_POLICY_NAME" # replace STATE_MACHINE_LOGGING_POLICY_NAME
          role = aws_iam_role.STATE_MACHINE_ROLE.id

          policy = jsonencode({
            Version = "2012-10-17"
            Statement = [
              {
                Effect = "Allow"
                Action = [
                  "logs:CreateLogStream",
                  "logs:PutLogEvents"
                ]
                Resource = "${aws_cloudwatch_log_group.STATE_MACHINE_LOG_GROUP.arn}:*"
              }
            ]
          })
        }

        resource "aws_sfn_state_machine" "STATE_MACHINE" {
          name     = "STATE_MACHINE_NAME" # replace STATE_MACHINE_NAME
          role_arn = aws_iam_role.STATE_MACHINE_ROLE.arn
          type     = "STANDARD"           # or "EXPRESS" as appropriate

          definition = file("PATH_TO_STATE_MACHINE_DEFINITION.json") # replace PATH_TO_STATE_MACHINE_DEFINITION.json

          logging_configuration {
            level                  = "ALL"
            include_execution_data = true

            destinations {
              cloudwatch_logs_log_group {
                log_group_arn = aws_cloudwatch_log_group.STATE_MACHINE_LOG_GROUP.arn
              }
            }
          }

          # Optionally add tags
          # tags = {
          #   Name = "STATE_MACHINE_NAME"
          # }
        }
        ```

        Substitute:

        * `STATE_MACHINE_NAME` with your Step Functions state machine name.
        * `STATE_MACHINE_ROLE_NAME` with the IAM role name used by the state machine (or point `role_arn` at an existing role).
        * `STATE_MACHINE_LOGGING_POLICY_NAME` with a suitable IAM policy name.
        * `PATH_TO_STATE_MACHINE_DEFINITION.json` with the path to your state machine ASL/JSON file.

        This change is in-place for an existing `aws_sfn_state_machine` (no forced replacement; Terraform will update the logging configuration).

        Verification: `terraform plan` should show:

        * `aws_cloudwatch_log_group.STATE_MACHINE_LOG_GROUP` to be created (if new).
        * `aws_iam_role_policy.STATE_MACHINE_LOGGING_POLICY` to be created or updated.
        * `aws_sfn_state_machine.STATE_MACHINE` with `logging_configuration.level = "ALL"`, `include_execution_data = true`, and a destination referencing the CloudWatch Log Group ARN.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
