Skip to content

Instantly share code, notes, and snippets.

@Gaafar
Created March 8, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gaafar/a2a47cf10543ef97826d988ea667d4b2 to your computer and use it in GitHub Desktop.
Save Gaafar/a2a47cf10543ef97826d988ea667d4b2 to your computer and use it in GitHub Desktop.
cdk-api-gateway-split-stacks
import { RestApi, Cors, CfnResource, CfnMethod, AuthorizationType } from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';
import { Stack, App, StackProps } from '@aws-cdk/core';
import { NestedStack } from '@aws-cdk/aws-cloudformation';
const name = 'stackName';
export class Methods extends Stack {
constructor(app: App, id: string, props: StackProps) {
super(app, id, props);
const restApi = new RestApi(this, name, {
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
},
});
const root = restApi.root.addResource('hashtags');
const defaultLambdaProps = {
code: new lambda.AssetCode('lambda'),
runtime: lambda.Runtime.NODEJS_10_X,
memorySize: 256,
tracing: lambda.Tracing.ACTIVE,
};
// tslint:disable-next-line:no-function-constructor-with-string-args
const lambdaFunction = new lambda.Function(
this,
'lambdaFunction',
{
...defaultLambdaProps,
handler: 'get-hashtags-from-keywords.handler',
});
const methodsStack = new NestedStack(this, 'methodsStack');
const singleItem = new CfnResource(methodsStack, 'id', {
restApiId: root.restApi.restApiId,
pathPart: '{keywords}',
parentId: root.resourceId,
});
new CfnMethod(methodsStack, 'method1', {
httpMethod: 'GET',
restApiId: root.restApi.restApiId,
resourceId: root.resourceId,
authorizationType: AuthorizationType.NONE,
integration: {
integrationHttpMethod: 'POST',
type: 'AWS_PROXY',
uri: `arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${lambdaFunction.functionArn}/invocations`,
},
});
new CfnMethod(methodsStack, 'method2', {
httpMethod: 'POST',
restApiId: root.restApi.restApiId,
resourceId: singleItem.ref,
authorizationType: AuthorizationType.NONE,
integration: {
integrationHttpMethod: 'POST',
type: 'AWS_PROXY',
uri: `arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${lambdaFunction.functionArn}/invocations`,
},
});
new CfnMethod(methodsStack, 'method3', {
httpMethod: 'DELETE',
restApiId: root.restApi.restApiId,
resourceId: singleItem.ref,
authorizationType: AuthorizationType.NONE,
integration: {
integrationHttpMethod: 'DELETE',
type: 'AWS_PROXY',
uri: `arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${lambdaFunction.functionArn}/invocations`,
},
});
// this is required to trigger deployment update, but may not include all changes
restApi.latestDeployment?.addToLogicalId(Date.now());
// this is required for creation and to wait for all changes before updating deployment
restApi.latestDeployment?.node.addDependency(methodsStack);
}
}
new Methods(new App(), 'MethodsStack', { stackName: name });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment