DEV Community


Posted on • Updated on

An example of AWS CDK that creates an API Gateway with existing Lambda and Domain

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(
        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(
    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: "",
      functionName: lambdaOne.functionArn,
      sourceArn: methodOne.methodArn.toString()

Top comments (1)

mokus profile image
mokus • Edited

What is domainNameAliasTarget and where do you get it?