Skip to main content
Version: Next

Running APISIX in AWS with AWS CDK

APISIX is a cloud-native microservices API gateway, delivering the ultimate performance, security, open source and scalable platform for all your APIs and microservices.


This reference architecture walks you through building APISIX as a serverless container API Gateway on top of AWS Fargate with AWS CDK.

Apache APISIX Serverless Architecture
Click to Preview

Generate an AWS CDK project with projen#

$ mkdir apisix-aws$ cd $_$ npx projen new awscdk-app-ts

update the .projenrc.js with the following content:

const { AwsCdkTypeScriptApp } = require('projen');
const project = new AwsCdkTypeScriptApp({  cdkVersion: "1.70.0",  name: "apisix-aws",  cdkDependencies: [    '@aws-cdk/aws-ec2',    '@aws-cdk/aws-ecs',    '@aws-cdk/aws-ecs-patterns',  ]});

update the project:

$ npx projen

update src/main.ts#

import * as cdk from '@aws-cdk/core';import { Vpc, Port } from '@aws-cdk/aws-ec2';import { Cluster, ContainerImage, TaskDefinition, Compatibility } from '@aws-cdk/aws-ecs';import { ApplicationLoadBalancedFargateService, NetworkLoadBalancedFargateService } from '@aws-cdk/aws-ecs-patterns';
export class ApiSixStack extends cdk.Stack {  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {    super(scope, id, props);
    const vpc = Vpc.fromLookup(this, 'VPC', {      isDefault: true    })
    const cluster = new Cluster(this, 'Cluster', {      vpc    })
    /**     * ApiSix service     */    const taskDefinition = new TaskDefinition(this, 'TaskApiSix', {      compatibility: Compatibility.FARGATE,      memoryMiB: '512',      cpu: '256'    })
    taskDefinition      .addContainer('apisix', {        image: ContainerImage.fromRegistry('iresty/apisix'),      })      .addPortMappings({        containerPort: 9080      })
    taskDefinition      .addContainer('etcd', {        image: ContainerImage.fromRegistry(''),        // image: ContainerImage.fromRegistry(''),      })      .addPortMappings({        containerPort: 2379      })
    const svc = new ApplicationLoadBalancedFargateService(this, 'ApiSixService', {      cluster,      taskDefinition,    })
    svc.targetGroup.setAttribute('deregistration_delay.timeout_seconds', '30')    svc.targetGroup.configureHealthCheck({      interval: cdk.Duration.seconds(5),      healthyHttpCodes: '404',      healthyThresholdCount: 2,      unhealthyThresholdCount: 3,      timeout: cdk.Duration.seconds(4)    })
    /**     * PHP service     */    const taskDefinitionPHP = new TaskDefinition(this, 'TaskPHP', {      compatibility: Compatibility.FARGATE,      memoryMiB: '512',      cpu: '256'    })
    taskDefinitionPHP      .addContainer('php', {        image: ContainerImage.fromRegistry('abiosoft/caddy:php'),      })      .addPortMappings({        containerPort: 2015      })
    const svcPHP = new NetworkLoadBalancedFargateService(this, 'PhpService', {      cluster,      taskDefinition: taskDefinitionPHP,      assignPublicIp: true,    })
    // allow Fargate task behind NLB to accept all traffic    svcPHP.service.connections.allowFromAnyIpv4(Port.tcp(2015))    svcPHP.targetGroup.setAttribute('deregistration_delay.timeout_seconds', '30')    svcPHP.loadBalancer.setAttribute('load_balancing.cross_zone.enabled', 'true')
    new cdk.CfnOutput(this, 'ApiSixDashboardURL', {      value: `http://${svc.loadBalancer.loadBalancerDnsName}/apisix/dashboard/`    })  }}
const devEnv = {  account: process.env.CDK_DEFAULT_ACCOUNT,  region: process.env.CDK_DEFAULT_REGION,};
const app = new cdk.App();
new ApiSixStack(app, 'apisix-stack-dev', { env: devEnv });

Deploy the APISIX Stack with AWS CDK#

$ cdk diff$ cdk deploy

On deployment complete, some outputs will be returned:

Outputs:apiSix.PhpServiceLoadBalancerDNS5E5BAB1B = = = =

Open the apiSix.ApiSixDashboardURL from your browser and you will see the login prompt.

Configure the upstream nodes#

All upstream nodes are running as AWS Fargate tasks and registered to the NLB(Network Load Balancer) exposing multiple static IP addresses. We can query the IP addresses by nslookup the apiSix.PhpServiceLoadBalancerDNS5E5BAB1B like this:

$ nslookup
Non-authoritative answer:Name:

Configure the IP addresses returned as your upstream nodes in your APISIX dashboard followed by the Services and Routes configuration. Let's say we have a /index.php as the URI for the first route for our first Service from the Upstream IP addresses.

upstream with AWS NLB IP addresses
Click to Preview
service with created upstream
Click to Preview
define route with service and uri
Click to Preview


OK. Let's test the /index.php on {apiSix.ApiSixServiceServiceURL}/index.php

Testing Apache APISIX on AWS Fargate
Click to Preview

Now we have been successfully running APISIX in AWS Fargate as serverless container API Gateway service.

Clean up#

$ cdk destroy

Running APISIX in AWS China Regions#

update src/main.ts

  taskDefinition    .addContainer('etcd', {      image: ContainerImage.fromRegistry(''),      // image: ContainerImage.fromRegistry(''),    })    .addPortMappings({      containerPort: 2379    })

(read here for more reference)

Run cdk deploy and specify your preferred AWS region in China.

# let's say we have another AWS_PROFILE for China regions called 'cn'# make sure you have aws configure --profile=cn properly.## deploy to NingXia region$ cdk deploy --profile cn -c region=cn-northwest-1# deploy to Beijing region$ cdk deploy --profile cn -c region=cn-north-1

In the following case, we got the Outputs returned for AWS Ningxia region(cn-northwest-1):

Outputs:apiSix.PhpServiceLoadBalancerDNS5E5BAB1B = = = =

Open the apiSix.ApiSixDashboardURL URL and log in to configure your APISIX in AWS China region.


Decouple APISIX and etcd3 on AWS#

For high availability and state consistency consideration, you might be interested to decouple the etcd3 as a separate cluster from APISIX not only for performance but also high availability and fault tolerance yet with highly reliable state consistency.