Last active
December 13, 2019 17:12
-
-
Save katryo/86724cfc6b87ea7aa7da644beb937e89 to your computer and use it in GitHub Desktop.
api-gateway-with-existing-domain-and-lambda-stack.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cdk = require("@aws-cdk/core"); | |
import apigateway = require("@aws-cdk/aws-apigateway"); | |
import lambda = require("@aws-cdk/aws-lambda"); | |
interface MultistackProps extends cdk.StackProps { | |
stage: "staging" | "production"; | |
functionArn: string; | |
domainName: string; | |
domainNameAliasHostedZoneId: string; | |
domainNameAliasTarget: string; | |
} | |
export class MuaFrontendApiGatewayStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, props: MultistackProps) { | |
super(scope, id, props); | |
const domain = apigateway.DomainName.fromDomainNameAttributes( | |
this, | |
`existing-domain-${props.stage}`, | |
{ | |
domainName: props.domainName, | |
domainNameAliasHostedZoneId: props.domainNameAliasHostedZoneId, | |
domainNameAliasTarget: props.domainNameAliasTarget | |
} | |
); | |
const api = new apigateway.RestApi(this, `api-gateway-${props.stage}`, { | |
restApiName: `api-gateway-${props.stage}` | |
}); | |
new apigateway.BasePathMapping(this, `basePathMapping-${props.stage}`, { | |
domainName: domain, | |
restApi: api | |
}); | |
const lambdaOne = lambda.Function.fromFunctionArn( | |
this, | |
`lambdaOne-${props.stage}`, | |
props.functionArn | |
); | |
const lambdaIntegration = new apigateway.LambdaIntegration(lambdaOne, { | |
contentHandling: apigateway.ContentHandling.CONVERT_TO_BINARY | |
}); | |
const rootProxy = api.root.addProxy({ | |
anyMethod: false | |
}); | |
const methodOne = rootProxy.addMethod("ANY", lambdaIntegration); | |
new lambda.CfnPermission(this, `permission-for-lambda-${props.stage}`, { | |
action: "lambda:invokeFunction", | |
principal: "apigateway.amazonaws.com", | |
functionName: lambdaOne.functionArn, | |
sourceArn: methodOne.methodArn.toString() | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment