Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCS-9587: restructure step functions install page #26825

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 240 additions & 0 deletions content/en/serverless/guide/step_functions_cdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
---
title: CDK Examples for Instrumenting AWS Step Functions
further_reading:
- link: "/serverless/step_functions/installation"
tag: "Documentation"
text: "Install Serverless Monitoring for AWS Step Functions"
---

This page lists language-specific code examples for [instrumenting AWS Step Functions][1] using [Datadog's CDK Construct Library][2].

## Basic setup

{{< tabs >}}
{{% tab "TypeScript" %}}

Example stack: [`step-functions-typescript-stack`][1]

{{< code-block lang="typescript" disable_copy="false" >}}
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
import { DatadogStepFunctions} from "datadog-cdk-constructs-v2";

const stateMachine = new sfn.StateMachine(...);
const datadogSfn = new DatadogStepFunctions(this, "DatadogSfn", {
env: "<ENV>", // e.g. "dev"
service: "<SERVICE>", // e.g. "my-cdk-service"
version: "<VERSION>", // e.g. "1.0.0"
forwarderArn: "<FORWARDER_ARN>", // e.g. "arn:test:forwarder:sa-east-1:12345678:1"
tags: <TAGS>, // optional, e.g. "custom-tag-1:tag-value-1,custom-tag-2:tag-value-2"
});
datadogSfn.addStateMachines([stateMachine]);
{{< /code-block >}}

[1]: https://github.com/DataDog/datadog-cdk-constructs/tree/main/examples/step-functions-typescript-stack

{{% /tab %}}
{{% tab "Python" %}}

Example stack: [`step-functions-python-stack`][1]

{{< code-block lang="python" disable_copy="false" >}}
from aws_cdk import (
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from datadog_cdk_constructs_v2 import DatadogStepFunctions, DatadogLambda

state_machine = sfn.StateMachine(...)
datadog_sfn = DatadogStepFunctions(
self,
"DatadogSfn",
env="<ENV>", # e.g. "dev"
service="<SERVICE>", # e.g. "my-cdk-service"
version="<VERSION>", # e.g. "1.0.0"
forwarderArn="<FORWARDER_ARN>", # e.g. "arn:test:forwarder:sa-east-1:12345678:1"
tags=<TAGS>, # optional, e.g. "custom-tag-1:tag-value-1,custom-tag-2:tag-value-2"
)
datadog_sfn.add_state_machines([child_state_machine, parent_state_machine])
{{< /code-block >}}

[1]: https://github.com/DataDog/datadog-cdk-constructs/tree/main/examples/step-functions-python-stack
{{% /tab %}}
{{% tab "Go" %}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question, but do we only have the official CDK constructs available in Go, TS and Python? And no support for .NET/Java right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. I added Go in August 2024. Let me know if there's a need to support .NET or Java.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.NET & Java definitely aren't as widely used, I'm just thinking for completeness that's all. So we can say we support all runtimes.

Is there not a way with the CDK to write it once in Javascript and then auto-gen the other runtimes though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a CDK construct for defining .NET Lambda functions. The construct is defined once in Typescript and then uses Projen to generate all the other packages. Could we not do the same for our constructs?

https://github.com/cdklabs/awscdk-lambda-dotnet/blob/main/.github/workflows/release.yml

Copy link
Contributor

@lym953 lym953 Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it's 1–2 week one-time work to support a new language.
From what I understand, we often use a "lazy" approach that we don't build something until some customer asks for it. Have you heard of any customer ask, or could you align with Sumedha on this?


Example stack: [`step-functions-go-stack`][1]

{{< code-block lang="go" disable_copy="false" >}}
import (
"github.com/DataDog/datadog-cdk-constructs-go/ddcdkconstruct"
"github.com/aws/aws-cdk-go/awscdk/v2"
sfn "github.com/aws/aws-cdk-go/awscdk/v2/awsstepfunctions"
)

stack := awscdk.NewStack(...)
stateMachine := sfn.NewStateMachine(...)
datadogSfn := ddcdkconstruct.NewDatadogStepFunctions(
stack,
jsii.String("DatadogSfn"),
&ddcdkconstruct.DatadogStepFunctionsProps{
Env: jsii.String("<ENV>"), // e.g. "dev"
Service: jsii.String("<SERVICE>"), // e.g. "my-cdk-service"
Version: jsii.String("<VERSION>"), // e.g. "1.0.0"
ForwarderArn: jsii.String("<FORWARDER_ARN>"), // e.g. "arn:test:forwarder:sa-east-1:12345678:1"
Tags: jsii.String("<TAGS>"), // optional, e.g. "custom-tag-1:tag-value-1,custom-tag-2:tag-value-2"
}
)
datadogSfn.AddStateMachines(&[]sfn.StateMachine{stateMachine}, nil)
{{< /code-block >}}

[1]: https://github.com/DataDog/datadog-cdk-constructs/tree/main/examples/step-functions-go-stack

{{% /tab %}}
{{< /tabs >}}

## Merging traces

To merge the Step Function's traces with downstream Lambda function or Step Function traces, modify the Lambda task payload or Step Function task input:

{{< tabs >}}
{{% tab "TypeScript" %}}

{{< code-block lang="typescript" disable_copy="false" >}}
import * as tasks from "aws-cdk-lib/aws-stepfunctions-tasks";
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
import { DatadogStepFunctions, DatadogLambda } from "datadog-cdk-constructs-v2";

const lambdaFunction = ...;
const lambdaTask = new tasks.LambdaInvoke(this, "MyLambdaTask", {
lambdaFunction: lambdaFunction,
payload: sfn.TaskInput.fromObject(
DatadogStepFunctions.buildLambdaPayloadToMergeTraces(
{ "custom-key": "custom-value" }
)
),
});

const childStateMachine = new sfn.StateMachine(...);
const invokeChildStateMachineTask = new tasks.StepFunctionsStartExecution(this, "InvokeChildStateMachineTask", {
stateMachine: childStateMachine,
input: sfn.TaskInput.fromObject(
DatadogStepFunctions.buildStepFunctionTaskInputToMergeTraces({ "custom-key": "custom-value" }),
),
});

const stateMachine = new sfn.StateMachine(this, "CdkTypeScriptTestStateMachine", {
definitionBody: sfn.DefinitionBody.fromChainable(lambdaTask.next(invokeChildStateMachineTask)),
});

const datadogLambda = ...;
datadogLambda.addLambdaFunctions([lambdaFunction]);

const datadogSfn = ...;
datadogSfn.addStateMachines([childStateMachine, stateMachine]);
{{< /code-block >}}

{{% /tab %}}
{{% tab "Python" %}}

{{< code-block lang="python" disable_copy="false" >}}
from aws_cdk import (
aws_lambda,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from datadog_cdk_constructs_v2 import DatadogStepFunctions, DatadogLambda

lambda_function = aws_lambda.Function(...)
lambda_task = tasks.LambdaInvoke(
self,
"MyLambdaTask",
lambda_function=lambda_function,
payload=sfn.TaskInput.from_object(
DatadogStepFunctions.build_lambda_payload_to_merge_traces(
{"custom-key": "custom-value"}
)
),
)

child_state_machine = sfn.StateMachine(...)
invoke_child_state_machine_task = tasks.StepFunctionsStartExecution(
self,
"InvokeChildStateMachineTask",
state_machine=child_state_machine,
input=sfn.TaskInput.from_object(
DatadogStepFunctions.build_step_function_task_input_to_merge_traces(
{"custom-key": "custom-value"}
)
),
)

state_machine = sfn.StateMachine(
self,
"CdkPythonTestStateMachine",
definition_body=sfn.DefinitionBody.from_chainable(
lambda_task.next(invoke_child_state_machine_task)
),
)

datadog_lambda = DatadogLambda(...)
datadog_lambda.add_lambda_functions([lambda_function])

datadog_sfn = DatadogStepFunctions(...)
datadog_sfn.add_state_machines([child_state_machine, state_machine])
{{< /code-block >}}

{{% /tab %}}
{{% tab "Go" %}}

{{< code-block lang="go" disable_copy="false" >}}
import (
"github.com/DataDog/datadog-cdk-constructs-go/ddcdkconstruct"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
sfn "github.com/aws/aws-cdk-go/awscdk/v2/awsstepfunctions"
sfntasks "github.com/aws/aws-cdk-go/awscdk/v2/awsstepfunctionstasks"
"github.com/aws/jsii-runtime-go"
)

lambdaFunction := awslambda.NewFunction(...)
lambdaPayload := ddcdkconstruct.DatadogStepFunctions_BuildLambdaPayloadToMergeTraces(&map[string]interface{}{
"custom-key": "custom-value",
})
lambdaTask := sfntasks.NewLambdaInvoke(stack, jsii.String("MyLambdaTask"), &sfntasks.LambdaInvokeProps{
LambdaFunction: lambdaFunction,
Payload: sfn.TaskInput_FromObject(lambdaPayload),
})

childStateMachine := sfn.NewStateMachine(...)
stateMachineTaskInput := ddcdkconstruct.DatadogStepFunctions_BuildStepFunctionTaskInputToMergeTraces(
&map[string]interface{}{
"custom-key": "custom-value",
}
)
invokeChildStateMachineTask := sfntasks.NewStepFunctionsStartExecution(
stack,
jsii.String("InvokeChildStateMachineTask"),
&sfntasks.StepFunctionsStartExecutionProps{
StateMachine: childStateMachine,
Input: sfn.TaskInput_FromObject(stateMachineTaskInput),
}
)
stateMachine := sfn.NewStateMachine(stack, jsii.String("CdkGoTestStateMachine"), &sfn.StateMachineProps{
DefinitionBody: sfn.DefinitionBody_FromChainable(lambdaTask.Next(invokeChildStateMachineTask)),
})

datadogLambda := ...
datadogLambda.AddLambdaFunctions(&[]interface{}{lambdaFunction}, nil)

datadogSfn := ...
datadogSfn.AddStateMachines(&[]sfn.StateMachine{childStateMachine, stateMachine}, nil)
{{< /code-block >}}

{{% /tab %}}
{{< /tabs >}}

## Further reading

{{< partial name="whats-next/whats-next.html" >}}

[1]: /serverless/step_functions/installation
[2]: https://github.com/DataDog/datadog-cdk-constructs

Loading
Loading