-
Notifications
You must be signed in to change notification settings - Fork 21
/
apache.demo.ts
159 lines (147 loc) · 6 KB
/
apache.demo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
/**
* @description
* This is demo stack for Apache web server
* @author @aws-solutions
*/
import {
CfnMapping,
CfnOutput,
NestedStack,
NestedStackProps,
Stack,
} from "aws-cdk-lib";
import {
AmazonLinuxCpuType,
AmazonLinuxVirt,
CloudFormationInit,
InitCommand,
InitFile,
InitPackage,
InitService,
InitServiceRestartHandle,
Instance,
InstanceType,
MachineImage,
} from "aws-cdk-lib/aws-ec2";
import { DemoConstruct } from "../demo.infra";
import { manifest, Workload } from "../exports";
/**
* @class
* @description web server resources construct
*/
export class ApacheDemo extends NestedStack {
readonly region: string;
constructor(scope: Stack, id: string, props?: NestedStackProps) {
super(scope, id, props);
const stack = Stack.of(this);
this.region = stack.region; // Returns the AWS::Region for this stack (or the literal value if known)
//=============================================================================================
// Metadata
//=============================================================================================
this.templateOptions.description = `(${manifest.solutionId}-Demo) - The AWS CloudFormation template for deployment of the ${manifest.solutionName} Apache workload demo resource. Version ${manifest.solutionVersion}`;
this.templateOptions.templateFormatVersion = manifest.templateVersion;
//=============================================================================================
// Map
//=============================================================================================
const map = new CfnMapping(this, "StackMap", {
mapping: {
Apache: {
AccessLog: Workload.Apache.AccessLog, // access log for apache instances, change as needed
InfraConfig:
"https://%%TEMPLATE_BUCKET%%.s3.amazonaws.com/%%SOLUTION_NAME%%/%%VERSION%%/linux_cw_infra.json", // base infra config file
ApacheConfig:
"https://%%TEMPLATE_BUCKET%%.s3.amazonaws.com/%%SOLUTION_NAME%%/%%VERSION%%/apache.config/apache.json", // apache config file
httpdConfig:
"https://%%TEMPLATE_BUCKET%%.s3.amazonaws.com/%%SOLUTION_NAME%%/%%VERSION%%/apache.config/httpd.conf", // httpd config file
CloudWatchAgent:
"https://%%TEMPLATE_BUCKET%%.s3.amazonaws.com/%%SOLUTION_NAME%%/%%VERSION%%/amazon-cloudwatch-agent.rpm",
},
},
});
//=============================================================================================
// Resources
//=============================================================================================
/**
* @description creating demo infrastructure
* @type {DemoConstruct}
*/
const demoInfra: DemoConstruct = new DemoConstruct(this, "ApacheDemoInfra");
const handle: InitServiceRestartHandle = new InitServiceRestartHandle();
/**
* @description cloudformation init configuration for web server
* @type {CloudFormationInit}
*/
const init: CloudFormationInit = CloudFormationInit.fromElements(
InitPackage.rpm(map.findInMap("Apache", "CloudWatchAgent"), {
serviceRestartHandles: [handle],
}),
InitPackage.yum("httpd", { serviceRestartHandles: [handle] }),
InitFile.fromUrl(
"/opt/aws/amazon-cloudwatch-agent/bin/infra_config.json",
map.findInMap("Apache", "InfraConfig")
),
InitFile.fromUrl(
"/opt/aws/amazon-cloudwatch-agent/bin/apache_config.json",
map.findInMap("Apache", "ApacheConfig")
),
InitFile.fromUrl(
"/etc/httpd/conf/httpd.conf",
map.findInMap("Apache", "httpdConfig")
),
InitFile.fromString("/var/www/html/index.html", `Hello World!`, {
mode: "000644",
owner: "apache",
group: "apache",
}),
InitCommand.shellCommand("mkdir /var/log/www/"),
InitCommand.shellCommand("mkdir /var/log/www/error"),
InitCommand.shellCommand("mkdir /var/log/www/access"),
InitService.enable("httpd", {
enabled: true,
ensureRunning: true,
serviceRestartHandle: handle,
})
);
/**
* @description web server instance
* @type {Instance}
*/
const demoEC2: Instance = new Instance(this, "ApacheDemoEC2", {
vpc: demoInfra.demoVPC,
instanceType: new InstanceType("t3.micro"),
machineImage: MachineImage.latestAmazonLinux2({
virtualization: AmazonLinuxVirt.HVM,
cpuType: AmazonLinuxCpuType.X86_64,
}),
init: init,
securityGroup: demoInfra.demoSecurityGroup,
requireImdsv2: true,
});
demoEC2.addUserData(
'echo "======setting up cloudwatch agent======"',
"/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/infra_config.json",
"/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a append-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/apache_config.json",
"curl 127.0.0.1"
);
demoEC2.role.attachInlinePolicy(demoInfra.demoInstancePolicy);
//=============================================================================================
// Output
//=============================================================================================
new CfnOutput(this, "Web URL", {
description: "URL for apache demo server",
value: `http://${demoEC2.instancePublicIp}`,
});
}
}