forked from aws-samples/aws-cdk-project-template-for-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-vpc-ecs-stack.ts
66 lines (56 loc) · 2.64 KB
/
sample-vpc-ecs-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as sm from 'aws-cdk-lib/aws-secretsmanager';
import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns';
import * as base from '../../lib/template/stack/vpc/vpc-base-stack';
import { Override } from '../../lib/template/stack/base/base-stack';
import { AppContext } from '../../lib/template/app-context';
import { StackConfig } from '../../lib/template/app-config'
export class SampleVpcEcsStack extends base.VpcBaseStack {
constructor(appContext: AppContext, stackConfig: StackConfig) {
super(appContext, stackConfig);
}
@Override
onLookupLegacyVpc(): base.VpcLegacyLookupProps | undefined {
return {
vpcNameLegacy: this.getVariable('VpcName')
};
}
@Override
onPostConstructor(baseVpc?: ec2.IVpc) {
const databaseHostName = this.getParameter('DatabaseHostName');
const databaseName = this.getParameter('DatabaseName');
const databaseSecretArn = this.getParameter('DatabaseSecretArn');
const databaseSecret = sm.Secret.fromSecretCompleteArn(this, 'secret', databaseSecretArn);
const taskDef = new ecs.FargateTaskDefinition(this, 'TaskDef');
taskDef.addContainer('DefaultContainer', {
image: ecs.ContainerImage.fromAsset(this.stackConfig.FilePath),
logging: new ecs.AwsLogDriver({
streamPrefix: this.withProjectPrefix('backend-fastapi')
}),
environment: {
HOST_NAME: databaseHostName,
DATABASE_NAME: databaseName,
SECRET_ARN: databaseSecretArn,
},
portMappings: [{
containerPort: 80,
protocol: ecs.Protocol.TCP
}]
});
databaseSecret.grantRead(taskDef.taskRole);
const albEcsService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Service', {
cluster: new ecs.Cluster(this, 'cluster', {
vpc: baseVpc,
clusterName: this.withProjectPrefix(this.stackConfig.ClusterName)
}),
memoryLimitMiB: this.stackConfig.Memory,
cpu: this.stackConfig.Cpu,
taskDefinition: taskDef,
publicLoadBalancer: false,
desiredCount: parseInt(this.stackConfig.DesiredCount)
});
const databaseSecurityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, 'DatabaseSecurityGroup', this.getParameter('DatabaseSecurityGroup'));
databaseSecurityGroup.addIngressRule(albEcsService.service.connections.securityGroups[0], ec2.Port.tcp(3306), 'from backend sg');
}
}