|
| 1 | +import * as cdk from "aws-cdk-lib"; |
| 2 | +import * as ec2 from "aws-cdk-lib/aws-ec2"; |
| 3 | +import * as ecs from "aws-cdk-lib/aws-ecs"; |
| 4 | +import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2"; |
| 5 | +import * as ecr from "aws-cdk-lib/aws-ecr"; |
| 6 | +import * as logs from "aws-cdk-lib/aws-logs"; |
| 7 | +import { Tags } from "aws-cdk-lib"; |
| 8 | + |
| 9 | +export interface BootstrapEcsProps { |
| 10 | + appName: string; |
| 11 | + environment: string; |
| 12 | + region: string; |
| 13 | + vpcId?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export class BootstrapEcsStack extends cdk.Stack { |
| 17 | + constructor(scope: cdk.App, id: string, props: BootstrapEcsProps) { |
| 18 | + super(scope, id, { env: { region: props.region } }); |
| 19 | + |
| 20 | + Tags.of(this).add("cicd:app", props.appName); |
| 21 | + Tags.of(this).add("cicd:env", props.environment); |
| 22 | + Tags.of(this).add("cicd:managed-by", "aws-cicd-skill"); |
| 23 | + |
| 24 | + const vpc = props.vpcId |
| 25 | + ? ec2.Vpc.fromLookup(this, "Vpc", { vpcId: props.vpcId }) |
| 26 | + : new ec2.Vpc(this, "Vpc", { maxAzs: 2, natGateways: 1 }); |
| 27 | + |
| 28 | + const repo = new ecr.Repository(this, "EcrRepo", { |
| 29 | + repositoryName: `${props.appName}-${props.environment}`, |
| 30 | + imageScanOnPush: true, |
| 31 | + }); |
| 32 | + |
| 33 | + const cluster = new ecs.Cluster(this, "Cluster", { |
| 34 | + vpc, |
| 35 | + clusterName: `${props.appName}-${props.environment}`, |
| 36 | + containerInsights: true, |
| 37 | + }); |
| 38 | + |
| 39 | + const taskDef = new ecs.FargateTaskDefinition(this, "TaskDef", { |
| 40 | + cpu: 512, |
| 41 | + memoryLimitMiB: 1024, |
| 42 | + }); |
| 43 | + taskDef.addContainer("App", { |
| 44 | + image: ecs.ContainerImage.fromEcrRepository(repo, "latest"), |
| 45 | + logging: ecs.LogDrivers.awsLogs({ |
| 46 | + streamPrefix: props.appName, |
| 47 | + logRetention: logs.RetentionDays.ONE_MONTH, |
| 48 | + }), |
| 49 | + portMappings: [{ containerPort: 8080 }], |
| 50 | + }); |
| 51 | + |
| 52 | + const service = new ecs.FargateService(this, "Service", { |
| 53 | + cluster, |
| 54 | + taskDefinition: taskDef, |
| 55 | + desiredCount: 1, |
| 56 | + assignPublicIp: true, |
| 57 | + }); |
| 58 | + |
| 59 | + const alb = new elbv2.ApplicationLoadBalancer(this, "Alb", { vpc, internetFacing: true }); |
| 60 | + const listener = alb.addListener("Http", { port: 80, open: true }); |
| 61 | + const tg = listener.addTargets("EcsTargets", { |
| 62 | + port: 8080, |
| 63 | + targets: [service], |
| 64 | + healthCheck: { path: "/health" }, |
| 65 | + }); |
| 66 | + |
| 67 | + new cdk.CfnOutput(this, "ClusterName", { value: cluster.clusterName }); |
| 68 | + new cdk.CfnOutput(this, "TargetGroupArn", { value: tg.targetGroupArn }); |
| 69 | + new cdk.CfnOutput(this, "EcrRepositoryUri", { value: repo.repositoryUri }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const app = new cdk.App(); |
| 74 | +const appName = app.node.tryGetContext("appName") ?? "my-app"; |
| 75 | +const environment = app.node.tryGetContext("environment") ?? "dev"; |
| 76 | +const region = app.node.tryGetContext("region") ?? "us-east-1"; |
| 77 | + |
| 78 | +new BootstrapEcsStack(app, "BootstrapEcs", { |
| 79 | + appName, |
| 80 | + environment, |
| 81 | + region, |
| 82 | + vpcId: app.node.tryGetContext("vpcId"), |
| 83 | +}); |
0 commit comments