如何在Terraform中启用CloudWatch日志记录和X射线的步进功能? [英] How to enable CloudWatch logging and X-ray for stepfunction in Terraform?

查看:99
本文介绍了如何在Terraform中启用CloudWatch日志记录和X射线的步进功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AWS控制台中,我们可以轻松地为步进功能状态机启用cloudwatch日志记录和X射线,但是我希望我的资源完全由Terraform进行管理,请访问以下页面:

In AWS console, we can easily enable cloudwatch logging and X-ray for a step function statemachine, but I want my resource fully managed by Terraform, from this page:https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sfn_state_machine

似乎Terraform目前不支持此功能(另请参见: https://github.com/hashicorp/terraform-provider-aws/issues/12192 )

It seems like Terraform doesn't support this at the moment (also see: https://github.com/hashicorp/terraform-provider-aws/issues/12192)

有人知道是否有解决方法吗?我真的很希望能够同时启用cloudwatch日志和来自Terraform的X射线.我找不到很多有关此的信息.可能有人可以提供帮助吗?非常感谢.

Does anyone know if there is any workaround to achieve this? I'd really like to be able to enable both cloudwatch logs & X-ray from Terraform. I can't find much info on this. Might someone be able to help please? Many thanks.

推荐答案

更新:此功能最近发布相应的文档链接: sfn_state_machine#logging

您可以在 terraform内包装用于启用日志记录的命令null_resource ,如链接的问题中所示启用步进功能记录到CloudWatch#12192 ,如下所示:

You can wrap the command for enabling the logging inside terraform null_resource as it showin the in the linked issueEnabling Step Function Logging To CloudWatch #12192, something like below:

先决条件:

aws-cli/2.1.1

之前:


    {
    "stateMachineArn": "arn:aws:states:us-east-1:1234567890:stateMachine:mystatemachine",
    "name": "my-state-machine",
    "status": "ACTIVE",
    "definition": "{\n  \"Comment\": \"A Hello World example of the Amazon States Language using an AWS Lambda Function\",\n  \"StartAt\": \"HelloWorld\",\n  \"States\": {\n    \"HelloWorld\": {\n      \"Type\": \"Pass\",\n      \"End\": true\n    }\n  }\n}\n",
    "roleArn": "arn:aws:iam::1234567890:role/service-role/StepFunctions-MyStateMachine-role-a6146d54",
    "type": "STANDARD",
    "creationDate": 1611682259.919,
    "loggingConfiguration": {
        "level": "OFF",
        "includeExecutionData": false
    }
}

resource "aws_sfn_state_machine" "sfn_state_machine" {
  name     = "mystatemachine"
  role_arn = "arn:aws:iam::1234567890:role/service-role/StepFunctions-MyStateMachine-role-a6146d54"

  definition = <<EOF
{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Pass",
      "End": true
    }
  }
}
EOF
}

resource "aws_cloudwatch_log_group" "yada" {
  name = "/aws/vendedlogs/states/myloggroup"
}

resource "null_resource" "enable_step_function_logging" {
      triggers = {
    state_machine_arn  = aws_sfn_state_machine.sfn_state_machine.arn
    logs_params=<<PARAMS
    {
        "level":"ALL",
        "includeExecutionData":true,
        "destinations":[
            {
                "cloudWatchLogsLogGroup":{
                    "logGroupArn":"${aws_cloudwatch_log_group.yada.arn}:*"
                    }
                }
            ]
            }
    PARAMS
    }
  provisioner "local-exec" {
    command = <<EOT
set -euo pipefail

aws stepfunctions update-state-machine --state-machine-arn ${self.triggers.state_machine_arn}  --tracing-configuration enabled=true --logging-configuration='${self.triggers.logs_params}'

EOT
    # interpreter = ["bash"]
  }
}

之后:

{
    "stateMachineArn": "arn:aws:states:us-east-1:1234567890:stateMachine:mystatemachine",
    "name": "mystatemachine",
    "status": "ACTIVE",
    "definition": "{\n  \"Comment\": \"A Hello World example of the Amazon States Language using an AWS Lambda Function\",\n  \"StartAt\": \"HelloWorld\",\n  \"States\": {\n    \"HelloWorld\": {\n      \"Type\": \"Pass\",\n      \"End\": true\n    }\n  }\n}\n",
    "roleArn": "arn:aws:iam::1234567890:role/service-role/StepFunctions-MyStateMachine-role-a6146d54",
    "type": "STANDARD",
    "creationDate": 1611687676.151,
    "loggingConfiguration": {
        "level": "ALL",
        "includeExecutionData": true,
        "destinations": [
            {
                "cloudWatchLogsLogGroup": {
                    "logGroupArn": "arn:aws:logs:us-east-1:1234567890:log-group:/aws/vendedlogs/states/myloggroup:*"
                }
            }
        ]
    }
}

这篇关于如何在Terraform中启用CloudWatch日志记录和X射线的步进功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆