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(stepfunctions-tasks): allow region override in call-rest-api task #33252

Merged
merged 9 commits into from
Feb 15, 2025

Conversation

huoandre
Copy link
Contributor

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.

(This review is outdated)

@huoandre
Copy link
Contributor 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
Contributor 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
Contributor 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
Contributor 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
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 👍

@huoandre huoandre changed the title feat(aws-stepfunctions-tasks): allow region override in call-rest-api task feat(stepfunctions-tasks): allow region override in call-rest-api task Feb 8, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review February 8, 2025 19:52

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Feb 8, 2025
@aaythapa
Copy link
Contributor

Hi @huoandre thanks for opening this PR. We have this construct that will help with cross region api calls. It does use a custom resources and provides props to configure IAM permissions, override API endpoints etc.

Wondering if this PR will replace that construct? Or customers can use that construct instead of the prop introduced in this PR.

GavinZZ
GavinZZ previously approved these changes Feb 13, 2025
Copy link
Contributor

mergify bot commented Feb 13, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@GavinZZ
Copy link
Contributor

GavinZZ commented Feb 13, 2025

Discuss with Aayush offline, and I think we can have this PR since the custom resource he mentioned is for generic AWS service API calls while this is adding a functionality to an existing call specifically for APIGateway.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Feb 13, 2025
Copy link
Contributor

mergify bot commented Feb 13, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Feb 13, 2025

This pull request has been removed from the queue for the following reason: pull request branch update failed.

The pull request can't be updated

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.

If you want to requeue this pull request, you need to post a comment with the text: @mergifyio requeue

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.00%. Comparing base (c89afe3) to head (dd2ba06).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #33252   +/-   ##
=======================================
  Coverage   81.00%   81.00%           
=======================================
  Files         238      238           
  Lines       14271    14271           
  Branches     2492     2492           
=======================================
  Hits        11560    11560           
  Misses       2425     2425           
  Partials      286      286           
Flag Coverage Δ
suite.unit 81.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 79.92% <ø> (ø)
packages/aws-cdk-lib/core 82.16% <ø> (ø)

@aaythapa
Copy link
Contributor

@mergify requeue

Copy link
Contributor

mergify bot commented Feb 13, 2025

requeue

✅ The queue state of this pull request has been cleaned. It can be re-embarked automatically

Copy link
Contributor

mergify bot commented Feb 13, 2025

This pull request has been removed from the queue for the following reason: pull request branch update failed.

The pull request can't be updated

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.

If you want to requeue this pull request, you need to post a comment with the text: @mergifyio requeue

@aaythapa
Copy link
Contributor

@mergify update

Copy link
Contributor

mergify bot commented Feb 14, 2025

update

❌ Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/analytics-metadata-updater.yml without workflows permission

@aaythapa
Copy link
Contributor

@mergify update

Copy link
Contributor

mergify bot commented Feb 14, 2025

update

☑️ Nothing to do

  • #commits-behind > 0 [📌 update requirement]
  • -closed [📌 update requirement]
  • -conflict [📌 update requirement]
  • queue-position = -1 [📌 update requirement]

@mergify mergify bot dismissed GavinZZ’s stale review February 14, 2025 00:45

Pull request has been modified.

Copy link
Contributor

mergify bot commented Feb 14, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Feb 14, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@mergify mergify bot merged commit 59470c5 into aws:main Feb 15, 2025
21 checks passed
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2025
@huoandre huoandre deleted the dev-huoandre branch February 21, 2025 22:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

5 participants