|
| 1 | +# AWS Step Functions to on-premises API (Terraform) |
| 2 | + |
| 3 | +This pattern demonstrate how to call an on-premises API from a Step Functions state machine, leveraging Amazon EventBridge connection and VPC Lattice resource gateway and resource configuration. |
| 4 | + |
| 5 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 12 | +* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed |
| 13 | + |
| 14 | +## Deployment Instructions |
| 15 | + |
| 16 | +### Pre-requisites |
| 17 | + |
| 18 | +This example assumes you already have a VPC with a connection to your datacenter (through VPN or Direct Connect) and an API is exposed on-premises and accessible from this VPC. |
| 19 | +The VPC and connection to your datacenter are not provided by this example. Refer to this [documentation](https://docs.aws.amazon.com/whitepapers/latest/aws-vpc-connectivity-options/network-to-amazon-vpc-connectivity-options.html) to set up such connectivity. |
| 20 | + |
| 21 | +### Deployment |
| 22 | + |
| 23 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 24 | + ``` |
| 25 | + git clone https://github.com/aws-samples/serverless-patterns |
| 26 | + ``` |
| 27 | +2. Change directory to the pattern directory: |
| 28 | + ``` |
| 29 | + cd stepfunctions-eventbridge-onpremise-tf |
| 30 | + ``` |
| 31 | +3. Create a `.tfvars` file with the following variables (use your custom values) |
| 32 | +
|
| 33 | + ``` |
| 34 | + api_domain_name = "api.internal.mycompany.com" |
| 35 | + api_key_secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:07a4e645-fc95-4a10-853a-410b1b1eca5b-012nZO" |
| 36 | + vpc_id = "vpc-0e03d4ab114e951be" |
| 37 | + private_subnet_ids = ["subnet-05d53fa850148290e","subnet-070324fd8bc5885a5"] |
| 38 | + on_premises_cidr = "172.32.0.0/20" |
| 39 | + ``` |
| 40 | + |
| 41 | +4. From the command line, use Terraform to deploy the AWS resources: |
| 42 | + ``` |
| 43 | + terraform init |
| 44 | + terraform apply -var-file=your-variables.tfvars |
| 45 | + ``` |
| 46 | +
|
| 47 | + When prompted "Do you want to deploy the infrastructure", type ```yes``` and press enter. |
| 48 | +
|
| 49 | +5. Note the outputs from the terraform deployment process. These contain the resource ARNs which are used for testing. |
| 50 | +
|
| 51 | +## How it works |
| 52 | +
|
| 53 | + |
| 54 | +
|
| 55 | +1. The HTTP task in Step Functions is leveraging an EventBridge Connection. It defines the target endpoint (e.g. https://my-internal-api.company.com/customer) and HTTP method (e.g. GET) as well as eventual HTTP headers. |
| 56 | +2. The EventBridge Connection defines the authentication mechanism (OAuth, Basic or API Key in this case) for the target endpoint as well as the resource configuration to use for a private/internal endpoint. |
| 57 | +3. The resource configuration defines the target endpoint itself, generally an on-premise IP address or DNS name (e.g. my-internal-api.company.com). Resource configuration is associated to a resource gateway. |
| 58 | +4. The resource gateway "opens a door" to the VPC and allow ingress. It is linked to the chosen subnets (generally private) and is also protected by a security group to further protect your backend API. Note: You could stop here at the VPC level, with a private API deployed in a private subnet. |
| 59 | +5. The site-to-site VPN or Direct Connect connection establishes the connection between the AWS cloud (generally with a VPN Gateway or a Transit Gateway) and your datacenter (through a Customer Gateway). |
| 60 | +6. Finally, the internal API that resides in your datacenter can be accessed via this "route". |
| 61 | +
|
| 62 | +You can get more details in this [blog post](https://community.aws/content/2oExiwtkpK7go3wzAVzzF05ysqu). |
| 63 | +
|
| 64 | +## Testing |
| 65 | +
|
| 66 | +1. First make sure the EventBridge connection is active. Use the command `aws events describe-connection --name on-premise-connection --query ConnectionState` and verify it is `ACTIVE`. Otherwise, wait for an additional minute and verify again. |
| 67 | +2. Go to the AWS Step Functions console and open the state machine deployed by the example (`state-machine-call-onprem`). |
| 68 | +3. Click on `Start Execution` on the top right and again in the popup (no input is required for this example). |
| 69 | +4. Observe the result. Your on-premise API should be called by the state machine and an eventual result returned to the task. |
| 70 | +
|
| 71 | +You can also use the AWS CLI with the following command (make sure to use the output of the terraform script): |
| 72 | +
|
| 73 | +```shell |
| 74 | + aws stepfunctions start-execution --state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:state-machine-call-onprem |
| 75 | +``` |
| 76 | + |
| 77 | +## Cleanup |
| 78 | +**To avoid incurring future charges, delete the resources created by the Terraform script.** |
| 79 | +1. Return to the directory where you deployed your terraform script. |
| 80 | +2. To destroy the infrastructure in AWS, run the command |
| 81 | + |
| 82 | +```bash |
| 83 | + terraform destroy |
| 84 | +``` |
| 85 | + When prompted do you want to destroy the infrastructure, type ```yes``` and press enter. |
| 86 | + |
| 87 | +---- |
| 88 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 89 | + |
| 90 | +SPDX-License-Identifier: MIT-0 |
0 commit comments