When designing an software, you need to combine and mix a number of AWS companies in essentially the most optimized manner for an efficient and environment friendly structure:
- Optimize for efficiency by decreasing the latency between companies
- Optimize for prices operability and sustainability, by avoiding pointless parts and decreasing workload footprint
- Optimize for resiliency by eradicating potential level of failures
- Optimize for safety by minimizing the assault floor
As said within the Serverless Software Lens of the Nicely-Architected Framework, “In case your AWS Lambda perform is just not performing customized logic whereas integrating with different AWS companies, likelihood is that it might be pointless.” As well as, Amazon API Gateway, AWS AppSync, AWS Step Features, Amazon EventBridge, and Lambda Locations can straight combine with various companies. These optimizations can give you extra worth and fewer operational overhead.
This weblog submit will present how you can optimize an structure with direct integration.
Workflow instance and preliminary structure
Determine 1 exhibits a typical workflow for the creation of a web based checking account. The shopper fills out a registration type with private info and provides an image of their ID card. The applying then validates ID and deal with, and scans if there’s already an present person by that title. If all the pieces checks out, a backend software shall be notified to create the account. Lastly, the person is notified of profitable completion.

Determine 1. Checking account software workflow
The workflow structure is proven in Determine 2 (click on on the image to get full decision).
This structure comprises 13 Lambda capabilities. In the event you take a look at the code on GitHub, you may see that:
5 of those Lambda capabilities are fundamental and carry out easy operations:
Further Lambda capabilities carry out different duties, resembling verification and validation:
- One perform generates a presigned URL to add ID card photos to Amazon Easy Storage Service (Amazon S3)
- One perform makes use of the Amazon Textract API to extract info from the ID card
- One perform verifies the id of the person towards the knowledge extracted from the ID card
- One perform performs easy HTTP request to a third-party API to validate the deal with
Lastly, 4 capabilities concern the websocket (join
, message
, and disconnect
) and notifications to the person.
Alternatives for enchancment
In the event you additional analyze the code of the 5 fundamental capabilities (see startWorkflow
on GitHub, for instance), you’ll discover that there are literally three strains of elementary code that begin the workflow. The others 38 strains contain imports, enter validation, error dealing with, logging, and tracing. Keep in mind that all this code should be examined and maintained.
import os
import json
import boto3
from aws_lambda_powertools import Tracer
from aws_lambda_powertools import Logger
import re
logger = Logger()
tracer = Tracer()
sfn = boto3.shopper('stepfunctions')
PATTERN = re.compile(r"^arn:(aws[a-zA-Z-]*)?:states:[a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-d{1}:d{12}:stateMachine:[a-zA-Z0-9-_]+$")
if ('STATE_MACHINE_ARN' not in os.environ
or os.environ['STATE_MACHINE_ARN'] is None
or not PATTERN.match(os.environ['STATE_MACHINE_ARN'])):
elevate RuntimeError('STATE_MACHINE_ARN env var is just not set or incorrect')
STATE_MACHINE_ARN = os.environ['STATE_MACHINE_ARN']
@logger.inject_lambda_context
@tracer.capture_lambda_handler
def handler(occasion, context):
strive:
occasion['requestId'] = context.aws_request_id
sfn.start_execution(
stateMachineArn=STATE_MACHINE_ARN,
enter=json.dumps(occasion)
)
return {
'requestId': occasion['requestId']
}
besides Exception as error:
logger.exception(error)
elevate RuntimeError('Inside Error - can not begin the creation workflow') from error
After working this workflow a number of instances and reviewing the AWS X-Ray traces (Determine 3), we are able to see that it takes about 2–3 seconds when capabilities are warmed:

Determine 3. X-Ray traces when Lambda capabilities are warmed
However the course of takes round 10 seconds with chilly begins, as proven in Determine 4:

Determine 4. X-Ray traces when Lambda capabilities are chilly
We use an asynchronous structure to keep away from ready time for the person, as this is usually a lengthy course of. We additionally use WebSockets to inform the person when it’s completed. This provides some complexity, new parts, and extra prices to the structure. Now let’s take a look at how we are able to optimize this structure.
Bettering the preliminary structure
Direct integration with Step Features
Step Features can straight combine with some AWS companies, together with DynamoDB, Amazon SQS, and EventBridge, and greater than 10,000 APIs from 200+ AWS companies. With these integrations, you may exchange Lambda capabilities when they don’t present worth. We suggest utilizing Lambda capabilities to rework information, to not transport information from one service to a different.
In our checking account creation use case, there are 4 Lambda capabilities we are able to exchange with direct service integrations (see massive arrows in Determine 5):
- Question a DynamoDB desk to seek for a person
- Ship a message to an SQS queue when the extraction fails
- Create the person in DynamoDB
- Ship an occasion on EventBridge to inform the backend

Determine 5. Lambda capabilities that may be changed
It’s not as clear that we have to exchange the opposite Lambda capabilities. Listed here are some concerns:
- To extract info from the ID card, we use Amazon Textract. It’s accessible via the SDK integration in Step Features. Nonetheless, the API’s response offers an excessive amount of info. We suggest utilizing a library resembling amazon-textract-response-parser to parse the end result. For this, you’ll want a Lambda perform.
- The id cross-check performs a easy comparability between the information offered within the internet type and the one extracted within the ID card. We are able to carry out this comparability in Step Features utilizing a
Selection
state and several other situations. If the enterprise logic turns into extra advanced, think about using a Lambda perform. - To validate the deal with, we question a third-party API. Step Features can not straight name a third-party HTTP endpoint, however as a result of it’s built-in with API Gateway, we are able to create a proxy for this endpoint.
In the event you solely have to retrieve information from an API or make a easy API name, use the direct integration. If it’s worthwhile to implement some logic, use a Lambda perform.
Direct integration with API Gateway
API Gateway additionally offers service integrations. Specifically, we are able to begin the workflow with out utilizing a Lambda perform. Within the console, choose the mixing kind “AWS Service”, the AWS service “Step Features”, the motion “StartExecution”, and “POST” methodology, as proven in Determine 6.

Determine 6. API Gateway direct integration with Step Features
After that, use a mapping template within the integration request to outline the parameters as proven right here:
{
"stateMachineArn":"arn:aws:states:eu-central-1:123456789012:stateMachine: accountCreationWorkflow",
"enter":"$util.escapeJavaScript($enter.json('$'))"
}
We are able to go additional and take away the websockets and related Lambda capabilities join
, message
, and disconnect
. By utilizing Synchronous Specific Workflows and the StartSyncExecution
API, we are able to begin the workflow and await the lead to a synchronous vogue. API Gateway will then straight return the results of the workflow to the shopper.
Ultimate optimized structure
After making use of these optimizations, we now have the up to date structure proven in Determine 7. It makes use of solely two Lambda capabilities out of the preliminary 13. The remaining have been changed by direct service integrations or applied in Step Features.

Determine 7. Ultimate optimized structure
We had been in a position to take away 11 Lambda capabilities and their related charges. On this structure, the price is principally pushed by Step Features, and the primary value distinction shall be your use of Specific Workflows as an alternative of Commonplace Workflows. If it’s worthwhile to hold some Lambda capabilities, use AWS Lambda Energy Tuning to configure your perform appropriately and profit from the very best value/efficiency ratio.
One of many principal advantages of this structure is efficiency. With the ultimate workflow structure, it now takes about 1.5 seconds when the Lambda perform is warmed and three seconds on chilly begins (versus as much as 10 seconds beforehand), see Determine 8:

Determine 8. X-Ray traces for the ultimate structure
The method can now be synchronous. It reduces the complexity of the structure and vastly improves the person expertise.
An additional advantage is that by decreasing the general complexity and eradicating the pointless Lambda capabilities, we now have additionally decreased the danger of failures. These will be errors within the code, reminiscence or timeout points attributable to dangerous configuration, lack of permissions, community points between parts, and extra. This will increase the resiliency of the applying and eases its upkeep.
Testing
Testability is a vital consideration when constructing your workflow. Unit testing a Lambda perform is easy, and you need to use your most well-liked testing framework and validate strategies. Adopting a hexagonal structure additionally helps take away dependencies to the cloud.
When eradicating capabilities and utilizing an method with direct service integrations, you might be by definition straight linked to the cloud. You continue to should confirm that the general course of is working as anticipated, and validate these integrations.
You possibly can obtain this sort of exams domestically utilizing Step Features Native, and the not too long ago introduced Mocked Service Integrations. By mocking service integrations, for instance, retrieving an merchandise in DynamoDB, you may validate the totally different paths of your state machine.
You additionally need to carry out integration exams, however that is true whether or not you utilize direct integrations or Lambda capabilities.
Conclusion
This submit describes how you can simplify your structure and optimize for efficiency, resiliency, and value by utilizing direct integrations in Step Features and API Gateway. Though many Lambda capabilities had been decreased, some stay helpful for dealing with extra advanced enterprise logic and information transformation. Do that out now by visiting the GitHub repository.
For additional studying: