Skip to content

With CDK, is it possible to configure the StateMachine’s HttpInvoke Connection as a runtime input?

0

I currently have a State Machine which calls different Http endpoints based on a Tenant ID that is passed in at runtime. For example, if the State Machine is triggered by Tenant 1 it will utilitize an EventBridge Connection stored for them ($tenantID-airtable), if it is triggered by Tenant 2 it will utilize their’s, and so on.

I have started migrating over to creating infrastructure with CDK instead of in the Management Console and am having trouble setting the Connection property of HttpInvoke. Any guide here would be much appreciated.

3 Answers
1

I do not believe that the response above is accurate as the connection property seems to not want a string:

Type 'string' is not assignable to type 'IConnection'.

I know this is how you would approach it within the console but via CDK it seems to either require a different approach or not be possible.

answered 2 years ago

0

Yeah this answer doesn't make sense. AI fail.

This is a real pain too as the alternative is to create a lambda to make http requests but adds complexity which HttpInvoke should abstract away..

answered a year ago

-1

Yes, it is possible to configure the StateMachine's HttpInvoke Connection as a runtime input using AWS CDK, although it requires a slightly different approach than directly setting the Connection property at build time.

To achieve this, you can use the AWS Step Functions Task construct with the "arn:aws:states:::http:invoke" resource and utilize JsonPath to dynamically set the Connection at runtime. Here's how you can approach this:

  1. Instead of specifying the Connection directly in the CDK code, you'll need to pass it as part of the input to your State Machine execution.

  2. In your CDK code, when defining the HttpInvoke task, you can use JsonPath to reference the Connection that will be provided at runtime. For example:

    const httpInvoke = new tasks.HttpInvoke(this, 'HttpInvoke', {
      apiEndpoint: JsonPath.stringAt('$.apiEndpoint'),
      httpMethod: HttpMethod.POST,
      connection: JsonPath.stringAt('$.connection'),
      // other properties as needed
    });
  3. When you execute the State Machine, you'll need to provide the Connection ARN as part of the input. For example:

    {
      "tenantId": "tenant1",
      "connection": "arn:aws:events:us-east-1:123456789012:connection/tenant1-airtable",
      "apiEndpoint": "/some/endpoint"
    }

This approach allows you to dynamically specify the Connection based on the Tenant ID at runtime, while still defining the structure of your State Machine using CDK.

Remember to ensure that your State Machine's IAM role has the necessary permissions to use all the potential Connections that might be passed in at runtime.

Also, note that while this method provides flexibility, it also means that you're deferring some of the configuration to runtime, which can make it harder to validate the correctness of your State Machine at build time. Make sure to implement proper error handling and input validation in your State Machine to account for potential runtime issues.

Sources
interface HttpInvokeProps · AWS CDK
interface HttpIntegrationProps · AWS CDK
Call HTTPS APIs in Step Functions workflows - AWS Step Functions

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.