33
loading...
This website collects cookies to deliver better user experience
cdk destroy
.item_step_function
.aws_cdk.aws_apigateway
's RestApi
constructor to create the base API object. You will use this for all further API setup:from aws_cdk import (
core,
aws_apigateway as apigateway,
aws_iam as iam,
aws_stepfunctions as sfn,
)
import json
item_step_function = sfn.StateMachine([...])
item_api = apigateway.RestApi(self, "item-api")
Role
construct in the aws_iam
package for this.Role,
give it a name, and pick the service that will assume it. Since you want API Gateway to have access to Step Functions, you will use "apigateway.amazonaws.com"
:item_api_role = iam.Role(
self,
f"item-api-role",
role_name=f"item-api-role",
assumed_by=iam.ServicePrincipal("apigateway.amazonaws.com"),
)
item_api_role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AWSStepFunctionsFullAccess")
)
.add_resource()
method, you will add a single resource level for this tutorial. To use a resource as a parameter, surround your parameter name with curly braces. Note that you have to add it to the root
of the API object:step_function_trigger_resource = item_api.root.add_resource("{item_id}")
[https://aws-generated-tld.com/1337](https://aws-generated-tld.com/1337)
where 1337
is the item_id
.AWSIntegration
construct that is supposed to make it easier to integrate with other AWS services. It does not. At least, not by itself.AWSIntegration
construct is difficult to use because implementations for different services aren't well-documented. You may not even know the internal service name for Step Functions or any other service you wish to integrate and have difficulty finding it. (if you do, the AWS CLI is here to help: aws list-services
)."application/json"
. Its value is a JSONified dictionary with your step function's ARN and input
, which is part of the Step Function StartExecution request syntax. You will use some methods built into the request from Amazon, specifically $input.params()
, which allows you to grab some or all of your request's parameters.$input.params()
call with $util.escapeJavaScript()
:"$util.escapeJavaScript($input.params('item_id'))"
.request_template = {
"application/json": json.dumps(
{
"stateMachineArn": item_state_machine.state_machine_arn,
"input": "{\"item_id\": \"$util.escapeJavaScript($input.params('item_id'))\"}",
}
)
}
AWSIntegration
construct is: it allows you to use any service, but you have to know what parameters their requests need."states"
. Then you need to provide the action you want to do. To start a Step Function execution, you'll use "StartExecution"
. This is determined again by the service's API, and you can read more about "StartExecution"
in the AWS Step Function documentation. credentials_role
: This will take the item_api_role
you set up earlier, attaching the Role (and its attached policy) to the API itself via the integration.integration_responses
: This is a list of possible responses to API requests. At the very least, you'll want to return an IntegrationResponse
object with a 200 status code, but you can define all sorts of situations that would trigger different status codes.request_templates
: This is where you'll attach the request template you made in the preitem_sfn_integration = apigateway.AwsIntegration(
service="states",
action="StartExecution",
options=apigateway.IntegrationOptions(
credentials_role=item_api_role,
integration_responses=[
apigateway.IntegrationResponse(status_code="200")
],
request_templates=request_template,
),
)
resource
you set up earlier, defining the item_id
parameter for the API? This object comes with an add_method()
function, which you can use to connect a REST verb (such as GET
, POST
, PUT
, etc.) to your integration. Doing this will allow a request using the correct verb to reach your integration.POST
and wire it to item_sfn_integration
like so:step_function_trigger_resource.add_method(
"POST",
item_sfn_integration,
method_responses=[apigateway.MethodResponse(status_code="200")],
)
step_function_trigger_resource
via the POST
verb, and you set it to respond with a 200
response status. Like the integration, you can set multiple method response statuses.cdk deploy <location>
, open the API Gateway console, and navigate to your API and the REST verb you set up. Just click Test, add your parameter, and check the output. You can also navigate to the Step Functions console and check on your execution.from aws_cdk import (
core,
aws_apigateway as apigateway,
aws_iam as iam,
aws_stepfunctions as sfn,
)
import json
item_step_function = sfn.StateMachine([...])
# Initialize the API
item_api = apigateway.RestApi(self, "item-api")
# Set up IAM role and policy
item_api_role = iam.Role(
self,
f"item-api-role",
role_name=f"item-api-role",
assumed_by=iam.ServicePrincipal("apigateway.amazonaws.com"),
)
item_api_role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AWSStepFunctionsFullAccess")
)
# Set up API resources
step_function_trigger_resource = item_api.root.add_resource("{item_id}")
# Set up request template and integration
request_template = {
"application/json": json.dumps(
{
"stateMachineArn": item_state_machine.state_machine_arn,
"input": "{\"item_id\": \"$util.escapeJavaScript($input.params('item_id'))\"}",
}
)
}
item_sfn_integration = apigateway.AwsIntegration(
service="states",
action="StartExecution",
options=apigateway.IntegrationOptions(
credentials_role=item_api_role,
integration_responses=[
apigateway.IntegrationResponse(status_code="200")
],
request_templates=request_template,
),
)
# Connect integrations to REST verbs
step_function_trigger_resource.add_method(
"POST",
item_sfn_integration,
method_responses=[apigateway.MethodResponse(status_code="200")],
)