Skip to content
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

feat(aws-stepfunctions-tasks): allow region override in call-rest-api task #33252

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

huoandre
Copy link

Issue # (if applicable)

#26509

Reason for this change

AWS Step Functions has the functionality to call APIs in different regions according to the AWS blog: "You can extend this architecture to run workflows across multiple Regions or accounts." However, CDK syntax doesn't support it. This change will help expand the functionality of CDK to be able to call APIs where the API endpoint is not in the same region as the stack it's contained in (such as calling an API in a different AWS account).

Description of changes

This PR implements the solution suggested by pahud, which is to add an optional region parameter to the API endpoint getter (and to props to provide it).

Adding region to IRestApi was another option, but this would not be backwards-compatible (how would existing IRestApis determine the region?).

Ideally, I believe some Region enum would be superior to type string for region, but I looked around and couldn't find any other examples in the codebase and besides it might introduce coupling/dependency that isn't necessary. Instead, an invalid region such as "us-north-42" is likely to simply throw an exception for invalid API endpoint, which should expose the problem to the dev.

This change supports an extra use-case of calling API endpoints in regions other than the region of the stack in which the API construct is defined. This uses AWS features of Step Functions invoking API Gateway endpoints in different regions.

Describe any new or updated permissions being added

None

Description of how you validated changes

Added a unit test with a hardcoded us-west-2 in the style of other surrounding unit tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Jan 31, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team January 31, 2025 05:22
@github-actions github-actions bot added the p2 label Jan 31, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ The title scope of the pull request should omit 'aws-' from the name of modified packages. Use 'stepfunctions-tasks' instead of 'aws-stepfunctions-tasks'.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

@huoandre
Copy link
Author

Clarification Request: This change seems small enough that a README.md update and an integration test update seem a little excessive. Could this PR be reclassified as a fix instead of a feat? Or does a change this small still require a README.md update and an integration test update?

@aws-cdk-automation aws-cdk-automation added the pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run label Jan 31, 2025
@huoandre
Copy link
Author

huoandre commented Jan 31, 2025

Just pushed a new commit adding the README.md update, the integration test (with snapshot), and fixing a unit test linting issue (double empty lines) that led to the CodeBuild failure. Apologies, out of habit I ran git commit --amend with an unchanged commit message, and now there are two commits in my PR(?). Would appreciate any advice on fixing that if necessary.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 31, 2025
@huoandre
Copy link
Author

3rd commit out -- didn't actually integration test properly in the 2nd commit. Let me know if I need to squash commits and then force push the combined commit to my dev branch or anything like that.

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 Left comments for small docs/test adjustments

Comment on lines 24 to 26
* AWS region, e.g. 'us-east-1', where the API is deployed. Uses the region of the Stack
* containing `api`if no region is provided.
* @default the region of the Stack of the `api` in these props
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* AWS region, e.g. 'us-east-1', where the API is deployed. Uses the region of the Stack
* containing `api`if no region is provided.
* @default the region of the Stack of the `api` in these props
* Specify a custom Region where the API is deployed, e.g. 'us-east-1'.
*
* @default - Uses the Region of the stack containing the `api`.

Comment on lines 159 to 172
the stack in which the provided `api` is created. Passing in a `region` string,
such as `us-west-2`, will instead construct the endpoint with the given region:

```ts
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
const restApi = new apigateway.RestApi(this, 'MyRestApi');
const endpointRegion = 'us-west-2';

const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(this, 'Call REST API', {
api: restApi,
stageName: 'prod',
method: tasks.HttpMethod.GET,
region: endpointRegion,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the stack in which the provided `api` is created. Passing in a `region` string,
such as `us-west-2`, will instead construct the endpoint with the given region:
```ts
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
const restApi = new apigateway.RestApi(this, 'MyRestApi');
const endpointRegion = 'us-west-2';
const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(this, 'Call REST API', {
api: restApi,
stageName: 'prod',
method: tasks.HttpMethod.GET,
region: endpointRegion,
});
the stack in which the provided `api` is created.
To construct the endpoint with a different region, use the `region` parameter:
```ts
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
const restApi = new apigateway.RestApi(this, 'MyRestApi');
const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(this, 'Call REST API', {
api: restApi,
stageName: 'prod',
method: tasks.HttpMethod.GET,
region: 'us-west-2',
});

restApi.root.addMethod('ANY', hello);

const importedRestApi = apigateway.RestApi.fromRestApiId(sfnStack, 'CrossStackReferenceToApi', restApi.restApiId);
const callEndpointJob = new CallApiGatewayRestApiEndpoint(sfnStack, 'Call APIGW', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please specify another endpoint job using the custom region parameter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch... I didn't realize the integ test could succeed without the parameter. Does running yarn integ only test whether it's a valid snapshot? The SFN execution itself should have failed, but I wasn't sure how to check that. Either way, should have a commit out soon with the custom region parameter.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 3, 2025
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 7639773
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants