-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
aws.ts
111 lines (99 loc) · 3.05 KB
/
aws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as cdk from '@aws-cdk/core';
import { ArnFormat } from '@aws-cdk/core';
import { Integration, IntegrationConfig, IntegrationOptions, IntegrationType } from '../integration';
import { Method } from '../method';
import { parseAwsApiCall } from '../util';
export interface AwsIntegrationProps {
/**
* Use AWS_PROXY integration.
*
* @default false
*/
readonly proxy?: boolean;
/**
* The name of the integrated AWS service (e.g. `s3`)
*/
readonly service: string;
/**
* A designated subdomain supported by certain AWS service for fast
* host-name lookup.
*/
readonly subdomain?: string;
/**
* The path to use for path-base APIs.
*
* For example, for S3 GET, you can set path to `bucket/key`.
* For lambda, you can set path to `2015-03-31/functions/${function-arn}/invocations`
*
* Mutually exclusive with the `action` options.
*/
readonly path?: string;
/**
* The AWS action to perform in the integration.
*
* Use `actionParams` to specify key-value params for the action.
*
* Mutually exclusive with `path`.
*/
readonly action?: string;
/**
* Parameters for the action.
*
* `action` must be set, and `path` must be undefined.
* The action params will be URL encoded.
*/
readonly actionParameters?: { [key: string]: string };
/**
* The integration's HTTP method type.
*
* @default POST
*/
readonly integrationHttpMethod?: string;
/**
* 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;
}
/**
* This type of integration lets an API expose AWS service actions. It is
* intended for calling all AWS service actions, but is not recommended for
* calling a Lambda function, because the Lambda custom integration is a legacy
* technology.
*/
export class AwsIntegration extends Integration {
private scope?: cdk.IConstruct;
constructor(props: AwsIntegrationProps) {
const backend = props.subdomain ? `${props.subdomain}.${props.service}` : props.service;
const type = props.proxy ? IntegrationType.AWS_PROXY : IntegrationType.AWS;
const { apiType, apiValue } = parseAwsApiCall(props.path, props.action, props.actionParameters);
super({
type,
integrationHttpMethod: props.integrationHttpMethod || 'POST',
uri: cdk.Lazy.string({
produce: () => {
if (!this.scope) { throw new Error('AwsIntegration must be used in API'); }
return cdk.Stack.of(this.scope).formatArn({
service: 'apigateway',
account: backend,
resource: apiType,
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
resourceName: apiValue,
region: props.region,
});
},
}),
options: props.options,
});
}
public bind(method: Method): IntegrationConfig {
const bindResult = super.bind(method);
this.scope = method;
return bindResult;
}
}