Skip to content

Instantly share code, notes, and snippets.

@btakita
Last active August 16, 2020 05:20
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 btakita/4f5c36b0b26092396709f22919478783 to your computer and use it in GitHub Desktop.
Save btakita/4f5c36b0b26092396709f22919478783 to your computer and use it in GitHub Desktop.
import { CfnOutput } from '@aws-cdk/core';
import { _b } from '@ctx-core/object';
import { ctx_type } from './ctx_type';
import { host_b } from './host_b';
import { lambdaVersion_b } from './lambdaVersion_b';
import { lambdaFunction_b } from './lambdaFunction_b';
import {
AwsIntegration,
LambdaIntegration, LambdaRestApi, MethodLoggingLevel,
} from '@aws-cdk/aws-apigateway';
import { assets_bucket_b } from './assets_bucket_b';
import { assets_role_b } from './assets_role_b';
export const api_b = _b(
'api', (ctx: ctx_type) => {
const { construct } = ctx;
const host = host_b(ctx);
const lambdaFunction = lambdaFunction_b(ctx);
const lambdaVersion = lambdaVersion_b(ctx);
const assets_bucket = assets_bucket_b(ctx);
const assets_role = assets_role_b(ctx);
const api = new LambdaRestApi(construct, `${host}-api`, {
handler: lambdaVersion.latestVersion,
proxy: false,
deploy: true,
deployOptions: {
loggingLevel: MethodLoggingLevel.INFO,
dataTraceEnabled: true,
},
// The first value is ignored & the ~1 is an escaped /
binaryMediaTypes: [
'*/*', '*~1*',
],
});
const assets_integration = new AwsIntegration({
service: 's3',
path: `${assets_bucket.bucketName}`,
options: {
credentialsRole: assets_role,
},
});
const client_integration = new AwsIntegration({
service: 's3',
path: `${assets_bucket.bucketName}`,
options: {
credentialsRole: assets_role,
},
});
const lambda_integration = new LambdaIntegration(lambdaFunction, {
proxy: true,
});
api.root.addMethod('ANY', lambda_integration);
api.root.addResource('assets').addResource('{proxy+}')
.addMethod('ANY', assets_integration);
api.root.addResource('client').addResource('{proxy+}')
.addMethod('ANY', client_integration);
api.root.addResource('{proxy+}')
.addMethod('ANY', lambda_integration);
new CfnOutput(construct, 'api.url', {
value: api.url
});
return api;
});
import { _b } from '@ctx-core/object';
import { ctx_type } from './ctx_type';
import { Role, ServicePrincipal } from '@aws-cdk/aws-iam';
export const assets_role_b = _b(
'assets_role', (ctx: ctx_type) => {
const { construct } = ctx;
const assets_role = new Role(construct, 'assets-role', {
assumedBy: new ServicePrincipal('apigateway.amazonaws.com'),
});
return assets_role;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment