/
cdk_dot_net_web_app_eb_pipeline-stack.ts
99 lines (89 loc) · 3.54 KB
/
cdk_dot_net_web_app_eb_pipeline-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
import cdk = require("@aws-cdk/core");
import codecommit = require("@aws-cdk/aws-codecommit");
import codebuild = require("@aws-cdk/aws-codebuild");
import codepipeline = require("@aws-cdk/aws-codepipeline");
import codepipeline_actions = require("@aws-cdk/aws-codepipeline-actions");
import awsEcr = require("@aws-cdk/aws-ecr");
import elasticbeanstalk = require("@aws-cdk/aws-elasticbeanstalk");
import { ElasticBeanStalkDeployAction } from "./ElasticBeanstalkDeployAction";
export class CdkDotNetWebAppEbPipelineStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
//Source code repository
const sourceCodeRepo = codecommit.Repository.fromRepositoryName(
this,
"MVCAppRepo",
"Sample-msf-eb-app"
);
//Custom docker image for .net build tools environment
const ecrRepo = awsEcr.Repository.fromRepositoryArn(
this,
"121212121212",
"arn:aws:ecr:us-east-1:121212121212:repository/vs_build_tools2017"
);
//Creating build enviroment using custom docker image
const cdkBuild = new codebuild.PipelineProject(this, "CdkBuild", {
description: "AWS build project for .Net web applications",
environment: {
buildImage: codebuild.WindowsBuildImage.fromEcrRepository(
ecrRepo,
"latest"
)
}
});
//Artifact stores for each pipeline stage
const sourceOutput = new codepipeline.Artifact();
const cdkBuildOutput = new codepipeline.Artifact();
//Adding stages to code pipeline
const pipeline = new codepipeline.Pipeline(this, "Pipeline", {
stages: [
{
stageName: "Source",
actions: [
new codepipeline_actions.CodeCommitSourceAction({
actionName: "CodeCommit_Source",
repository: sourceCodeRepo,
output: sourceOutput
})
]
},
{
stageName: "Build",
actions: [
new codepipeline_actions.CodeBuildAction({
actionName: "cdkBuild",
project: cdkBuild,
input: sourceOutput,
outputs: [cdkBuildOutput]
})
]
}
]
});
//As we are planning to deploy to Elastic Beanstalk we will create EB Environment for ourapplication before adding deploy stage
//objects for access parameters
const appName = "SampleDotNetMVCWebApp";
const app = new elasticbeanstalk.CfnApplication(this, "EBApplication", {
applicationName: appName
});
const elbEnv = new elasticbeanstalk.CfnEnvironment(this, "Environment", {
environmentName: "SampleMVCEBEnvironment",
applicationName: appName,
solutionStackName: "64bit Windows Server 2012 R2 v2.5.0 running IIS 8.5"
});
pipeline.addStage({
stageName: "Deploy",
actions: [
//As Deploy to Elastic Beanstalk action feature is not yet release by AWS we have made our own custom action by implementing codepipeline.IAction interface
//This custom AWS CodePipeline action to deploy artifact package to Elastic Beanstalk
new ElasticBeanStalkDeployAction({
actionName: "DeployToEB",
account: "[TargetAWSAccountNumber]", //replace "[TargetAWSAccountNumber]" with account-2's account number.
applicationName: appName,
environmentName: "SampleMVCEBEnvironment",
input: cdkBuildOutput
})
]
});
}
}