Skip to content

Instantly share code, notes, and snippets.

@flexelem

flexelem/cost-api-stack.ts

Last active Mar 18, 2021
Embed
What would you like to do?
import {
CfnParameter, Construct, Stack, StackProps,
} from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as iam from '@aws-cdk/aws-iam';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { ApiStackProps } from './entity';
export class CostApiStack extends Stack {
constructor(scope: Construct, id: string, stage: string, props: StackProps, apiStackProps: ApiStackProps) {
super(scope, id, props);
// const cfnParameter = new CfnParameter(this, 'costApiDeploymentBucketNameParam');
const stageOptions: apigateway.StageOptions = {
stageName: 'live',
tracingEnabled: true,
dataTraceEnabled: true,
loggingLevel: apigateway.MethodLoggingLevel.INFO,
};
const restApi = new apigateway.RestApi(this, 'cost-api', {
deploy: true,
deployOptions: stageOptions,
endpointTypes: [apigateway.EndpointType.EDGE],
});
const methodOptions: apigateway.MethodOptions = {
methodResponses: [
{
statusCode: '200',
responseParameters: {
'method.response.header.Content-Type': true,
},
},
{
statusCode: '400',
responseParameters: {
'method.response.header.Content-Type': true,
},
},
],
};
const domainName = apigateway.DomainName.fromDomainNameAttributes(this, 'domainNameRef',
{
domainName: apiStackProps.domain,
domainNameAliasHostedZoneId: apiStackProps.zoneId,
domainNameAliasTarget: apiStackProps.domainNameAlias,
});
const baseMappingProps: apigateway.BasePathMappingProps = {
basePath: 'costs',
domainName,
restApi,
};
new apigateway.BasePathMapping(this, 'costBasePathMapping', baseMappingProps);
const lambdaRole = new iam.Role(this, 'costLambdaRole', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSNSFullAccess'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonS3FullAccess'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSXRayDaemonWriteAccess'),
],
});
const createCostFunction = new lambda.Function(this, 'costPostV1Function', {
role: lambdaRole,
code: lambda.Code.fromAsset('src'),
handler: 'lambda/create-cost-lambda.handler',
memorySize: 512,
runtime: lambda.Runtime.NODEJS_12_X,
});
const v1 = restApi.root.addResource('v1');
v1.addMethod('POST', new apigateway.LambdaIntegration(createCostFunction));
v1.addCorsPreflight({
allowOrigins: ['*'],
statusCode: 200,
allowMethods: ['GET', 'POST', 'DELETE', 'PUT', 'OPTIONS'],
allowHeaders: ['Content-Type', 'X-Amz-Date', 'Authorization', 'X-Api-Key',
'X-Amz-Security-Token', 'X-Requested-With', 'X-Shipper-Version'],
});
// const secret = secretsmanager.Secret.fromSecretCompleteArn(this, 'costApiSecrets', apiStackProps.secretsArn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment