-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
cswatt
wants to merge
6
commits into
master
Choose a base branch
from
cswatt/sfn-updates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+428
−10
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23aafeb
initial commit
cswatt b6e5031
updating
cswatt 5ec964f
Merge branch 'master' into cswatt/sfn-updates
cswatt bfd6259
more updates
cswatt 7706f5f
some updates
cswatt a7bd19a
Merge branch 'master' into cswatt/sfn-updates
cswatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %}} | ||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?