-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.yaml
281 lines (281 loc) · 8.25 KB
/
package.yaml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
AWSTemplateFormatVersion: '2010-09-09'
Description: CodePipeline integration with the Bitbucket Server
Parameters:
BitbucketSecret:
Type: String
Description: Bitbucket webhook secret used to sign webhook events. You should
define the secret and use the same value here and in the Bitbucket server webhook.
NoEcho: true
BitbucketServerUrl:
Type: String
Description: URL of your Bitbucket Server e.g. http://server:port
BitbucketToken:
Type: String
Description: Personal token generated to access the repositories
NoEcho: true
EndpointType:
Type: String
Description: Select the type of endpoint to integrate with the Lambda Function
AllowedValues:
- API Gateway
- Application Load Balancer
LBCIDR:
Type: String
Description: CIDR allowed to communicate with the Load Balancer. It should allow
the Bitbucket server IP address. Leave it blank if you are using the API Gateway
endpoint type.
LBSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Subnets where the Application Load Balancer run. Leave it blank if
you are using the API Gateway endpoint type.
LBSSLCertificateArn:
Type: String
Description: SSL Certificate to associate with the Application Load Balancer.
Leave it blank if you are using the API Gateway endpoint type.
LambdaSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Subnets where the Lambda Function run
S3BucketCodePipelineName:
Type: String
Description: S3 bucket name to store the Bitbucket repository content
AllowedPattern: ^[a-z]*$
ConstraintDescription: This field should contain only lower case characters
VPCID:
Type: AWS::EC2::VPC::Id
Description: VPC ID where the Application Load Balancer and the Lambda function
run
WebProxyHost:
Type: String
Description: Hostname of your Proxy server used by the Lambda Function to access
the Bitbucket server. If you don't need a web proxy leave it blank. e.g. myproxy.mydomain.com
WebProxyPort:
Type: String
Description: Port of your Proxy server used by the Lambda Function to access the
Bitbucket server. If you don't need a web proxy leave it blank. e.g. 8080
Conditions:
EndpointTypeALB:
Fn::Equals:
- Ref: EndpointType
- Application Load Balancer
EndpointTypeAPIGW:
Fn::Equals:
- Ref: EndpointType
- API Gateway
Resources:
S3BucketCodePipeline:
Type: AWS::S3::Bucket
Properties:
BucketName:
Ref: S3BucketCodePipelineName
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
KMSKey:
Type: AWS::KMS::Key
Properties:
Description: CMK used by the Lambda Function to encrypt the environment variables
KeyPolicy:
Version: '2012-10-17'
Id: root
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS:
Fn::Sub: arn:aws:iam::${AWS::AccountId}:root
Action: kms:*
Resource: '*'
IamPolicyLambdaFunction:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: AWS CodePipeline integration with BitBucket Server
ManagedPolicyName: CodePipeline-Bitbucket-Integration
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:PutObject
Resource:
- Fn::Sub: arn:aws:s3:::${S3BucketCodePipeline}
- Fn::Sub: arn:aws:s3:::${S3BucketCodePipeline}/*
- Effect: Allow
Action:
- kms:decrypt
Resource:
- Fn::GetAtt:
- KMSKey
- Arn
IamRoleLambdaFunction:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- Ref: IamPolicyLambdaFunction
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
RoleName: CodePipeline-Bitbucket-Integration
SgLambdaFunction:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SG used by the Bitbucket Integration Lambda Function
GroupName: CodePipeline-Bitbucket-Integration-Lambda
VpcId:
Ref: VPCID
SgAlb:
Type: AWS::EC2::SecurityGroup
Condition: EndpointTypeALB
Properties:
GroupDescription: SG used by the Bitbucket Integration ALB
GroupName: CodePipeline-Bitbucket-Integration-ALB
SecurityGroupIngress:
- CidrIp:
Ref: LBCIDR
Description: Range of IP allowed to connect to the Load Balancer
FromPort: 443
ToPort: 443
IpProtocol: tcp
VpcId:
Ref: VPCID
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: CodePipeline-Bitbucket-Integration
Environment:
Variables:
BITBUCKET_SERVER_URL:
Ref: BitbucketServerUrl
BITBUCKET_TOKEN:
Ref: BitbucketToken
BITBUCKET_SECRET:
Ref: BitbucketSecret
S3BUCKET:
Ref: S3BucketCodePipeline
WEBPROXY_HOST:
Ref: WebProxyHost
WEBPROXY_PORT:
Ref: WebProxyPort
Handler: index.handler
KmsKeyArn:
Fn::GetAtt:
- KMSKey
- Arn
Role:
Fn::GetAtt:
- IamRoleLambdaFunction
- Arn
Code:
S3Bucket: lexlabs3bucketmgmtapps
S3Key: f4a779b4953357ca1517a6a0e76994f4
Runtime: nodejs14.x
Timeout: '30'
VpcConfig:
SecurityGroupIds:
- Ref: SgLambdaFunction
SubnetIds:
Ref: LambdaSubnets
LambdaPermissionAlb:
Type: AWS::Lambda::Permission
Condition: EndpointTypeALB
Properties:
Action: lambda:InvokeFunction
FunctionName:
Ref: LambdaFunction
Principal: elasticloadbalancing.amazonaws.com
LambdaPermissionAPIGw:
Type: AWS::Lambda::Permission
Condition: EndpointTypeAPIGW
Properties:
Action: lambda:InvokeFunction
FunctionName:
Ref: LambdaFunction
Principal: apigateway.amazonaws.com
Alb:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Condition: EndpointTypeALB
Properties:
Name: cp-bitbucket-int
Scheme: internet-facing
SecurityGroups:
- Ref: SgAlb
Subnets:
Ref: LBSubnets
IpAddressType: ipv4
AlbListener443:
Type: AWS::ElasticLoadBalancingV2::Listener
Condition: EndpointTypeALB
Properties:
DefaultActions:
- TargetGroupArn:
Ref: TargetGroup
Type: forward
LoadBalancerArn:
Ref: Alb
Port: 443
Protocol: HTTPS
Certificates:
- CertificateArn:
Ref: LBSSLCertificateArn
TargetGroup:
DependsOn: LambdaPermissionAlb
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Condition: EndpointTypeALB
Properties:
TargetType: lambda
Name: cp-bitbucket-int
Targets:
- Id:
Fn::GetAtt:
- LambdaFunction
- Arn
RestApi:
Type: AWS::ApiGateway::RestApi
Condition: EndpointTypeAPIGW
Properties:
Description: API used by the AWS CodePipeline integration with the Bitbucket
Server
EndpointConfiguration:
Types:
- REGIONAL
Name: CodePipeline-Bitbucket-Integration
RestApiMethod:
Type: AWS::ApiGateway::Method
Condition: EndpointTypeAPIGW
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
ResourceId:
Fn::GetAtt:
- RestApi
- RootResourceId
RestApiId:
Ref: RestApi
RestApiDeployment:
Type: AWS::ApiGateway::Deployment
Condition: EndpointTypeAPIGW
DependsOn: RestApiMethod
Properties:
Description: Stage Deployment
RestApiId:
Ref: RestApi
StageName: prod
Outputs:
EndpointUrlAlb:
Condition: EndpointTypeALB
Value:
Fn::GetAtt:
- Alb
- DNSName