|
1 | | -import * as cdk from 'aws-cdk-lib' |
2 | | -import {DockerImageAsset, Platform} from 'aws-cdk-lib/aws-ecr-assets' |
3 | | -import {Construct} from 'constructs' |
4 | | -// import * as sqs from 'aws-cdk-lib/aws-sqs'; |
5 | | -import * as ec2 from "aws-cdk-lib/aws-ec2" |
6 | | -import * as ecs from "aws-cdk-lib/aws-ecs" |
7 | | -import * as ecs_patterns from "aws-cdk-lib/aws-ecs-patterns" |
| 1 | +import * as cdk from 'aws-cdk-lib'; |
| 2 | +import { DockerImageAsset, Platform } from 'aws-cdk-lib/aws-ecr-assets'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | +import * as ec2 from 'aws-cdk-lib/aws-ec2'; |
| 5 | +import * as ecs from 'aws-cdk-lib/aws-ecs'; |
| 6 | +import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns'; |
8 | 7 |
|
9 | | -const path = require('path') |
| 8 | +const path = require('path'); |
10 | 9 |
|
11 | 10 | export class RepoStack extends cdk.Stack { |
12 | | - /** |
13 | | - * |
14 | | - * @param {Construct} scope |
15 | | - * @param {string} id |
16 | | - * @param {StackProps=} props |
17 | | - */ |
18 | 11 | constructor(scope: Construct, id: string, props?: cdk.StackProps) { |
19 | | - super(scope, id, props) |
| 12 | + super(scope, id, props); |
20 | 13 |
|
21 | | - // base infrastructure |
22 | | - const vpc = new ec2.Vpc(this, 'VPC', {maxAzs: 2}) |
| 14 | + const vpc = new ec2.Vpc(this, 'VPC', {maxAzs: 2}); |
23 | 15 | const cluster = new ecs.Cluster(this, 'Cluster', { |
24 | 16 | clusterName: 'Services', |
25 | 17 | vpc: vpc |
26 | | - }) |
| 18 | + }); |
27 | 19 |
|
28 | 20 | const asset = new DockerImageAsset(this, 'service1', { |
29 | 21 | directory: path.join(__dirname, '../../../src'), |
30 | 22 | platform: Platform.LINUX_AMD64, |
31 | | - }) |
| 23 | + }); |
| 24 | + |
| 25 | + const taskDefinition = new ecs.FargateTaskDefinition(this, "MyFargateTask", { |
| 26 | + cpu: 512, |
| 27 | + memoryLimitMiB: 2048, |
| 28 | + }); |
| 29 | + |
| 30 | + taskDefinition.addVolume({ |
| 31 | + name: 'local-src', |
| 32 | + host: { |
| 33 | + sourcePath: path.join(__dirname, '../../../src/app') |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + const container = taskDefinition.addContainer("MyFargateContainer", { |
| 38 | + image: ecs.ContainerImage.fromDockerImageAsset(asset), |
| 39 | + logging: new ecs.AwsLogDriver({streamPrefix: "MyApp"}), |
| 40 | + memoryLimitMiB: 2048, |
| 41 | + cpu: 512 |
| 42 | + }); |
| 43 | + |
| 44 | + container.addPortMappings({ |
| 45 | + containerPort: 3000 |
| 46 | + }); |
| 47 | + |
| 48 | + // Match the container path with the Dockerfile WORKDIR |
| 49 | + container.addMountPoints({ |
| 50 | + containerPath: '/app', |
| 51 | + sourceVolume: 'local-src', |
| 52 | + readOnly: false |
| 53 | + }); |
32 | 54 |
|
33 | | - // task definition |
34 | | - // Create a load-balanced Fargate service and make it public |
35 | 55 | const albfs = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", { |
36 | | - cluster: cluster, // Required |
37 | | - cpu: 512, // Default is 256 |
38 | | - desiredCount: 2, // Default is 1 |
39 | | - taskImageOptions: { |
40 | | - image: ecs.ContainerImage.fromDockerImageAsset(asset), |
41 | | - containerPort: 3000, |
42 | | - }, |
43 | | - memoryLimitMiB: 2048, // Default is 512 |
| 56 | + cluster: cluster, |
| 57 | + desiredCount: 1, |
| 58 | + taskDefinition: taskDefinition, |
44 | 59 | listenerPort: 80, |
45 | | - publicLoadBalancer: true // Default is true |
46 | | - }) |
| 60 | + publicLoadBalancer: true |
| 61 | + }); |
47 | 62 |
|
48 | 63 | new cdk.CfnOutput(this, 'serviceslb', { |
49 | 64 | value: albfs.loadBalancer.loadBalancerDnsName, |
50 | | - }) |
| 65 | + }); |
51 | 66 | new cdk.CfnOutput(this, 'localstackserviceslb', { |
52 | 67 | value: `${albfs.loadBalancer.loadBalancerDnsName}:4566`, |
53 | | - }) |
| 68 | + }); |
54 | 69 | } |
55 | 70 | } |
56 | 71 |
|
57 | | -module.exports = {RepoStack} |
| 72 | +module.exports = {RepoStack}; |
0 commit comments