-
Notifications
You must be signed in to change notification settings - Fork 1
/
05-pipeline-to-single-environment-stack.ts
154 lines (137 loc) · 4.14 KB
/
05-pipeline-to-single-environment-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
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
import * as elasticBeanstalk from "@aws-cdk/aws-elasticbeanstalk";
import {
CfnOutput,
Construct,
StackProps,
Stack,
SecretValue,
} from "@aws-cdk/core";
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipelineActions from "@aws-cdk/aws-codepipeline-actions";
import * as codebuild from "@aws-cdk/aws-codebuild";
import * as elasticbeanstalkDeployAction from "./pipeline-deploy-action";
export interface ApplicationProps extends StackProps {
ApplicationName: string;
}
export class ApplicationStack extends Stack {
public readonly applicationName: CfnOutput;
constructor(scope: Construct, id: string, props: ApplicationProps) {
super(scope, id, props);
const application = new elasticBeanstalk.CfnApplication(
this,
"Application",
{
applicationName: props.ApplicationName,
}
);
this.applicationName = new CfnOutput(this, "ApplicationName", {
value: props.ApplicationName,
});
}
}
export interface EnvironmentProps extends StackProps {
EnvironmentName: string;
ApplicationName: string;
SolutionStackName: string;
}
export class EnvironmentStack extends Stack {
public readonly environmentName: CfnOutput;
constructor(scope: Construct, id: string, props: EnvironmentProps) {
super(scope, id, props);
const optionSettingProperties: elasticBeanstalk.CfnEnvironment.OptionSettingProperty[] = [
{
namespace: "aws:ec2:instances",
optionName: "EnableSpot",
value: "true",
},
{
namespace: "aws:ec2:instances",
optionName: "InstanceTypes",
value: "t2.micro,t3.micro,t3.small",
},
{
namespace: "aws:ec2:instances",
optionName: "SpotFleetOnDemandBase",
value: "0", // 0 = 100% Spot
},
{
namespace: "aws:ec2:instances",
optionName: "SpotFleetOnDemandAboveBasePercentage",
value: "0", // 0 = 100% Spot
},
];
const environment = new elasticBeanstalk.CfnEnvironment(
this,
"Environment",
{
environmentName: props.EnvironmentName,
applicationName: props.ApplicationName,
solutionStackName: props.SolutionStackName,
optionSettings: optionSettingProperties,
}
);
this.environmentName = new CfnOutput(this, "EnvironmentName", {
value: props.EnvironmentName,
});
}
}
export interface PipelineProps extends StackProps {
GitHubOrganization: string;
GitHubRepo: string;
ApplicationName: string;
EnvironmentName: string;
}
export class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props: PipelineProps) {
super(scope, id, props);
const build = new codebuild.PipelineProject(this, "Build", {
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
build: {
commands: "./gradlew build",
},
},
}),
});
const sourceOutput = new codepipeline.Artifact();
const buildOutput = new codepipeline.Artifact("BuildOutput");
const pipeline = new codepipeline.Pipeline(this, "Pipeline");
pipeline.addStage({
stageName: "Source",
actions: [
new codepipelineActions.GitHubSourceAction({
actionName: "GitHub_Source",
owner: props.GitHubOrganization,
repo: props.GitHubRepo,
oauthToken: SecretValue.secretsManager("github-token"),
output: sourceOutput,
branch: "master",
trigger: codepipelineActions.GitHubTrigger.POLL,
}),
],
});
pipeline.addStage({
stageName: "Build",
actions: [
new codepipelineActions.CodeBuildAction({
actionName: "Application_Build",
project: build,
input: sourceOutput,
outputs: [buildOutput],
}),
],
});
pipeline.addStage({
stageName: "Deploy",
actions: [
new elasticbeanstalkDeployAction.ElasticBeanStalkDeployAction({
actionName: "Beanstalk_Deploy",
applicationName: props.ApplicationName,
environmentName: props.EnvironmentName,
input: buildOutput,
}),
],
});
}
}