import { KubectlV34Layer } from "@aws-cdk/lambda-layer-kubectl-v34"
import * as cdk from "aws-cdk-lib"
import * as ec2 from "aws-cdk-lib/aws-ec2"
import * as eks from "aws-cdk-lib/aws-eks"
import * as iam from "aws-cdk-lib/aws-iam"
import { Construct } from "constructs"
export class EksManifestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
const vpc = new ec2.Vpc(this, "EksVpc", {
maxAzs: 2,
natGateways: 0,
subnetConfiguration: [
{ name: "PublicSubnet", subnetType: ec2.SubnetType.PUBLIC },
],
})
const cluster = new eks.Cluster(this, "MyEksCluster", {
vpc,
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }],
defaultCapacity: 0,
version: eks.KubernetesVersion.V1_34,
kubectlLayer: new KubectlV34Layer(this, "KubectlLayer"),
authenticationMode: eks.AuthenticationMode.API_AND_CONFIG_MAP,
bootstrapClusterCreatorAdminPermissions: true,
})
const nodeGroup = cluster.addNodegroupCapacity("PublicNodeGroup", {
instanceTypes: [new ec2.InstanceType("t3.small")],
minSize: 1,
maxSize: 2,
subnets: { subnetType: ec2.SubnetType.PUBLIC },
amiType: eks.NodegroupAmiType.AL2023_X86_64_STANDARD,
})
const s3Policy = new iam.PolicyStatement({
actions: ["s3:PutObject", "s3:GetObject", "s3:ListBucket"],
resources: ["*"],
})
const serviceAccount = cluster.addServiceAccount("NestJsServiceAccount", {
name: "nestjs-s3-sa",
namespace: "default",
})
serviceAccount.addToPrincipalPolicy(s3Policy)
const nestJsAppResource = new eks.KubernetesManifest(
this,
"NestJsAppResources",
{
cluster,
manifest: [
{
apiVersion: "apps/v1",
kind: "Deployment",
metadata: { name: "nestjs-app", namespace: "default" },
spec: {
replicas: 1,
selector: { matchLabels: { app: "nestjs" } },
template: {
metadata: { labels: { app: "nestjs" } },
spec: {
serviceAccountName: serviceAccount.serviceAccountName,
containers: [
{
name: "nestjs-container",
image:
"758222924841.dkr.ecr.ap-southeast-1.amazonaws.com/cdk-hnb659fds-container-assets-758222924841-ap-southeast-1:35e3cfdaa7395cdc051c3a6eb6c8390d0c1ffde54debfa49d6c60865ae98a60d",
ports: [{ containerPort: 3000 }],
env: [
{ name: "REGION", value: "ap-southeast-1" },
{ name: "BUCKET", value: "bucket-public-cb2a91d6" },
],
},
],
},
},
},
},
{
apiVersion: "v1",
kind: "Service",
metadata: { name: "nestjs-service", namespace: "default" },
spec: {
type: "LoadBalancer",
selector: { app: "nestjs" },
ports: [{ protocol: "TCP", port: 80, targetPort: 3000 }],
},
},
],
},
)
nestJsAppResource.node.addDependency(nodeGroup)
const lbAddress = cluster.getServiceLoadBalancerAddress("nestjs-service", {
namespace: "default",
})
new cdk.CfnOutput(this, "LoadBalancerAddress", {
value: lbAddress,
description: "DNS to access Load Balancer",
})
new cdk.CfnOutput(this, "ClusterName", {
value: cluster.clusterName,
})
}
}
Comments
Post a Comment