Skip to content

Commit

Permalink
feat(apigateway): integrate with aws services in a different region (#…
Browse files Browse the repository at this point in the history
…13251)

This allows the user to choose a different region for the integrated service. By default, the region is the same as the stack's.

Fixes #7009


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo authored Feb 24, 2021
1 parent 6d6aa0c commit d942699
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ book.addMethod('GET', getBookIntegration, {
});
```

It is possible to also integrate with AWS services in a different region. The following code integrates with Amazon SQS in the
`eu-west-1` region.

```ts
const getMessageIntegration = new apigateway.AwsIntegration({
service: 'sqs',
path: 'queueName',
region: 'eu-west-1'
});
```

## API Keys

The following example shows how to use an API Key with a usage plan:
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-apigateway/lib/integrations/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export interface AwsIntegrationProps {
* Integration options, such as content handling, request/response mapping, etc.
*/
readonly options?: IntegrationOptions

/**
* The region of the integrated AWS service.
*
* @default - same region as the stack
*/
readonly region?: string;
}

/**
Expand Down Expand Up @@ -87,6 +94,7 @@ export class AwsIntegration extends Integration {
resource: apiType,
sep: '/',
resourceName: apiValue,
region: props.region,
});
},
}),
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ describe('method', () => {

});

test('integration can be set for a service in the provided region', () => {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: false });

// WHEN
new apigw.Method(stack, 'my-method', {
httpMethod: 'POST',
resource: api.root,
integration: new apigw.AwsIntegration({ service: 'sqs', path: 'queueName', region: 'eu-west-1' }),
});

// THEN
expect(stack).toHaveResource('AWS::ApiGateway::Method', {
Integration: {
IntegrationHttpMethod: 'POST',
Type: 'AWS',
Uri: {
'Fn::Join': [
'',
[
'arn:', { Ref: 'AWS::Partition' }, ':apigateway:eu-west-1:sqs:path/queueName',
],
],
},
},
});
});

test('integration with a custom http method can be set via a property', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit d942699

Please sign in to comment.