# ApiGatewayLoadBalancedFargateService
Amazon API Gateway allows you to integrate with VPC link and route traffic to services in VPC through Cloud Map, Application Load Balancers(ALB) or Network Load Balancers(NLB).
This constructs provisions API Gateway HTTP API with Fargate service and route the traffic throught a VPC link. By default, the `vpcLinkIntegration` type is Cloud Map and no ALB or NLB will be created.
The default capacity provider strategy for the service is:
```ts
[
{ capacityProvider: 'FARGATE_SPOT', base: 2, weight: 50 },
{ capacityProvider: 'FARGATE', weight: 50 },
]
```
This means the first 2 tasks will always be `FARGATE_SPOT` and 50-50 afterwards. You can customize that in the `capacityProviderStrategies` property.
## Example
```ts
const vpc = new ec2.Vpc(stack, 'Vpc', { natGateways: 1 });
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'Task', {
memoryLimitMiB: 512,
cpu: 256,
});
taskDefinition.addContainer('nyancat', {
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/pahudnet/nyancat-docker-image:latest'),
portMappings: [{ containerPort: 80, name: 'default' }],
healthCheck: {
command: ['CMD-SHELL', 'curl -f http://localhost/ || exit 1'],
},
});
new ApiGatewayLoadBalancedFargateService(stack, 'DemoService', {
vpc,
cluster,
taskDefinition,
vpcLinkIntegration: VpcLinkIntegration.CLOUDMAP,
});
```
## Deploy the sample workload
```ts
$ yarn install
$ npx cdk diff
$ npx cdk deploy
```
## Destroy the sample workload
```ts
$ npx cdk destroy
```
# API Reference
## Constructs
### ApiGatewayLoadBalancedFargateService
#### Initializers
```typescript
import { ApiGatewayLoadBalancedFargateService } from 'ecs-fargate-apigateway-cloudmap-cdk'
new ApiGatewayLoadBalancedFargateService(scope: Construct, id: string, props: ApiGatewayLoadBalancedFargateServiceProps)
```
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| scope
| constructs.Construct
| *No description.* |
| id
| string
| *No description.* |
| props
| ApiGatewayLoadBalancedFargateServiceProps
| *No description.* |
---
##### `scope`Required
- *Type:* constructs.Construct
---
##### `id`Required
- *Type:* string
---
##### `props`Required
- *Type:* ApiGatewayLoadBalancedFargateServiceProps
---
#### Methods
| **Name** | **Description** |
| --- | --- |
| toString
| Returns a string representation of this construct. |
---
##### `toString`
```typescript
public toString(): string
```
Returns a string representation of this construct.
#### Static Functions
| **Name** | **Description** |
| --- | --- |
| isConstruct
| Checks if `x` is a construct. |
---
##### ~~`isConstruct`~~
```typescript
import { ApiGatewayLoadBalancedFargateService } from 'ecs-fargate-apigateway-cloudmap-cdk'
ApiGatewayLoadBalancedFargateService.isConstruct(x: any)
```
Checks if `x` is a construct.
###### `x`Required
- *Type:* any
Any object.
---
#### Properties
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| node
| constructs.Node
| The tree node. |
---
##### `node`Required
```typescript
public readonly node: Node;
```
- *Type:* constructs.Node
The tree node.
---
## Structs
### ApiGatewayLoadBalancedFargateServiceProps
#### Initializer
```typescript
import { ApiGatewayLoadBalancedFargateServiceProps } from 'ecs-fargate-apigateway-cloudmap-cdk'
const apiGatewayLoadBalancedFargateServiceProps: ApiGatewayLoadBalancedFargateServiceProps = { ... }
```
#### Properties
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| cluster
| aws-cdk-lib.aws_ecs.ICluster
| The ECS Cluster. |
| taskDefinition
| aws-cdk-lib.aws_ecs.TaskDefinition
| The ECS task definition of the Fargate service. |
| vpc
| aws-cdk-lib.aws_ec2.IVpc
| The VPC for the ECS cluster. |
| capacityProviderStrategies
| aws-cdk-lib.aws_ecs.CapacityProviderStrategy[]
| The capacity provider strategies for the service. |
| desiredCount
| number
| The desired number of instantiations of the task definition to keep running on the service. |
| discoveryName
| string
| The discovery name of the cloud map service. |
| vpcLinkIntegration
| VpcLinkIntegration
| The vpc link integration type for the API Gateway private integration. |
| vpcSubnets
| aws-cdk-lib.aws_ec2.SubnetSelection
| subnets for the ecs tasks. |
---
##### `cluster`Required
```typescript
public readonly cluster: ICluster;
```
- *Type:* aws-cdk-lib.aws_ecs.ICluster
The ECS Cluster.
---
##### `taskDefinition`Required
```typescript
public readonly taskDefinition: TaskDefinition;
```
- *Type:* aws-cdk-lib.aws_ecs.TaskDefinition
The ECS task definition of the Fargate service.
---
##### `vpc`Required
```typescript
public readonly vpc: IVpc;
```
- *Type:* aws-cdk-lib.aws_ec2.IVpc
The VPC for the ECS cluster.
---
##### `capacityProviderStrategies`Optional
```typescript
public readonly capacityProviderStrategies: CapacityProviderStrategy[];
```
- *Type:* aws-cdk-lib.aws_ecs.CapacityProviderStrategy[]
- *Default:* [ { capacityProvider: 'FARGATE_SPOT', base: 2, weight: 50 }, { capacityProvider: 'FARGATE', weight: 50 }, ];
The capacity provider strategies for the service.
---
##### `desiredCount`Optional
```typescript
public readonly desiredCount: number;
```
- *Type:* number
- *Default:* When creating the service, default is 1; when updating the service, default uses the current task number.
The desired number of instantiations of the task definition to keep running on the service.
---
##### `discoveryName`Optional
```typescript
public readonly discoveryName: string;
```
- *Type:* string
- *Default:* 'default'
The discovery name of the cloud map service.
---
##### `vpcLinkIntegration`Optional
```typescript
public readonly vpcLinkIntegration: VpcLinkIntegration;
```
- *Type:* VpcLinkIntegration
- *Default:* VpcLinkIntegration.CLOUDMAP;
The vpc link integration type for the API Gateway private integration.
---
##### `vpcSubnets`Optional
```typescript
public readonly vpcSubnets: SubnetSelection;
```
- *Type:* aws-cdk-lib.aws_ec2.SubnetSelection
- *Default:* Public subnets if `assignPublicIp` is set, otherwise the first available one of Private, Isolated, Public, in that order.
subnets for the ecs tasks.
---
## Classes
### CloudMapIntegration
#### Initializers
```typescript
import { CloudMapIntegration } from 'ecs-fargate-apigateway-cloudmap-cdk'
new CloudMapIntegration(cloudMapServiceArn: string, vpcLinkId: string, name?: string)
```
| **Name** | **Type** | **Description** |
| --- | --- | --- |
| cloudMapServiceArn
| string
| *No description.* |
| vpcLinkId
| string
| *No description.* |
| name
| string
| *No description.* |
---
##### `cloudMapServiceArn`Required
- *Type:* string
---
##### `vpcLinkId`Required
- *Type:* string
---
##### `name`Optional
- *Type:* string
---
#### Methods
| **Name** | **Description** |
| --- | --- |
| bind
| Bind this integration to the route. |
---
##### `bind`
```typescript
public bind(_: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig
```
Bind this integration to the route.
###### `_`Required
- *Type:* @aws-cdk/aws-apigatewayv2-alpha.HttpRouteIntegrationBindOptions
---
## Enums
### VpcLinkIntegration
The vpc link integration type for the API Gateway private integration through the VPC Link.
#### Members
| **Name** | **Description** |
| --- | --- |
| CLOUDMAP
| *No description.* |
| NLB
| *No description.* |
| ALB
| *No description.* |
---
##### `CLOUDMAP`
---
##### `NLB`
---
##### `ALB`
---