|
| 1 | +import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; |
| 2 | +import { IRestApi } from 'aws-cdk-lib/aws-apigateway'; |
| 3 | +import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam'; |
| 4 | + |
| 5 | +/** |
| 6 | + * API Gateway REST API target properties. |
| 7 | + */ |
| 8 | +export interface ApiGatewayTargetParameters { |
| 9 | + /** |
| 10 | + * The input transformation to apply to the message before sending it to the target. |
| 11 | + * |
| 12 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate |
| 13 | + * @default - none |
| 14 | + */ |
| 15 | + readonly inputTransformation?: IInputTransformation; |
| 16 | + |
| 17 | + /** |
| 18 | + * The method for API Gateway resource. |
| 19 | + * |
| 20 | + * @default '*' - ANY |
| 21 | + */ |
| 22 | + readonly method?: string; |
| 23 | + |
| 24 | + /** |
| 25 | + * The path for the API Gateway resource. |
| 26 | + * |
| 27 | + * @default '/' |
| 28 | + */ |
| 29 | + readonly path?: string; |
| 30 | + |
| 31 | + /** |
| 32 | + * The deployment stage for the API Gateway resource. |
| 33 | + * |
| 34 | + * @default - the value of `deploymentStage.stageName` of target API Gateway resource. |
| 35 | + */ |
| 36 | + readonly stage?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * The headers to send as part of the request invoking the API Gateway REST API. |
| 40 | + * |
| 41 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargethttpparameters.html#cfn-pipes-pipe-pipetargethttpparameters-headerparameters |
| 42 | + * @default - none |
| 43 | + */ |
| 44 | + readonly headerParameters?: Record<string, string>; |
| 45 | + |
| 46 | + /** |
| 47 | + * The path parameter values used to populate the API Gateway REST API path wildcards ("*"). |
| 48 | + * |
| 49 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargethttpparameters.html#cfn-pipes-pipe-pipetargethttpparameters-pathparametervalues |
| 50 | + * @default - none |
| 51 | + */ |
| 52 | + readonly pathParameterValues?: string[]; |
| 53 | + |
| 54 | + /** |
| 55 | + * The query string keys/values that need to be sent as part of request invoking the API Gateway REST API. |
| 56 | + * |
| 57 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargethttpparameters.html#cfn-pipes-pipe-pipetargethttpparameters-querystringparameters |
| 58 | + * @default - none |
| 59 | + */ |
| 60 | + readonly queryStringParameters?: Record<string, string>; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * An EventBridge Pipes target that sends messages to an EventBridge API destination. |
| 65 | + */ |
| 66 | +export class ApiGatewayTarget implements ITarget { |
| 67 | + private restApi: IRestApi; |
| 68 | + private restApiParameters?: ApiGatewayTargetParameters; |
| 69 | + private restApiArn: string; |
| 70 | + public readonly targetArn; |
| 71 | + |
| 72 | + constructor(restApi: IRestApi, parameters?: ApiGatewayTargetParameters) { |
| 73 | + this.restApi = restApi; |
| 74 | + this.restApiParameters = parameters; |
| 75 | + |
| 76 | + if (this.restApiParameters?.stage === undefined && this.restApi.deploymentStage === undefined) { |
| 77 | + throw Error('The REST API must have a deployed stage.'); |
| 78 | + } |
| 79 | + |
| 80 | + this.restApiArn = this.restApi.arnForExecuteApi( |
| 81 | + this.restApiParameters?.method, |
| 82 | + this.restApiParameters?.path || '/', |
| 83 | + this.restApiParameters?.stage || this.restApi.deploymentStage.stageName, |
| 84 | + ); |
| 85 | + |
| 86 | + this.targetArn = this.restApiArn; |
| 87 | + } |
| 88 | + |
| 89 | + grantPush(grantee: IRole): void { |
| 90 | + grantee.addToPrincipalPolicy(new PolicyStatement({ |
| 91 | + resources: [this.restApiArn], |
| 92 | + actions: ['execute-api:Invoke'], |
| 93 | + })); |
| 94 | + } |
| 95 | + |
| 96 | + bind(pipe: IPipe): TargetConfig { |
| 97 | + if (!this.restApiParameters) { |
| 98 | + return { |
| 99 | + targetParameters: {}, |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + targetParameters: { |
| 105 | + inputTemplate: this.restApiParameters.inputTransformation?.bind(pipe).inputTemplate, |
| 106 | + httpParameters: this.restApiParameters, |
| 107 | + }, |
| 108 | + }; |
| 109 | + } |
| 110 | +} |
0 commit comments